From ac444818357a4f5fe3bd5c61eb1f6049624f383e Mon Sep 17 00:00:00 2001 From: Laura Filmeyer Date: Fri, 16 Aug 2024 12:30:15 -0400 Subject: [PATCH] Add role to surveyors index --- .../app/controllers/surveyors_controller.rb | 2 +- .../views/surveyors/_surveyor.json.jbuilder | 2 ++ backend/spec/requests/surveyors_spec.rb | 34 ++++++++++++++++--- 3 files changed, 33 insertions(+), 5 deletions(-) diff --git a/backend/app/controllers/surveyors_controller.rb b/backend/app/controllers/surveyors_controller.rb index 5df779d6..c77a56aa 100644 --- a/backend/app/controllers/surveyors_controller.rb +++ b/backend/app/controllers/surveyors_controller.rb @@ -6,7 +6,7 @@ class SurveyorsController < ApplicationController # GET /surveyors or /surveyors.json def index - @surveyors = policy_scope(Surveyor).where(search_params) + @surveyors = policy_scope(Surveyor).where(search_params).includes(:user) end # GET /surveyors/1 or /surveyors/1.json diff --git a/backend/app/views/surveyors/_surveyor.json.jbuilder b/backend/app/views/surveyors/_surveyor.json.jbuilder index 9cd2771b..ae1aa624 100644 --- a/backend/app/views/surveyors/_surveyor.json.jbuilder +++ b/backend/app/views/surveyors/_surveyor.json.jbuilder @@ -2,4 +2,6 @@ json.extract! surveyor, :id, :user_id, :assignment_ids, :firstname, :lastname, :email, :phone, :street_address, :city, :state, :zipcode, :geocode, :status + +json.role surveyor.user.role json.url surveyor_url(surveyor, format: :json) diff --git a/backend/spec/requests/surveyors_spec.rb b/backend/spec/requests/surveyors_spec.rb index 44199f4c..3dab0366 100644 --- a/backend/spec/requests/surveyors_spec.rb +++ b/backend/spec/requests/surveyors_spec.rb @@ -52,19 +52,45 @@ end describe 'GET /index' do + before do + @surveyor = Surveyor.create! valid_attributes + sign_in user + end + it 'renders a successful response' do - Surveyor.create! valid_attributes get surveyors_url, as: :json expect(response).to be_successful + + surveyors_json = JSON.parse(response.body) + expect(surveyors_json[0]['id']).to eq(@surveyor.id) end end describe 'GET /show' do - it 'renders a successful response' do + before do + @surveyor = Surveyor.create! valid_attributes sign_in user - surveyor = Surveyor.create! valid_attributes - get surveyor_url(surveyor), as: :json + end + + it 'renders a successful response' do + get surveyor_url(@surveyor), as: :json expect(response).to be_successful + + surveyor_json = JSON.parse(response.body) + expect(surveyor_json['id']).to eq(@surveyor.id) + expect(surveyor_json['user_id']).to eq(user.id) + expect(surveyor_json['assignment_ids']).to eq([]) + expect(surveyor_json['firstname']).to eq('John') + expect(surveyor_json['lastname']).to eq('Smith') + expect(surveyor_json['email']).to eq('johnsmith@example.com') + expect(surveyor_json['phone']).to eq('1234567890') + expect(surveyor_json['street_address']).to eq('123 First Street') + expect(surveyor_json['city']).to eq('Cambridge') + expect(surveyor_json['state']).to eq('MA') + expect(surveyor_json['zipcode']).to eq('01234') + expect(surveyor_json['status']).to eq('active') + expect(surveyor_json['role']).to eq('surveyor') + expect(surveyor_json['url']).to eq("http://www.example.com/surveyors/#{@surveyor.id}.json") end end