This repository has been archived by the owner on Aug 8, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9ad8e0d
commit db13b25
Showing
6 changed files
with
186 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
module Todo | ||
module Models | ||
class ListItem | ||
include DataMapper::Resource | ||
|
||
property :id, Serial | ||
property :description, String | ||
property :priority, Integer | ||
property :completed, Boolean | ||
property :due_date, Date | ||
|
||
belongs_to :user, 'Todo::Models::User' | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
require 'spec_helper' | ||
|
||
describe 'Todos API', api: true do | ||
let(:user_params) { { email: 'test@test.com', password: '123test123' } } | ||
|
||
let!(:user) { Todo::Models::User.create user_params } | ||
|
||
let!(:access_token) do | ||
session_creator = Todo::Services::SessionCreator.new(user_params) | ||
expect(session_creator).to be_valid | ||
session_creator.access_token | ||
end | ||
|
||
let(:list_item) do | ||
list_item = user.list_items.create(list_item_params) | ||
expect(list_item).to be_saved | ||
list_item | ||
end | ||
|
||
context 'when creating a list item' do | ||
let(:list_item_params) do | ||
{ | ||
description: 'Buy beer', | ||
priority: 1, | ||
completed: false, | ||
due_date: '2014-01-01' | ||
} | ||
end | ||
|
||
context 'when passing an invalid access token' do | ||
it 'should not allow to create' do | ||
post '/api/list_item', { list_item: list_item_params } | ||
|
||
expect(last_response.headers['Content-Type']).to eq 'application/json;charset=utf-8' | ||
expect(last_response.headers['Set-Cookie']).to be_nil | ||
expect(last_response.body).to have_json_path 'errors/default' | ||
expect(last_response.status).to eq 501 | ||
end | ||
|
||
it 'should not allow to edit' do | ||
edit_params = { list_item: { description: 'Buy wine bottles' } } | ||
put "/api/list_item/#{list_item.id}", edit_params | ||
|
||
expect(last_response.headers['Content-Type']).to eq 'application/json;charset=utf-8' | ||
expect(last_response.headers['Set-Cookie']).to be_nil | ||
expect(last_response.body).to have_json_path 'errors/default' | ||
expect(last_response.status).to eq 501 | ||
end | ||
|
||
it 'should not allow to destroy' do | ||
delete "/api/list_item/#{list_item.id}" | ||
|
||
expect(last_response.headers['Content-Type']).to eq 'application/json;charset=utf-8' | ||
expect(last_response.headers['Set-Cookie']).to be_nil | ||
expect(last_response.body).to have_json_path 'errors/default' | ||
expect(last_response.status).to eq 501 | ||
end | ||
end | ||
|
||
context 'when passing a valid access token' do | ||
before do | ||
set_cookie "access_token=#{access_token}" | ||
end | ||
|
||
it 'should create a list item' do | ||
post '/api/list_item', { list_item: list_item_params } | ||
|
||
expect(last_response.headers['Content-Type']).to eq 'application/json;charset=utf-8' | ||
expect(last_response.headers['Set-Cookie']).to be_nil | ||
last_response.body.tap do |body| | ||
expect(body).to have_json_path 'id' | ||
expect(body).to have_json_path 'description' | ||
expect(body).to have_json_path 'priority' | ||
expect(body).to have_json_path 'due_date' | ||
expect(body).to have_json_path 'completed' | ||
end | ||
expect(last_response.status).to eq 200 | ||
end | ||
|
||
it 'should edit a list item owned by you' do | ||
edit_params = { list_item: { description: 'Buy wine bottles' } } | ||
put "/api/list_item/#{list_item.id}", edit_params | ||
|
||
expect(last_response.headers['Content-Type']).to eq 'application/json;charset=utf-8' | ||
expect(last_response.headers['Set-Cookie']).to be_nil | ||
last_response.body.tap do |body| | ||
expect(body).to have_json_path 'id' | ||
expect(body).to have_json_path 'description' | ||
expect(body).to have_json_path 'priority' | ||
expect(body).to have_json_path 'due_date' | ||
expect(body).to have_json_path 'completed' | ||
expect(JSON.parse(body)['description']).to eq 'Buy wine bottles' | ||
end | ||
|
||
expect(last_response.status).to eq 200 | ||
end | ||
|
||
it 'should destroy a list item owned by you' do | ||
delete "/api/list_item/#{list_item.id}" | ||
|
||
expect(last_response.headers['Content-Type']).to eq 'application/json;charset=utf-8' | ||
expect(last_response.headers['Set-Cookie']).to be_nil | ||
expect(last_response.body).to eq '' | ||
expect(last_response.status).to eq 200 | ||
end | ||
end | ||
end | ||
end |