|
1 | 1 | RSpec.describe "Admin::UsersController", type: :request do
|
2 |
| - let(:organization) { create(:organization) } |
3 |
| - let(:user) { create(:user, organization: organization) } |
| 2 | + let(:organization) { create(:organization, name: "Org ABC") } |
| 3 | + let(:user) { create(:user, organization: organization, name: "User 123") } |
4 | 4 | let(:organization_admin) { create(:organization_admin, organization: organization) }
|
5 | 5 | let(:super_admin) { create(:super_admin, organization: organization) }
|
6 |
| - |
7 |
| - let(:default_params) do |
8 |
| - { organization_name: organization.id } |
9 |
| - end |
10 |
| - |
11 |
| - let(:org) { create(:organization, name: 'Org ABC') } |
12 |
| - let(:partner) { create(:partner, name: 'Partner XYZ', organization: org) } |
13 |
| - let(:user) { create(:user, organization: org, name: 'User 123') } |
| 6 | + let(:partner) { create(:partner, name: 'Partner XYZ', organization: organization) } |
14 | 7 |
|
15 | 8 | context "When logged in as a super admin" do
|
16 | 9 | before do
|
17 | 10 | sign_in(super_admin)
|
18 | 11 | AddRoleService.call(user_id: user.id, resource_type: Role::PARTNER, resource_id: partner.id)
|
19 | 12 | end
|
20 | 13 |
|
| 14 | + describe "GET #index" do |
| 15 | + it "renders index template and shows banks and partners correctly" do |
| 16 | + AddRoleService.call(user_id: user.id, resource_type: Role::ORG_ADMIN, resource_id: organization.id) |
| 17 | + get admin_users_path |
| 18 | + |
| 19 | + expect(response).to render_template(:index) |
| 20 | + |
| 21 | + page = Nokogiri::HTML(response.body) |
| 22 | + banks_and_partners = page.at_xpath("//*[contains(text(), \"#{user.email}\")]/../td[1]").text.strip |
| 23 | + expect(banks_and_partners).to eq("Org ABC, Partner XYZ") |
| 24 | + end |
| 25 | + end |
| 26 | + |
21 | 27 | describe "GET #edit" do
|
22 | 28 | it "renders edit template and shows roles" do
|
23 | 29 | get edit_admin_user_path(user)
|
|
31 | 37 | describe "PATCH #update" do
|
32 | 38 | context 'with no errors' do
|
33 | 39 | it "renders index template with a successful update flash message" do
|
34 |
| - patch admin_user_path(user), params: { user: default_params.merge(organization_id: user.organization.id, |
35 |
| - name: 'New User 123', email: '[email protected]') } |
| 40 | + patch admin_user_path(user), params: { user: { name: 'New User 123', email: '[email protected]' } } |
36 | 41 | expect(response).to redirect_to admin_users_path
|
37 | 42 | expect(flash[:notice]).to eq("New User 123 updated!")
|
38 | 43 | end
|
39 | 44 | end
|
40 | 45 |
|
41 | 46 | context 'with errors' do
|
42 |
| - it "redirects back with no organization_id flash message" do |
43 |
| - patch admin_user_path(user), params: { user: { name: 'New User 123' } } |
| 47 | + it "redirects back with flash message" do |
| 48 | + patch admin_user_path(user), params: { user: { name: 'New User 123', email: "invalid_email" } } |
44 | 49 | expect(response).to redirect_to(edit_admin_user_path)
|
45 |
| - expect(flash[:error]).to eq('Please select an organization for the user.') |
46 |
| - end |
47 |
| - |
48 |
| - it "redirects back with no role found flash message" do |
49 |
| - patch admin_user_path(user), params: { user: { name: 'New User 123', organization_id: -1 } } |
50 |
| - expect(response).to redirect_to(edit_admin_user_path) |
51 |
| - expect(flash[:error]).to eq('Error finding a role within the provided organization') |
| 50 | + expect(flash[:error]).to eq("Something didn't work quite right -- try again?") |
52 | 51 | end
|
53 | 52 | end
|
54 | 53 | end
|
|
59 | 58 | allow(AddRoleService).to receive(:call)
|
60 | 59 | post admin_user_add_role_path(user_id: user.id,
|
61 | 60 | resource_type: Role::ORG_ADMIN,
|
62 |
| - resource_id: org.id), |
| 61 | + resource_id: organization.id), |
63 | 62 | headers: { 'HTTP_REFERER' => '/back/url'}
|
64 | 63 | expect(AddRoleService).to have_received(:call).with(user_id: user.id.to_s,
|
65 | 64 | resource_type: Role::ORG_ADMIN.to_s,
|
66 |
| - resource_id: org.id.to_s) |
| 65 | + resource_id: organization.id.to_s) |
67 | 66 | expect(flash[:notice]).to eq('Role added!')
|
68 | 67 | expect(response).to redirect_to('/back/url')
|
69 | 68 | end
|
|
74 | 73 | allow(AddRoleService).to receive(:call).and_raise('OH NOES')
|
75 | 74 | post admin_user_add_role_path(user_id: user.id,
|
76 | 75 | resource_type: Role::ORG_ADMIN,
|
77 |
| - resource_id: org.id), |
| 76 | + resource_id: organization.id), |
78 | 77 | headers: { 'HTTP_REFERER' => '/back/url'}
|
79 | 78 | expect(AddRoleService).to have_received(:call).with(user_id: user.id.to_s,
|
80 | 79 | resource_type: Role::ORG_ADMIN.to_s,
|
81 |
| - resource_id: org.id.to_s) |
| 80 | + resource_id: organization.id.to_s) |
82 | 81 | expect(flash[:alert]).to eq('OH NOES')
|
83 | 82 | expect(response).to redirect_to('/back/url')
|
84 | 83 | end
|
|
135 | 134 | post admin_users_path, params: { user: { organization_id: organization.id } }
|
136 | 135 | expect(assigns(:organizations)).to eq(Organization.all.alphabetized)
|
137 | 136 | end
|
| 137 | + |
| 138 | + context "with missing organization id" do |
| 139 | + it "redirects back with flash message" do |
| 140 | + post admin_users_path, params: { user: { name: "ABC", email: organization.email } } |
| 141 | + |
| 142 | + expect(response).to render_template("admin/users/new") |
| 143 | + expect(flash[:error]).to eq("Failed to create user: Please select an organization for the user.") |
| 144 | + end |
| 145 | + end |
138 | 146 | end
|
139 | 147 | end
|
140 | 148 |
|
|
0 commit comments