Skip to content

Instantly share code, notes, and snippets.

@elizabrock
Created September 12, 2014 18:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save elizabrock/47d109372d8f3557ba0a to your computer and use it in GitHub Desktop.
Save elizabrock/47d109372d8f3557ba0a to your computer and use it in GitHub Desktop.
require 'spec_helper'
describe '/api/v2/favorites', :api do
let!(:facility) { FactoryGirl.create(:facility) }
let!(:user) { FactoryGirl.create(:user, facility: facility) }
let!(:user2) { FactoryGirl.create(:user, facility: facility) }
let!(:other_user) { FactoryGirl.create(:user, facility: facility) }
before(:each) { user.reset_authentication_token! }
describe 'POST' do
context 'user favorites another user' do
it 'creates a favorite to the associated user' do
post '/api/v2/favorites',
{
token: user.authentication_token,
user_id: user2.id
}
Favorite.last.tap do |f|
f.user_id.should == user.id
f.contact_id.should == user2.id
end
expected = {
"name"=> user2.name,
"id"=>user2.id,
"avatar_url"=>user2.avatar.polaroid_url,
"phone"=>user2.formatted_phone,
"demographics" => [],
"favorite" => true,
"support" => false
}
last_response.status.should == 200
last_response_json.should == expected
end
end
end
describe 'DELETE' do
context 'user unfavorites a user' do
it 'deletes the favorite to the associated user' do
favorite = Favorite.where(contact_id: user2.id, user_id: user.id).first_or_create
user.favorites.count.should == 1
delete "/api/v2/favorites/#{user2.id}",
{
token: user.authentication_token,
}
last_response.status.should == 200
end
context 'when favorite does not exist' do
it 'returns status 401 for unauthorized favorite' do
favorite = Favorite.where(contact_id: user2.id, user_id: user.id).first_or_create
user.favorites.count.should == 1
delete "/api/v2/favorites/#{other_user.id}",
{
token: user.authentication_token
}
last_response.status.should == 401
end
end
end
end
describe 'GET' do
it 'returns all favorited users' do
Favorite.create(contact_id: user2.id, user_id: user.id)
Favorite.create(contact_id: other_user.id, user_id: user.id)
get 'api/v2/favorites', token: user.authentication_token
expected = [
{
"name"=> other_user.name,
"id"=>other_user.id,
"avatar_url"=>other_user.avatar.polaroid_url,
"phone"=>other_user.formatted_phone,
"demographics" => [],
"favorite" => true,
"support" => false
},
{
"name"=> user2.name,
"id"=>user2.id,
"avatar_url"=>user2.avatar.polaroid_url,
"phone"=>user2.formatted_phone,
"demographics" => [],
"favorite" => true,
"support" => false
}
]
last_response_json.should =~ expected
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment