From 73087ac5a9341924aa0471f047b8eed05a765f80 Mon Sep 17 00:00:00 2001 From: sibsmc Date: Sat, 23 Nov 2019 18:27:12 +0100 Subject: [PATCH 1/8] updated seed file and amended models and views --- server/app/controllers/api/v1/wishes_controller.rb | 5 ++++- server/app/models/programminglanguage.rb | 1 + server/app/models/wish.rb | 3 +++ server/app/views/api/v1/api_users/_api_user.json.jbuilder | 2 +- server/app/views/api/v1/api_users/index.json.jbuilder | 4 ++++ server/db/seeds.rb | 6 +++--- 6 files changed, 16 insertions(+), 5 deletions(-) diff --git a/server/app/controllers/api/v1/wishes_controller.rb b/server/app/controllers/api/v1/wishes_controller.rb index f623bcc9..ac4ccfc3 100644 --- a/server/app/controllers/api/v1/wishes_controller.rb +++ b/server/app/controllers/api/v1/wishes_controller.rb @@ -33,7 +33,7 @@ def destroy private def wish_params - params.permit(:available_offline, :available_online, :goal, :api_user_id, :id) + params.permit(:available_offline, :available_online, :goal, :api_user_id, :id, :programminglanguage_id, :meetinginterval_id) end def set_api_user @@ -44,4 +44,7 @@ def set_api_user_wish @wish = @api_user.wishes.find_by!(id: params[:id]) if @api_user end + # def set_programminglanguage + # @programminglanguage = Programminglanguage.find(params[:programminglanguage_id]) + # end end diff --git a/server/app/models/programminglanguage.rb b/server/app/models/programminglanguage.rb index f45e72ef..bb96d79d 100644 --- a/server/app/models/programminglanguage.rb +++ b/server/app/models/programminglanguage.rb @@ -1,3 +1,4 @@ class Programminglanguage < ApplicationRecord + has_many :wishes validates_presence_of :language end diff --git a/server/app/models/wish.rb b/server/app/models/wish.rb index 508ae832..ae0e4b73 100644 --- a/server/app/models/wish.rb +++ b/server/app/models/wish.rb @@ -1,5 +1,8 @@ class Wish < ApplicationRecord belongs_to :api_user + belongs_to :programminglanguage + # has_one :programminglanguage, class_name: 'Programminglanguage', foreign_key: 'programminglanguage_id' + delegate :language, :to => :programminglanguage # validation validates_presence_of :goal validate :online_or_offline diff --git a/server/app/views/api/v1/api_users/_api_user.json.jbuilder b/server/app/views/api/v1/api_users/_api_user.json.jbuilder index 0dbc6f36..da016a7c 100644 --- a/server/app/views/api/v1/api_users/_api_user.json.jbuilder +++ b/server/app/views/api/v1/api_users/_api_user.json.jbuilder @@ -3,5 +3,5 @@ json.call(api_user, :id, :mentor, :mentee) json.wishes api_user.wishes do |wish| - json.call(wish, :id, :goal) + json.call(wish, :id, :goal, :available_online, :available_offline) end diff --git a/server/app/views/api/v1/api_users/index.json.jbuilder b/server/app/views/api/v1/api_users/index.json.jbuilder index bc49df00..7889b00d 100644 --- a/server/app/views/api/v1/api_users/index.json.jbuilder +++ b/server/app/views/api/v1/api_users/index.json.jbuilder @@ -3,3 +3,7 @@ json.array! @api_users do |api_user| json.partial! 'api_user', api_user: api_user end +json.array! @wishes do |wish| + json.partial! 'wish', wish: wish +end + diff --git a/server/db/seeds.rb b/server/db/seeds.rb index a75bf2a1..2e3f4315 100644 --- a/server/db/seeds.rb +++ b/server/db/seeds.rb @@ -10,9 +10,9 @@ Meetinginterval.create([{interval: 'weekly'}, {interval: 'daily'}]) Programminglanguage.create([{language: 'python'},{language: 'SCALA'}]) -users.first.wishes.create([{available_offline: false, available_online: true, goal: 'Code real good'}]) -users.second.wishes.create([{available_offline: true, available_online: true, goal: 'Get smarter'}]) -users.first.wishes.create([{available_offline: false, available_online: true, goal: 'Code really well in Python'}]) +users.first.wishes.create([{available_offline: false, available_online: true, goal: 'Code real good', programminglanguage_id: Programminglanguage.second.id, meetinginterval_id: Meetinginterval.first.id}]) +users.second.wishes.create([{available_offline: true, available_online: true, goal: 'Get smarter', programminglanguage_id: Programminglanguage.first.id, meetinginterval_id: Meetinginterval.first.id}]) +users.first.wishes.create([{available_offline: false, available_online: true, goal: 'Code really well in Python', programminglanguage_id: Programminglanguage.first.id, meetinginterval_id: Meetinginterval.second.id}]) p "Created #{ApiUser.count} api_user entries" p "Created #{Meetinginterval.count} meeting interval entries" From 0ad8fbd0c4e38b848ae72c6eafbfe2a47cb43612 Mon Sep 17 00:00:00 2001 From: sibsmc Date: Sat, 23 Nov 2019 20:33:29 +0100 Subject: [PATCH 2/8] programming language and meeting interval information now pulled from tables during api user and wish calls --- server/app/controllers/api/v1/api_users_controller.rb | 6 ++++-- server/app/controllers/api/v1/wishes_controller.rb | 11 +++++------ server/app/models/meetinginterval.rb | 2 +- server/app/models/wish.rb | 3 ++- .../views/api/v1/api_users/_api_user.json.jbuilder | 3 ++- server/app/views/api/v1/wishes/.#index.json.jbuilder | 1 + server/app/views/api/v1/wishes/_wish.json.jbuilder | 5 +++++ server/app/views/api/v1/wishes/index.json.jbuilder | 3 +++ 8 files changed, 23 insertions(+), 11 deletions(-) create mode 120000 server/app/views/api/v1/wishes/.#index.json.jbuilder create mode 100644 server/app/views/api/v1/wishes/_wish.json.jbuilder create mode 100644 server/app/views/api/v1/wishes/index.json.jbuilder diff --git a/server/app/controllers/api/v1/api_users_controller.rb b/server/app/controllers/api/v1/api_users_controller.rb index 3a0a6d73..49dd8190 100644 --- a/server/app/controllers/api/v1/api_users_controller.rb +++ b/server/app/controllers/api/v1/api_users_controller.rb @@ -1,12 +1,14 @@ class Api::V1::ApiUsersController < ApplicationController before_action :set_api_user, only: [:show, :update, :destroy] - #xbefore_action :set_default_response_format + #GET /api_users def index - @api_users = ApiUser.includes(:wishes).all + @api_users = ApiUser.includes(:wishes).includes(wishes: :programminglanguage).includes(wishes: :meetinginterval).all + #Explanation regarding includes: not necessary to link programming languages and meeting interval to ApiUser, but it means there is only one call to the database during which it pulls all the information linked by foreign keys in case it needs it in the future. @api_users = @api_users.where(mentor: true) if params[:mentor] == 'true' @api_users = @api_users.where(mentee: true) if params[:mentee] == 'true' + # @api_user = @api_user.wishes.where(available_online: true) if params[available_online] == 'true' end # Post /api_users diff --git a/server/app/controllers/api/v1/wishes_controller.rb b/server/app/controllers/api/v1/wishes_controller.rb index ac4ccfc3..4a964f11 100644 --- a/server/app/controllers/api/v1/wishes_controller.rb +++ b/server/app/controllers/api/v1/wishes_controller.rb @@ -2,10 +2,12 @@ class Api::V1::WishesController < ApplicationController before_action :set_api_user before_action :set_api_user_wish, only: [:show, :update, :destroy] - #GET /api_users/:api_user_id/wishes def index - json_response(@api_user.wishes) + @wishes = @api_user.wishes.includes(:programminglanguage).includes(:meetinginterval) + @wishes = @wishes.where(available_offline: true) if params[:available_offline] == 'true' + @wishes = @wishes.where(available_online: true) if params[:available_online] == 'true' + #Explanation regarding includes: not necessary to link programming languages and meeting interval to Wishes, but it means there is only one call to the database during which it pulls all the information linked by foreign keys in case it needs it in the future. end # Post /api_users/:api_user_id/wishes @@ -33,7 +35,7 @@ def destroy private def wish_params - params.permit(:available_offline, :available_online, :goal, :api_user_id, :id, :programminglanguage_id, :meetinginterval_id) + params.permit(:available_offline, :available_online, :goal, :api_user_id, :id, :programminglanguage_id, :meetinginterval_id)#, :programminglanguage, :meetinginterval) end def set_api_user @@ -44,7 +46,4 @@ def set_api_user_wish @wish = @api_user.wishes.find_by!(id: params[:id]) if @api_user end - # def set_programminglanguage - # @programminglanguage = Programminglanguage.find(params[:programminglanguage_id]) - # end end diff --git a/server/app/models/meetinginterval.rb b/server/app/models/meetinginterval.rb index 389839d5..ee019be5 100644 --- a/server/app/models/meetinginterval.rb +++ b/server/app/models/meetinginterval.rb @@ -1,4 +1,4 @@ class Meetinginterval < ApplicationRecord + has_many :wishes validates_presence_of :interval - end diff --git a/server/app/models/wish.rb b/server/app/models/wish.rb index ae0e4b73..c27fac75 100644 --- a/server/app/models/wish.rb +++ b/server/app/models/wish.rb @@ -1,8 +1,9 @@ class Wish < ApplicationRecord belongs_to :api_user belongs_to :programminglanguage - # has_one :programminglanguage, class_name: 'Programminglanguage', foreign_key: 'programminglanguage_id' delegate :language, :to => :programminglanguage + belongs_to :meetinginterval + delegate :interval, :to => :meetinginterval # validation validates_presence_of :goal validate :online_or_offline diff --git a/server/app/views/api/v1/api_users/_api_user.json.jbuilder b/server/app/views/api/v1/api_users/_api_user.json.jbuilder index da016a7c..fa1e4bb4 100644 --- a/server/app/views/api/v1/api_users/_api_user.json.jbuilder +++ b/server/app/views/api/v1/api_users/_api_user.json.jbuilder @@ -3,5 +3,6 @@ json.call(api_user, :id, :mentor, :mentee) json.wishes api_user.wishes do |wish| - json.call(wish, :id, :goal, :available_online, :available_offline) + json.call(wish, :id, :goal, :available_online, :available_offline, :language, :interval) end + diff --git a/server/app/views/api/v1/wishes/.#index.json.jbuilder b/server/app/views/api/v1/wishes/.#index.json.jbuilder new file mode 120000 index 00000000..a60fdff6 --- /dev/null +++ b/server/app/views/api/v1/wishes/.#index.json.jbuilder @@ -0,0 +1 @@ +siobhanmcloughlin@Siobhans-MBP.h.drongo.info.672 \ No newline at end of file diff --git a/server/app/views/api/v1/wishes/_wish.json.jbuilder b/server/app/views/api/v1/wishes/_wish.json.jbuilder new file mode 100644 index 00000000..133b2404 --- /dev/null +++ b/server/app/views/api/v1/wishes/_wish.json.jbuilder @@ -0,0 +1,5 @@ + + +json.call(wish, :id, :goal, :available_online, :available_offline, :language, :interval) + + diff --git a/server/app/views/api/v1/wishes/index.json.jbuilder b/server/app/views/api/v1/wishes/index.json.jbuilder new file mode 100644 index 00000000..82a17ffb --- /dev/null +++ b/server/app/views/api/v1/wishes/index.json.jbuilder @@ -0,0 +1,3 @@ +json.array! @wishes do |wish| + json.partial! 'wish', wish: wish +end From aa11e5007b377855c955d4112c3c87d4ee3abe19 Mon Sep 17 00:00:00 2001 From: sibsmc Date: Sat, 23 Nov 2019 20:40:36 +0100 Subject: [PATCH 3/8] removed unneeded comments --- server/app/controllers/api/v1/api_users_controller.rb | 1 - server/app/controllers/api/v1/wishes_controller.rb | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/server/app/controllers/api/v1/api_users_controller.rb b/server/app/controllers/api/v1/api_users_controller.rb index 49dd8190..0817d397 100644 --- a/server/app/controllers/api/v1/api_users_controller.rb +++ b/server/app/controllers/api/v1/api_users_controller.rb @@ -8,7 +8,6 @@ def index #Explanation regarding includes: not necessary to link programming languages and meeting interval to ApiUser, but it means there is only one call to the database during which it pulls all the information linked by foreign keys in case it needs it in the future. @api_users = @api_users.where(mentor: true) if params[:mentor] == 'true' @api_users = @api_users.where(mentee: true) if params[:mentee] == 'true' - # @api_user = @api_user.wishes.where(available_online: true) if params[available_online] == 'true' end # Post /api_users diff --git a/server/app/controllers/api/v1/wishes_controller.rb b/server/app/controllers/api/v1/wishes_controller.rb index 4a964f11..436a5fa0 100644 --- a/server/app/controllers/api/v1/wishes_controller.rb +++ b/server/app/controllers/api/v1/wishes_controller.rb @@ -35,7 +35,7 @@ def destroy private def wish_params - params.permit(:available_offline, :available_online, :goal, :api_user_id, :id, :programminglanguage_id, :meetinginterval_id)#, :programminglanguage, :meetinginterval) + params.permit(:available_offline, :available_online, :goal, :api_user_id, :id, :programminglanguage_id, :meetinginterval_id) end def set_api_user From 9f957a0dfa2f7bb4d4fd85fb136e429b78f0a6b0 Mon Sep 17 00:00:00 2001 From: sibsmc Date: Sun, 24 Nov 2019 20:15:09 +0100 Subject: [PATCH 4/8] made api_user email a unique attribute, updated tests --- .../api/v1/api_users_controller.rb | 4 +- server/app/models/api_user.rb | 1 + .../views/api/v1/wishes/.#index.json.jbuilder | 1 - .../views/api/v1/wishes/index.json.jbuilder | 2 + server/spec/requests/api/v1/api_users_spec.rb | 42 ++++++++++++++++++- server/spec/requests/api/v1/wishes_spec.rb | 17 ++++---- 6 files changed, 56 insertions(+), 11 deletions(-) delete mode 120000 server/app/views/api/v1/wishes/.#index.json.jbuilder diff --git a/server/app/controllers/api/v1/api_users_controller.rb b/server/app/controllers/api/v1/api_users_controller.rb index 0817d397..7cd0a391 100644 --- a/server/app/controllers/api/v1/api_users_controller.rb +++ b/server/app/controllers/api/v1/api_users_controller.rb @@ -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 diff --git a/server/app/models/api_user.rb b/server/app/models/api_user.rb index 78e45568..d681151a 100644 --- a/server/app/models/api_user.rb +++ b/server/app/models/api_user.rb @@ -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 end diff --git a/server/app/views/api/v1/wishes/.#index.json.jbuilder b/server/app/views/api/v1/wishes/.#index.json.jbuilder deleted file mode 120000 index a60fdff6..00000000 --- a/server/app/views/api/v1/wishes/.#index.json.jbuilder +++ /dev/null @@ -1 +0,0 @@ -siobhanmcloughlin@Siobhans-MBP.h.drongo.info.672 \ No newline at end of file diff --git a/server/app/views/api/v1/wishes/index.json.jbuilder b/server/app/views/api/v1/wishes/index.json.jbuilder index 82a17ffb..6b9396f2 100644 --- a/server/app/views/api/v1/wishes/index.json.jbuilder +++ b/server/app/views/api/v1/wishes/index.json.jbuilder @@ -1,3 +1,5 @@ + + json.array! @wishes do |wish| json.partial! 'wish', wish: wish end diff --git a/server/spec/requests/api/v1/api_users_spec.rb b/server/spec/requests/api/v1/api_users_spec.rb index 057708d2..1e5e20c6 100644 --- a/server/spec/requests/api/v1/api_users_spec.rb +++ b/server/spec/requests/api/v1/api_users_spec.rb @@ -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 @@ -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': 'Test_email@email.com', '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@email.com' } }.to_json } before { post '/api/v1/api_users', params: invalid_attributes } @@ -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': 'Test_email@email.com'}.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 diff --git a/server/spec/requests/api/v1/wishes_spec.rb b/server/spec/requests/api/v1/wishes_spec.rb index 7432c884..ee012ba8 100644 --- a/server/spec/requests/api/v1/wishes_spec.rb +++ b/server/spec/requests/api/v1/wishes_spec.rb @@ -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} @@ -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 } @@ -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 @@ -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 @@ -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) From 916cab1532d49a949c85314a0932f11245f84338 Mon Sep 17 00:00:00 2001 From: sibsmc Date: Sun, 24 Nov 2019 20:53:38 +0100 Subject: [PATCH 5/8] added username field to api_user and made it unique, same with email --- .../api/v1/api_users_controller.rb | 4 +- server/app/models/api_user.rb | 3 +- .../20190623093635_create_api_users.rb | 1 + server/db/schema.rb | 1 + server/db/seeds.rb | 6 +-- server/spec/factories/api_users.rb | 1 + server/spec/requests/api/v1/api_users_spec.rb | 42 ++++++++++++++++--- 7 files changed, 47 insertions(+), 11 deletions(-) diff --git a/server/app/controllers/api/v1/api_users_controller.rb b/server/app/controllers/api/v1/api_users_controller.rb index 7cd0a391..7baf14d2 100644 --- a/server/app/controllers/api/v1/api_users_controller.rb +++ b/server/app/controllers/api/v1/api_users_controller.rb @@ -18,7 +18,7 @@ def create # GET /api_users/:id def show - json_response(@api_user), + json_response(@api_user) end # PUT /api_users/:id @@ -37,7 +37,7 @@ def destroy def api_user_params - params.require(:api_user).permit(:first_name, :last_name, :city, :email, :password_digest, :mentor, :mentee) + params.require(:api_user).permit(:first_name, :last_name, :city, :email, :password_digest, :username, :mentor, :mentee) end def set_api_user diff --git a/server/app/models/api_user.rb b/server/app/models/api_user.rb index d681151a..c2cb4f18 100644 --- a/server/app/models/api_user.rb +++ b/server/app/models/api_user.rb @@ -6,7 +6,8 @@ class ApiUser < ApplicationRecord # Can write model association here for example has_many :wishes, dependent: :destroy # validation - validates_presence_of :email, :password_digest + validates_presence_of :email, :password_digest, :username validates :email, uniqueness: true, on: :create and :update + validates :username, uniqueness: true, on: :create and :update end diff --git a/server/db/migrate/20190623093635_create_api_users.rb b/server/db/migrate/20190623093635_create_api_users.rb index ae065c6d..8985b56b 100644 --- a/server/db/migrate/20190623093635_create_api_users.rb +++ b/server/db/migrate/20190623093635_create_api_users.rb @@ -4,6 +4,7 @@ def change t.string :first_name t.string :last_name + t.string :username t.string :city t.string :email t.string :password_digest diff --git a/server/db/schema.rb b/server/db/schema.rb index 67e7b5c9..e4263069 100644 --- a/server/db/schema.rb +++ b/server/db/schema.rb @@ -15,6 +15,7 @@ create_table "api_users", force: :cascade do |t| t.string "first_name" t.string "last_name" + t.string "username" t.string "city" t.string "email" t.string "password_digest" diff --git a/server/db/seeds.rb b/server/db/seeds.rb index 2e3f4315..006496dc 100644 --- a/server/db/seeds.rb +++ b/server/db/seeds.rb @@ -3,9 +3,9 @@ Programminglanguage.destroy_all Wish.destroy_all users = ApiUser.create([ - {first_name: 'Mary', last_name: 'Black', city: 'Dublin', mentor: true, mentee: false, email: 'Test_email_1@example.com', password_digest: 'password1'}, - {first_name: 'Bob', last_name: 'Dylan', city: 'New York', mentor: true, mentee: true, email: 'Test_email_2@example.com', password_digest: 'password2'}, - {first_name: 'Jane', last_name: 'Smith', city: 'Edinburgh', mentor: false, mentee: true, email: 'Test_email3@example.com', password_digest: 'password3'} + {first_name: 'Mary', last_name: 'Black', city: 'Dublin', mentor: true, mentee: false, email: 'Test_email_1@example.com', password_digest: 'password1', username: 'user1'}, + {first_name: 'Bob', last_name: 'Dylan', city: 'New York', mentor: true, mentee: true, email: 'Test_email_2@example.com', password_digest: 'password2', username: 'user2'}, + {first_name: 'Jane', last_name: 'Smith', city: 'Edinburgh', mentor: false, mentee: true, email: 'Test_email3@example.com', password_digest: 'password3', username: 'user3' } ]) Meetinginterval.create([{interval: 'weekly'}, {interval: 'daily'}]) Programminglanguage.create([{language: 'python'},{language: 'SCALA'}]) diff --git a/server/spec/factories/api_users.rb b/server/spec/factories/api_users.rb index 3615d3b1..deaa1b57 100644 --- a/server/spec/factories/api_users.rb +++ b/server/spec/factories/api_users.rb @@ -4,6 +4,7 @@ password_digest {Faker::Lorem.word} #Misc doesn't seem to work {Faker::Misc.password} first_name {Faker::Name.first_name} last_name {Faker::Name.last_name} + username {Faker::Name.name} city {Faker::Address.city} mentor {Faker::Boolean.boolean(true_ratio: 0.5)} mentee {Faker::Boolean.boolean(true_ratio: 0.5)} diff --git a/server/spec/requests/api/v1/api_users_spec.rb b/server/spec/requests/api/v1/api_users_spec.rb index 1e5e20c6..ba4be7f4 100644 --- a/server/spec/requests/api/v1/api_users_spec.rb +++ b/server/spec/requests/api/v1/api_users_spec.rb @@ -21,7 +21,7 @@ end end - # Test suite for GET /api/v1/api_userss/:id + # Test suite for GET /api/v1/api_users/:id describe 'GET /api/v1/api_users/:id' do before { get "/api/v1/api_users/#{api_user_id}/" } @@ -54,7 +54,7 @@ describe 'POST /api/v1/api_users' do let(:valid_attributes) do # send json payload - { 'email': 'Test_email@email.com', 'password_digest': 'password1'}.to_json + { 'email': 'Test_email@email.com', 'password_digest': 'password1', 'username': 'user11'}.to_json context 'when request is valid' do before { post '/api/v1/api_users', params: valid_attributes} @@ -64,7 +64,7 @@ end it 'returns same params as entered' do - expect(json['email'], json['password_digest']).to eq('Test_email@email.com','password1') + expect(json['email'], json['password_digest']).to eq('Test_email@email.com','password1', 'user11') end end end @@ -87,7 +87,7 @@ context 'when the request is invalid as email already in use' do let(:invalid_email) do - { 'email': 'Test_email@email.com', 'password_digest': 'password2' } .to_json + { 'email': 'Test_email@email.com', 'password_digest': 'password2', 'username': 'user12' } .to_json before { post '/api/v1/api_users', params: invalid_email } it 'returns status code 422' do @@ -99,7 +99,23 @@ .to match(/Validation failed: Email has already been taken/) end end - end + end + + context 'when the request is invalid as username already in use' do + let(:invalid_username) do + { 'email': 'Test_email_1@email.com', 'password_digest': 'password2', 'username': 'user11' } .to_json + before { post '/api/v1/api_users', params: invalid_username } + + 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: Username 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@email.com' } }.to_json } @@ -160,6 +176,22 @@ end end + context 'when username already in use' do + let(:api_user_id) {api_users.first.id} + let(:invalid_username_update) do + # send json payload + { 'username': 'user11'}.to_json + before { patch "/api/v1/api_users/#{api_user_id}/", params: invalid_username_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: Username has already been taken/) + end + end + end From cefe16f4a58af345678af982602fe3d44b8c84c9 Mon Sep 17 00:00:00 2001 From: sibsmc Date: Sun, 24 Nov 2019 21:38:46 +0100 Subject: [PATCH 6/8] edited api_user model --- server/app/models/api_user.rb | 2 +- server/spec/requests/api/v1/api_users_spec.rb | 3 +-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/server/app/models/api_user.rb b/server/app/models/api_user.rb index c2cb4f18..6481b47d 100644 --- a/server/app/models/api_user.rb +++ b/server/app/models/api_user.rb @@ -8,6 +8,6 @@ class ApiUser < ApplicationRecord # validation validates_presence_of :email, :password_digest, :username validates :email, uniqueness: true, on: :create and :update - validates :username, uniqueness: true, on: :create and :update + validates :username, uniqueness: true, on: [:create, :update] end diff --git a/server/spec/requests/api/v1/api_users_spec.rb b/server/spec/requests/api/v1/api_users_spec.rb index ba4be7f4..f9735e8b 100644 --- a/server/spec/requests/api/v1/api_users_spec.rb +++ b/server/spec/requests/api/v1/api_users_spec.rb @@ -87,7 +87,7 @@ context 'when the request is invalid as email already in use' do let(:invalid_email) do - { 'email': 'Test_email@email.com', 'password_digest': 'password2', 'username': 'user12' } .to_json + { 'email': 'Test_email@email.com', 'password_digest': 'password2', 'username': 'user12' } .to_json before { post '/api/v1/api_users', params: invalid_email } it 'returns status code 422' do @@ -154,7 +154,6 @@ it 'returns same params as entered' do expect(json['first_name'],json['last_name'],json['city'], json['email'], json['password_digest'],json['mentor'],json['mentee']).to eq('Bobby', 'Dylan','Mexico', 'Test_email@email.com','password1', True, False) end - end From bb218915c47f2578d3bf9914c70f3b903c5c587a Mon Sep 17 00:00:00 2001 From: sibsmc Date: Tue, 31 Dec 2019 20:52:27 +0100 Subject: [PATCH 7/8] requested changes made --- server/Gemfile | 3 + server/Gemfile.lock | 16 ++ server/app/models/api_user.rb | 4 +- server/spec/factories/api_users.rb | 4 +- server/spec/requests/api/v1/api_users_spec.rb | 205 ++++++++---------- .../requests/api/v1/meetingintervals_spec.rb | 140 ++++++------ .../api/v1/programminglanguages_spec.rb | 103 ++++----- server/spec/requests/api/v1/wishes_spec.rb | 5 +- 8 files changed, 226 insertions(+), 254 deletions(-) diff --git a/server/Gemfile b/server/Gemfile index 4b3807f8..096ceefa 100644 --- a/server/Gemfile +++ b/server/Gemfile @@ -42,6 +42,9 @@ group :development, :test do # Call 'byebug' anywhere in the code to stop execution and get a debugger console gem 'byebug', platforms: [:mri, :mingw, :x64_mingw] # Adds support for Capybara system testing and selenium driver + + gem 'pry-rescue' + gem 'pry-stack_explorer' gem 'capybara', '~> 3.29' gem 'selenium-webdriver' gem 'rspec-rails', '~> 3.9' diff --git a/server/Gemfile.lock b/server/Gemfile.lock index 84e24ccf..cefae7b5 100644 --- a/server/Gemfile.lock +++ b/server/Gemfile.lock @@ -60,6 +60,8 @@ GEM public_suffix (>= 2.0.2, < 5.0) bcrypt (3.1.13) bindex (0.8.1) + binding_of_caller (0.8.0) + debug_inspector (>= 0.0.1) bootsnap (1.4.5) msgpack (~> 1.0) builder (3.2.3) @@ -73,6 +75,7 @@ GEM regexp_parser (~> 1.5) xpath (~> 3.2) childprocess (3.0.0) + coderay (1.1.2) coffee-rails (5.0.0) coffee-script (>= 2.2.0) railties (>= 5.2.0) @@ -83,6 +86,7 @@ GEM concurrent-ruby (1.1.5) crass (1.0.5) database_cleaner (1.7.0) + debug_inspector (0.0.3) diff-lcs (1.3) erubi (1.9.0) execjs (2.7.0) @@ -98,6 +102,7 @@ GEM activesupport (>= 4.2.0) i18n (1.7.0) concurrent-ruby (~> 1.0) + interception (0.5) jbuilder (2.9.1) activesupport (>= 4.2.0) jwt (2.2.1) @@ -121,6 +126,15 @@ GEM nokogiri (1.10.4) mini_portile2 (~> 2.4.0) pg (1.1.4) + pry (0.12.2) + coderay (~> 1.1.0) + method_source (~> 0.9.0) + pry-rescue (1.5.0) + interception (>= 0.5) + pry (>= 0.12.0) + pry-stack_explorer (0.4.9.3) + binding_of_caller (>= 0.7) + pry (>= 0.9.11) public_suffix (4.0.1) puma (4.2.1) nio4r (~> 2.0) @@ -241,6 +255,8 @@ DEPENDENCIES jwt listen (>= 3.0.5, < 3.3) pg + pry-rescue + pry-stack_explorer puma (~> 4.2) rails (~> 6.0) rspec-rails (~> 3.9) diff --git a/server/app/models/api_user.rb b/server/app/models/api_user.rb index 6481b47d..ba888510 100644 --- a/server/app/models/api_user.rb +++ b/server/app/models/api_user.rb @@ -6,8 +6,8 @@ class ApiUser < ApplicationRecord # Can write model association here for example has_many :wishes, dependent: :destroy # validation - validates_presence_of :email, :password_digest, :username - validates :email, uniqueness: true, on: :create and :update + validates_presence_of :email, :password_digest, :username, :mentor, :mentee + validates :email, uniqueness: true, on: [:create, :update] validates :username, uniqueness: true, on: [:create, :update] end diff --git a/server/spec/factories/api_users.rb b/server/spec/factories/api_users.rb index deaa1b57..13f09e32 100644 --- a/server/spec/factories/api_users.rb +++ b/server/spec/factories/api_users.rb @@ -6,7 +6,7 @@ last_name {Faker::Name.last_name} username {Faker::Name.name} city {Faker::Address.city} - mentor {Faker::Boolean.boolean(true_ratio: 0.5)} - mentee {Faker::Boolean.boolean(true_ratio: 0.5)} + mentor {Faker::Boolean.boolean(true_ratio: 1)} + mentee {Faker::Boolean.boolean(true_ratio: 1)} end end diff --git a/server/spec/requests/api/v1/api_users_spec.rb b/server/spec/requests/api/v1/api_users_spec.rb index f9735e8b..0acfe838 100644 --- a/server/spec/requests/api/v1/api_users_spec.rb +++ b/server/spec/requests/api/v1/api_users_spec.rb @@ -4,8 +4,9 @@ # initialize test data - let!(:api_users){create_list(:api_user, 10)} - let(:api_user_id) {api_users.first.id} + let!(:api_users){create_list(:api_user, 2)} + let!(:api_user_id) {api_users.first.id} + # Test suite for GET /api/v1/api_users describe 'GET /api/v1/api_users' do @@ -13,7 +14,7 @@ it 'returns api_users' do expect(json).not_to be_empty - expect(json.size).to eq(10) + expect(json.size).to eq(2) end it 'returns status code 200' do @@ -47,33 +48,28 @@ expect(json['message']).to match(/Couldn't find ApiUser with 'id'=#{api_user_id}/) end end - end # Test suite POST /api/v1/api_user describe 'POST /api/v1/api_users' do let(:valid_attributes) do - # send json payload - { 'email': 'Test_email@email.com', 'password_digest': 'password1', 'username': 'user11'}.to_json - - context 'when request is valid' do - before { post '/api/v1/api_users', params: valid_attributes} - - it 'returns status code 201' do - expect(response).to have_http_status(201) - end + { email: 'Test_email@email.com', password_digest: 'password1', username: 'user11', mentor: true, mentee: true} + end + before { post '/api/v1/api_users', params: valid_attributes, as: :json} + context 'when request is valid' do + it 'returns status code 201' do + expect(response).to have_http_status(201) + end - it 'returns same params as entered' do - expect(json['email'], json['password_digest']).to eq('Test_email@email.com','password1', 'user11') - end + it 'returns same params as entered' do + expect([json["email"], json["password_digest"], json["username"]]).to eq(["Test_email@email.com", "password1", "user11"]) end end - context 'when the request is invalid as no params' do - let(:invalid_attributes) do - { email: nil } .to_json - before { post '/api/v1/api_users', params: invalid_attributes } + context 'when the request is invalid as no params' do + invalid_attributes = { } + before { post '/api/v1/api_users', params: invalid_attributes, as: :json } it 'returns status code 422' do expect(response).to have_http_status(422) end @@ -82,134 +78,113 @@ 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': 'Test_email@email.com', 'password_digest': 'password2', 'username': 'user12' } .to_json - before { post '/api/v1/api_users', params: invalid_email } + let(:invalid_email) do + { 'email': 'Test_email@email.com', 'password_digest': 'password2', 'username': 'user12', mentor: true, mentee: true } + end + before { post '/api/v1/api_users', params: invalid_email, as: :json } it 'returns status code 422' do - expect(response).to have_http_status(422) + 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 username already in use' do - let(:invalid_username) do - { 'email': 'Test_email_1@email.com', 'password_digest': 'password2', 'username': 'user11' } .to_json - before { post '/api/v1/api_users', params: invalid_username } + let(:invalid_username) do + { 'email': 'Test_email_1@email.com', 'password_digest': 'password2', 'username': 'user11' , mentor: true, mentee: true} + end + before { post '/api/v1/api_users', params: invalid_username, as: :json } - it 'returns status code 422' do - expect(response).to have_http_status(422) - end + 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: Username has already been taken/) - end + it 'returns a validation failure message' do + expect(json['message']) + .to match(/Validation failed: Username has already been taken/) end end context 'when the request is invalid as only some requird params' do - let(:invalid_attributes) { { 'api_user': { 'email': 'email@email.com' } }.to_json } - before { post '/api/v1/api_users', params: invalid_attributes } + let(:some_invalid_attributes) {{ 'email': 'email@email.com' }} + before { post '/api/v1/api_users', params: some_invalid_attributes, as: :json } 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(/param is missing or the value is empty: api_user/) + .to match(/Validation failed: Password digest can't be blank, Username can't be blank, Mentor can't be blank, Mentee can't be blank/) end end - end # Test suite for Patch /api/v1/api_userss/:id describe 'PATCH /api/v1/api_users/:id' do - let(:valid_attributes) do - # send json payload - { 'first_name': 'Bobby','last_name': 'Dylan', 'city': 'Mexico', 'email': 'Test_email@email.com', 'password_digest': 'password1', 'mentor': True, 'mentee': False}.to_json - let(:api_user_id) {api_users.first.id} - - context 'when request is valid' do - before { patch "/api/v1/api_users/#{api_user_id}/", params: valid_attributes} + let!(:api_user_id) {api_users.first.id} + let(:valid_attributes) do + { first_name: 'Bobby',last_name: 'Dylan', city: 'Mexico', email: 'Test_email13@email.com', username: 'user13', password_digest: 'password1' } + end - it 'returns status code 204' do - expect(response).to have_http_status(204) - end - end - - context 'check that parameters have updated correctly' do - before { get "/api/v1/api_users/#{api_user_id}/", params: valid_attributes} - end - - it 'returns same params as entered' do - expect(json['first_name'],json['last_name'],json['city'], json['email'], json['password_digest'],json['mentor'],json['mentee']).to eq('Bobby', 'Dylan','Mexico', 'Test_email@email.com','password1', True, False) - end - 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': 'Test_email@email.com'}.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 username already in use' do - let(:api_user_id) {api_users.first.id} - let(:invalid_username_update) do - # send json payload - { 'username': 'user11'}.to_json - before { patch "/api/v1/api_users/#{api_user_id}/", params: invalid_username_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: Username has already been taken/) - end - end - end - - - - context 'when api_user does not exist' do - let(:api_user_id) {0} - let(:valid_attributes) do - # send json payload - { 'email': 'Test_email@email.com', 'password_digest': 'password1'}.to_json - before { patch "/api/v1/api_users/#{api_user_id}/", params: valid_attributes} - - it 'returns status code 404' do - expect(response).to have_http_status(404) - end - - it 'returns message informing no user with that id' do - expect(json['message']).to match(/Couldn't find ApiUser with 'id'=#{api_user_id}/) - end - end - end + + before { patch "/api/v1/api_users/#{api_user_id}/", params: valid_attributes, as: :json} + it 'returns status code 204' do + expect(response).to have_http_status(204) + end + + context 'check update worked' do + before { get "/api/v1/api_users/#{api_user_id}/"} + it 'returns same params as entered' do + + expect([json["first_name"], json["last_name"], json["city"], json["email"], json["password_digest"], json["username"]]).to eq(['Bobby', 'Dylan','Mexico', 'Test_email13@email.com','password1','user13']) + end + end + + # context 'when email already in use' do + # let(:api_users.first + + context 'invalid email' do + before { patch "/api/v1/api_users/#{api_user_id}/", params: {email: api_users.second.email} , as: :json} + + 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 + + context 'when username already in use' do + before { patch "/api/v1/api_users/#{api_user_id}/", params: {username: api_users.second.username}, as: :json} + + 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: Username has already been taken/) + end + end + + context 'when api_user does not exist' do + let(:api_user_id) {0} + before { patch "/api/v1/api_users/#{api_user_id}/", params: valid_attributes, as: :json} + it 'returns status code 404' do + expect(response).to have_http_status(404) + end + + it 'returns message informing no user with that id' do + expect(json['message']).to match(/Couldn't find ApiUser with 'id'=#{api_user_id}/) + end + end end diff --git a/server/spec/requests/api/v1/meetingintervals_spec.rb b/server/spec/requests/api/v1/meetingintervals_spec.rb index 3ea80421..8c930ece 100644 --- a/server/spec/requests/api/v1/meetingintervals_spec.rb +++ b/server/spec/requests/api/v1/meetingintervals_spec.rb @@ -50,101 +50,66 @@ end - # Test suite POST /api/v1/api_user + # Test suite POST /api/v1/api_user describe 'POST /api/v1/meetingintervals' do - let(:valid_attributes) do - # send json payload - { 'interval': 'monthlynonsense'}.to_json - - context 'when request is valid' do - before { post '/api/v1/meetingintervals', params: valid_attributes} - - it 'returns status code 201' do - expect(response).to have_http_status(201) - end + let(:valid_attributes) { {interval: 'monthlynonsense'}} + + context 'when request is valid' do + before { post '/api/v1/meetingintervals', params: valid_attributes, as: :json} + it 'returns status code 201' do + expect(response).to have_http_status(201) + end - it 'returns same params as entered' do - expect(json['interval']).to eq('monthlynonsense') - end + it 'returns same params as entered' do + expect(json['interval']).to eq('monthlynonsense') end end + end - context 'when the request is invalid as no params' do - let(:invalid_attributes) { { meetinginterval: { interval: nil } }.to_json } - before { post '/api/v1/meetingintervals', params: invalid_attributes } + context 'when the request is invalid as no params' do + let(:invalid_attributes) { { meetinginterval: { interval: nil } }.to_json } + before { post '/api/v1/meetingintervals', params: invalid_attributes } - it 'returns status code 422' do - expect(response).to have_http_status(422) - end + 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: Interval can't be blank/) - end + it 'returns a validation failure message' do + expect(json['message']) + .to match(/Validation failed: Interval can't be blank/) end end - # Test suite for Patch /api/v1/meetingintervals/:id describe 'PATCH /api/v1/meetingintervals/:id' do - let(:valid_attributes) do - # send json payload - { 'interval': 'firstweek'}.to_json - let(:meetinginterval_id) {meetingintervals.first.id} - - context 'when request is valid' do - before { patch "/api/v1/meetingintervals/#{meetinginterval_id}/", params: valid_attributes} - - it 'returns status code 204' do - expect(response).to have_http_status(204) - end - end - - context 'check that parameters have updated correctly' do - before { get "/api/v1/meetingintervals/#{meetinginterval_id}/", params: valid_attributes} - end - - it 'returns same params as entered' do - expect(json['interval']).to eq('firstweek') - end - - end - - context 'when meeting interval does not exist' do - let(:meetinginterval_id) {0} - let(:valid_attributes) do - # send json payload - { 'interval': 'secondweek'}.to_json - before { patch "/api/v1/meetingintervals/#{meetinginterval_id}/", params: valid_attributes} - - it 'returns status code 404' do - expect(response).to have_http_status(404) - end - - it 'returns message informing no user with that id' do - expect(json['message']).to match(/Couldn't find Meetinginterval with 'id'=#{meetinginterval_id}/) - end - end - end - end + let(:valid_attributes) do + { 'interval': 'firstweek'} + end + let(:meetinginterval_id) {meetingintervals.first.id} + before { patch "/api/v1/meetingintervals/#{meetinginterval_id}/", params: valid_attributes, as: :json} + it 'returns status code 204' do + expect(response).to have_http_status(204) + end - # Test suite for Delete /api/v1/meetingintervals/:id - describe 'DELETE /api/v1/meetingintervals/:id' do + context 'check that parameters have updated correctly' do + before { get "/api/v1/meetingintervals/#{meetinginterval_id}/", params: valid_attributes, as: :json} - context 'when request made to delete an interval' do - let(:meetinginterval_id) {meetingintervals.first.id} - before { delete "/api/v1/meetingintervals/#{meetinginterval_id}/" } - it 'returns status code 204' do - expect(response).to have_http_status(204) + it 'returns same params as entered' do + expect(json["interval"]).to eq('firstweek') end end - context 'when meetinginterval does not exist' do + context 'when meeting interval does not exist' do let(:meetinginterval_id) {0} - before { delete "/api/v1/meetingintervals/#{meetinginterval_id}/" } - + let(:valid_attributes) do + # send json payload + { 'interval': 'secondweek'}.to_json + end + + before { patch "/api/v1/meetingintervals/#{meetinginterval_id}/", params: valid_attributes} + it 'returns status code 404' do expect(response).to have_http_status(404) end @@ -156,7 +121,30 @@ end + # Test suite for Delete /api/v1/meetingintervals/:id + describe 'DELETE /api/v1/meetingintervals/:id' do + context 'when request made to delete an interval' do + let(:meetinginterval_id) {meetingintervals.first.id} + before { delete "/api/v1/meetingintervals/#{meetinginterval_id}/" } + it 'returns status code 204' do + expect(response).to have_http_status(204) + end + end -end + context 'when meetinginterval does not exist' do + let(:meetinginterval_id) {0} + before { delete "/api/v1/meetingintervals/#{meetinginterval_id}/" } + it 'returns status code 404' do + expect(response).to have_http_status(404) + end + + it 'returns message informing no user with that id' do + expect(json['message']).to match(/Couldn't find Meetinginterval with 'id'=#{meetinginterval_id}/) + end + end + end + + +end diff --git a/server/spec/requests/api/v1/programminglanguages_spec.rb b/server/spec/requests/api/v1/programminglanguages_spec.rb index 53d50535..5478fd85 100644 --- a/server/spec/requests/api/v1/programminglanguages_spec.rb +++ b/server/spec/requests/api/v1/programminglanguages_spec.rb @@ -52,80 +52,71 @@ # Test suite POST /api/v1/api_user describe 'POST /api/v1/programminglanguages' do - let(:valid_attributes) do - # send json payload - { 'language': 'javascript'}.to_json - - context 'when request is valid' do - before { post '/api/v1/programminglanguages', params: valid_attributes} + let(:valid_attributes) { { 'language': 'javascript' } } - it 'returns status code 201' do - expect(response).to have_http_status(201) - end + context 'when request is valid' do + before { post '/api/v1/programminglanguages', params: valid_attributes, as: :json} + it 'returns status code 201' do + expect(response).to have_http_status(201) + end - it 'returns same params as entered' do - expect(json['language']).to eq('javascript') - end + it 'returns same params as entered' do + expect(json['language']).to eq('javascript') end end + end - context 'when the request is invalid as no params' do - let(:invalid_attributes) { { programminglanguage: { language: nil } }.to_json } - before { post '/api/v1/programminglanguages', params: invalid_attributes } + context 'when the request is invalid as no params' do + let(:invalid_attributes) { { programminglanguage: { language: nil } }.to_json } + before { post '/api/v1/programminglanguages', params: invalid_attributes } - it 'returns status code 422' do - expect(response).to have_http_status(422) - end + 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: Language can't be blank/) - end + it 'returns a validation failure message' do + expect(json['message']) + .to match(/Validation failed: Language can't be blank/) end end - # Test suite for Patch /api/v1/programminglanguages/:id describe 'PATCH /api/v1/programminglanguages/:id' do - let(:valid_attributes) do - # send json payload - { 'language': 'ruby'}.to_json - let(:programminglanguage_id) {programminglanguages.first.id} + let(:valid_attributes) do + { 'language': 'ruby'} + end + let(:programminglanguage_id) {programminglanguages.first.id} - context 'when request is valid' do - before { patch "/api/v1/programminglanguages/#{programminglanguage_id}/", params: valid_attributes} + before { patch "/api/v1/programminglanguages/#{programminglanguage_id}/", params: valid_attributes, as: :json} - it 'returns status code 204' do - expect(response).to have_http_status(204) - end - end - - context 'check that parameters have updated correctly' do - before { get "/api/v1/programminglanguages/#{programminglanguage_id}/", params: valid_attributes} - end + it 'returns status code 204' do + expect(response).to have_http_status(204) + end - it 'returns same params as entered' do - expect(json['language']).to eq('ruby') - end + context 'check that parameters have updated correctly' do + before { get "/api/v1/programminglanguages/#{programminglanguage_id}/", params: valid_attributes, as: :json} - end + it 'returns same params as entered' do + expect(json["language"]).to eq('ruby') + end + end - context 'when programming language does not exist' do - let(:programminglanguage_id) {0} - let(:valid_attributes) do + context 'when programming language does not exist' do + let(:programminglanguage_id) {0} + let(:valid_attributes) do # send json payload - { 'language': 'ruby on rails'}.to_json - before { patch "/api/v1/programminglanguages/#{programminglanguage_id}/", params: valid_attributes} - - it 'returns status code 404' do - expect(response).to have_http_status(404) - end - - it 'returns message informing no user with that id' do - expect(json['message']).to match(/Couldn't find Programminglanguage with 'id'=#{programminglanguage_id}/) - end - end - end + { 'language': 'ruby on rails'}.to_json + end + before { patch "/api/v1/programminglanguages/#{programminglanguage_id}/", params: valid_attributes} + + it 'returns status code 404' do + expect(response).to have_http_status(404) + end + + it 'returns message informing no user with that id' do + expect(json['message']).to match(/Couldn't find Programminglanguage with 'id'=#{programminglanguage_id}/) + end + end end diff --git a/server/spec/requests/api/v1/wishes_spec.rb b/server/spec/requests/api/v1/wishes_spec.rb index ee012ba8..b9600213 100644 --- a/server/spec/requests/api/v1/wishes_spec.rb +++ b/server/spec/requests/api/v1/wishes_spec.rb @@ -151,7 +151,6 @@ # Test suite for DELETE api_users/:api_user_id/wishes/:id describe 'DELETE /wishes/:id' do context 'when request made to delete a wish' do - #let(:api_user_id) {api_users.first.id} before { delete "/api/v1/api_users/#{api_user_id}/wishes/#{id}" } it 'returns status code 204' do expect(response).to have_http_status(204) @@ -161,14 +160,14 @@ context 'when wish does not exist' do let(:id) {0} before { delete "/api/v1/api_users/#{api_user_id}/wishes/#{id}" } - it 'returns status code 404' do expect(response).to have_http_status(404) end # #Not included as the eror message does not seem to work # it 'returns message informing no wish with that id' do - # expect(json['message']).to match(/Couldn't find Wish with 'id'=#{id}/) + # #binding.pry + # expect(json["message"]).to match(/Couldn't find Wish with 'id'=#{id}/) # end end end From f4220a2f81dca0c75942efd98eaaf5e051271c77 Mon Sep 17 00:00:00 2001 From: Siobhan McLoughlin Date: Sun, 12 Jan 2020 12:19:39 +0100 Subject: [PATCH 8/8] remove unneeded comments from api_user spec --- server/spec/requests/api/v1/api_users_spec.rb | 2 -- 1 file changed, 2 deletions(-) diff --git a/server/spec/requests/api/v1/api_users_spec.rb b/server/spec/requests/api/v1/api_users_spec.rb index 0acfe838..a9809bae 100644 --- a/server/spec/requests/api/v1/api_users_spec.rb +++ b/server/spec/requests/api/v1/api_users_spec.rb @@ -147,8 +147,6 @@ end end - # context 'when email already in use' do - # let(:api_users.first context 'invalid email' do before { patch "/api/v1/api_users/#{api_user_id}/", params: {email: api_users.second.email} , as: :json}