Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Api prog and meeting part 2 #194

Merged
merged 11 commits into from
Jan 12, 2020
4 changes: 2 additions & 2 deletions server/app/controllers/api/v1/api_users_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ def create

# GET /api_users/:id
def show
json_response(@api_user)
json_response(@api_user),
end

# PUT /api_users/:id
def update
@api_user.update(api_user_params)
@api_user.update!(api_user_params)
head :no_content
end

Expand Down
1 change: 1 addition & 0 deletions server/app/models/api_user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@ class ApiUser < ApplicationRecord
has_many :wishes, dependent: :destroy
# validation
validates_presence_of :email, :password_digest
validates :email, uniqueness: true, on: :create and :update
sibsmc marked this conversation as resolved.
Show resolved Hide resolved
end

1 change: 0 additions & 1 deletion server/app/views/api/v1/wishes/.#index.json.jbuilder

This file was deleted.

2 changes: 2 additions & 0 deletions server/app/views/api/v1/wishes/index.json.jbuilder
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@


json.array! @wishes do |wish|
json.partial! 'wish', wish: wish
end
42 changes: 41 additions & 1 deletion server/spec/requests/api/v1/api_users_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@
end

context 'when the request is invalid as no params' do
let(:invalid_attributes) { { api_user: { email: nil } }.to_json }
let(:invalid_attributes) do
{ email: nil } .to_json
before { post '/api/v1/api_users', params: invalid_attributes }

it 'returns status code 422' do
Expand All @@ -81,8 +82,25 @@
expect(json['message'])
.to match(/param is missing or the value is empty: api_user/)
end
end
end

context 'when the request is invalid as email already in use' do
let(:invalid_email) do
{ 'email': '[email protected]', 'password_digest': 'password2' } .to_json
before { post '/api/v1/api_users', params: invalid_email }

it 'returns status code 422' do
expect(response).to have_http_status(422)
end

it 'returns a validation failure message' do
expect(json['message'])
.to match(/Validation failed: Email has already been taken/)
end
end
end

context 'when the request is invalid as only some requird params' do
let(:invalid_attributes) { { 'api_user': { 'email': '[email protected]' } }.to_json }
before { post '/api/v1/api_users', params: invalid_attributes }
Expand Down Expand Up @@ -123,6 +141,28 @@

end



context 'when email already in use' do
let(:api_user_id) {api_users.first.id}
let(:invalid_email_update) do
# send json payload
{ 'email': '[email protected]'}.to_json
before { patch "/api/v1/api_users/#{api_user_id}/", params: invalid_email_updates}

it 'returns status code 422' do
expect(response).to have_http_status(422)
end

it 'returns message informing no user with that id' do
expect(json['message']).to match(/Validation failed: Email has already been taken/)
end
end
end




context 'when api_user does not exist' do
let(:api_user_id) {0}
let(:valid_attributes) do
Expand Down
17 changes: 10 additions & 7 deletions server/spec/requests/api/v1/wishes_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

# initialize test data
let!(:api_user){create(:api_user)}
let!(:wishes){create_list(:wish,5,api_user_id: api_user.id)}
let!(:programminglanguage){create(:programminglanguage)}
let!(:meetinginterval){create(:meetinginterval)}
let!(:wishes){create_list(:wish,5,api_user_id: api_user.id, programminglanguage_id: programminglanguage.id, meetinginterval_id: meetinginterval.id)}
let(:api_user_id){api_user.id}
let(:id){wishes.first.id}

Expand Down Expand Up @@ -65,8 +67,8 @@

# Test suite for PUT /api_users/:api_user_id/wishes
describe 'POST /api/v1/api_users/#{api_user_id}/wishes' do
let(:valid_attributes) { { available_offline: false, available_online: true, goal: 'Learn Postgresql'} }
let(:not_available_attributes) { { available_offline: false, available_online: false, goal: 'Learn GraphSQL'} }
let(:valid_attributes) { { available_offline: false, available_online: true, goal: 'Learn Postgresql', programminglanguage_id: programminglanguage.id, meetinginterval_id: meetinginterval.id} }
let(:not_available_attributes) { { available_offline: false, available_online: false, goal: 'Learn GraphSQL', programminglanguage_id: programminglanguage.id, meetinginterval_id: meetinginterval.id} }

context 'when request attributes are valid' do
before { post "/api/v1/api_users/#{api_user_id}/wishes", params: valid_attributes }
Expand All @@ -84,7 +86,8 @@
end

it 'returns a failure message' do
expect(response.body).to match(/Validation failed: Goal can't be blank/)
# expect(response.body).to match(/Validation failed: Goal can't be blank/)
expect(response.body).to match(/Validation failed: Programminglanguage must exist, Meetinginterval must exist, Goal can't be blank/)
end
end

Expand All @@ -103,11 +106,11 @@


# Test suite for PUT /api_users/:api_user_id/wishes/:id
describe 'PUT /api/v1/api_users/:api_user_id/wishes/:id' do
describe 'PATCH /api/v1/api_users/:api_user_id/wishes/:id' do
let(:valid_attributes) { { goal: 'Improve Mysql' } }
let(:not_available_attributes) { { available_offline: false, available_online: false, goal: 'Learn GraphSQL'} }

before { put "/api/v1/api_users/#{api_user_id}/wishes/#{id}", params: valid_attributes }
before { patch "/api/v1/api_users/#{api_user_id}/wishes/#{id}", params: valid_attributes }

context 'when wish exists' do
it 'returns status code 204' do
Expand All @@ -121,7 +124,7 @@
end

context 'when wish exists but the user is not available' do
before { put "/api/v1/api_users/#{api_user_id}/wishes/#{id}", params: not_available_attributes }
before { patch "/api/v1/api_users/#{api_user_id}/wishes/#{id}", params: not_available_attributes }

it 'returns status code 422' do
expect(response).to have_http_status(422)
Expand Down