Skip to content

Instantly share code, notes, and snippets.

@r38y
Created April 7, 2014 13:04
Show Gist options
  • Save r38y/10019910 to your computer and use it in GitHub Desktop.
Save r38y/10019910 to your computer and use it in GitHub Desktop.
require 'spec_helper'
describe 'creating a person' do
it 'works with valid attributes' do
user = create(:user)
post "/api/people.json",
{ person: { name: 'Bob Johnson' } },
{ 'Authorization' => "Token token=#{user.authentication_token}" }
expect(response.status).to eq 201
end
it 'works with all attributes' do
user = create(:user)
person = {
person: {
name: 'Bob Johnson',
nicknames: 'Bob',
about: 'I am a man.',
twitter_pointer: 'robert',
facebook_pointer: 'http://facebook.com/robert',
github_pointer: 'http://github.com/robert',
}
}
post "/api/people.json",
person,
{ 'Authorization' => "Token token=#{user.authentication_token}" }
expect(response.body).to be_json_eql(person.to_json).
excluding(:twitter_url, :facebook_url, :github_url, :photo_url)
expect(response.status).to eq 201
end
it 'sets the person owner to the current user' do
user = create(:user)
post "/api/people.json",
{ person: { name: 'Bob Johnson' } },
{ 'Authorization' => "Token token=#{user.authentication_token}" }
person = Person.first
expect(person.owner).to eq user
end
it 'fails with invalid person attributes' do
user = create(:user)
post "/api/people.json",
{ person: { name: nil } },
{ 'Authorization' => "Token token=#{user.authentication_token}" }
expect(parse_json(response.body)['errors']).to_not be_blank
expect(response.status).to eq 422
end
it 'fails if the token is wrong' do
post "/api/people.json", { name: 'John' }, { 'Authorization' => "Token token=wrong" }
expect(parse_json(response.body)['error']).to_not be_blank
expect(response.status).to eq 401
end
end
require 'spec_helper'
describe 'updating a person' do
it 'works with valid attributes' do
user = create(:user)
person = create(:person, owner: user, name: 'Tyler')
expect {
put "/api/people/#{person.id}.json",
{ person: { name: 'Bob Johnson' } },
{ 'Authorization' => "Token token=#{user.authentication_token}" }
person.reload
}.to change(person, :name).from('Tyler').to('Bob Johnson')
expect(parse_json(response.body)['person']['name']).to eq 'Bob Johnson'
expect(response.status).to eq 200
end
it 'fails if the current user is not the owner' do
user = create(:user)
person = create(:person)
expect(person.owner).to_not eq user
put "/api/people/#{person.id}.json",
{person: { name: 'some new name' } },
{ 'Authorization' => "Token token=#{user.authentication_token}" }
expect(parse_json(response.body)['error']).to_not be_blank
expect(response.status).to eq 404
end
it 'fails with invalid person attributes' do
user = create(:user)
person = create(:person, owner: user)
put "/api/people/#{person.id}.json",
{ person: { name: nil } },
{ 'Authorization' => "Token token=#{user.authentication_token}" }
expect(parse_json(response.body)['errors']).to_not be_blank
expect(response.status).to eq 422
end
it 'fails if the token is wrong' do
user = create(:user)
person = create(:person, owner: user)
put "/api/people/#{person.id}.json",
{ name: 'John' },
{ 'Authorization' => "Token token=wrong" }
expect(parse_json(response.body)['error']).to_not be_blank
expect(response.status).to eq 401
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment