Skip to content

Commit

Permalink
add tests for API.
Browse files Browse the repository at this point in the history
  • Loading branch information
Blair Anderson committed Mar 10, 2015
1 parent 5250bf6 commit fa37fd5
Show file tree
Hide file tree
Showing 10 changed files with 174 additions and 22 deletions.
16 changes: 16 additions & 0 deletions app/serializers/item_comment_serializer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
class ItemCommentSerializer < ActiveModel::Serializer
# self.per_page = 1

# has_scope :newest, type: :boolean
# has_scope :disabled, type: :boolean
# has_scope :by_degree
# has_scope :by_period, :using => [:started_at, :ended_at], :type => :hash


# filter #(optional) - Filters are used to narrow down the events used in an analysis request based on event property values.
# timeframe #(optional) - A Timeframe specifies the events to use for analysis based on a window of time.
# property_names #(optional) - A URL-encoded array of strings containing properties you wish to extract. If this parameter is omitted, all properties will be returned.

# cache key: 'posts', expires_in: 3.hours
attributes :id, :content, :user_id, :item_id, :created_at, :updated_at
end
2 changes: 1 addition & 1 deletion config/initializers/active_api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
resources: 'items',
do: [
{
resources: 'comments'
resources: 'item_comments',
}
]
},
Expand Down
4 changes: 1 addition & 3 deletions config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
Rails.application.routes.draw do
mount ActiveApi::Engine => '/aappii'

root to: 'items#index'
resources :user_sessions, only: [:new, :create, :destroy]
resources :users, except: [:index]
Expand All @@ -13,7 +11,7 @@
end
end


get 'login' => 'user_sessions#new', as: :login
post 'logout' => 'user_sessions#destroy', as: :logout
mount ActiveApi::Engine => '/api'
end
36 changes: 18 additions & 18 deletions spec/acceptance/users_spec.rb
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
require 'rails_helper'
require 'rspec_api_documentation/dsl'

resource "users" do
get "/user" do
example "Current User Resource" do
user = FactoryGirl.create(:user, password: "password", password_confirmation: "password")
login_user_post(user.username, "password")
get "/user"
status.should == 200
end
end

get "/user/:username" do
example "User Resource" do
user = FactoryGirl.create(:user)
get "users/#{user.username}"
status.should == 200
end
end
end
# resource "users" do
# get "/user" do
# example "Current User Resource" do
# user = FactoryGirl.create(:user, password: "password", password_confirmation: "password")
# login_user_post(user.username, "password")
# get "/user"
# status.should == 200
# end
# end
#
# get "/user/:username" do
# example "User Resource" do
# user = FactoryGirl.create(:user)
# get "users/#{user.username}"
# status.should == 200
# end
# end
# end
7 changes: 7 additions & 0 deletions spec/factories/item_comments.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
FactoryGirl.define do
factory :item_comment do
item
user
content {Faker::Company.bs[0..250]}
end
end
7 changes: 7 additions & 0 deletions spec/factories/items.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
FactoryGirl.define do
factory :item do
user
title {Faker::Company.bs[0..250]}
url {Faker::Internet.url}
end
end
4 changes: 4 additions & 0 deletions spec/rails_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,7 @@
def login_user_post(user, password)
page.driver.post(user_sessions_url, { username: user, password: password})
end

def json
@json ||= JSON.parse(response.body)
end
47 changes: 47 additions & 0 deletions spec/requests/item_comments_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
require 'rails_helper'



describe ActiveApi, 'item_comments' do

describe "GET /items/:item_id/item_comments" do
before do

end
it 'sends a list of comments for a given item' do
item = FactoryGirl.create(:item)

length = 10
FactoryGirl.create_list(:item_comment, length, item: item)

get active_api_path + "items/#{item.id}/item_comments?parent_resource=foo"
expect(response).to be_success
expect(json['item_comments'].length).to eq(length) # check to make sure the right amount of messages are returned
end

end

describe "POST /items/:item_id/item_comments" do
it 'allows an authenticated user to create a comment'
end

describe "GET /items/:item_id/item_comments/:id" do
it 'sends a single comment for a given item.'
end

describe "PATCH /items/:item_id/item_comments/:id" do
it 'allows an authenticated user to update a comment'
end

describe "PUT /items/:item_id/item_comments/:id" do
it 'allows an authenticated user to update a comment'
end

describe "DELETE /items/:item_id/item_comments/:id" do
it 'allows an authenticated user to delete a comment'
end

end



37 changes: 37 additions & 0 deletions spec/requests/items_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
require 'rails_helper'


describe ActiveApi, "items" do
describe "GET /items" do
it 'sends a list of items' do
length = 10

FactoryGirl.create_list(:item, length)

get active_api_path + "items"
expect(response).to be_success # test for the 200 status-code
expect(json['items'].length).to eq(length) # check to make sure the right amount of messages are returned
end
end

describe "GET /items/:id" do
it 'sends an item'
end

describe "POST /items" do
it 'lets an authenticated user create an item'
end

describe "PATCH /items/:id" do
it 'lets an authenticated user update an item'
end

describe "PUT /items/:id" do
it 'lets an authenticated user update an item'
end

describe "DELETE /items/:id" do
it 'lets an authenticated user delete an item'
end

end
36 changes: 36 additions & 0 deletions spec/requests/users_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
require 'rails_helper'

describe ActiveApi, 'users' do
describe 'GET /users' do
it 'sends a list of users' do
length = 10

FactoryGirl.create_list(:user, length)

get active_api_path + 'users'
expect(response).to be_success # test for the 200 status-code
expect(json['users'].length).to eq(length) # check to make sure the right amount of messages are returned
end
end

describe 'GET /users/:id' do
it 'sends a user'
end

describe 'POST /users' do
it 'lets you create an account'
end

describe 'PATCH /users/:id' do
it 'allows an authenticated user to update their account.'
end

describe 'PUT /users/:id' do
it 'allows an authenticated user to update their account.'
end

describe 'DELETE /users/:id' do
it 'allows an authenticated user to delete their account.'
end

end

0 comments on commit fa37fd5

Please sign in to comment.