|
6 | 6 | let(:user_group) { FactoryBot.create(:user_group) } |
7 | 7 | let(:entitleable) { FactoryBot.create(:project) } |
8 | 8 |
|
| 9 | + let!(:omniauth_uid) { "12345" } |
| 10 | + |
9 | 11 | let!(:user_group_external_identifier) { FactoryBot.create(:external_identifier, identifiable: user_group) } |
10 | 12 | let!(:entitleable_external_identifier) { FactoryBot.create(:external_identifier, identifiable: entitleable) } |
11 | 13 |
|
|
14 | 16 | let(:auth_hash) do |
15 | 17 | { |
16 | 18 | provider: provider.to_s, |
17 | | - uid: "12345", |
| 19 | + uid: omniauth_uid, |
18 | 20 | info: { |
19 | 21 | first_name: Faker::Name.first_name, |
20 | 22 | last_name: Faker::Name.last_name, |
|
25 | 27 | } |
26 | 28 | end |
27 | 29 |
|
| 30 | + before do |
| 31 | + OmniAuth.config.mock_auth[provider] = OmniAuth::AuthHash.new(auth_hash) |
| 32 | + Rails.application.env_config["omniauth.auth"] = OmniAuth.config.mock_auth[provider] |
| 33 | + end |
| 34 | + |
28 | 35 | context "as mocked google_oauth2 OAuth" do |
29 | 36 | let(:provider) { :google_oauth2 } |
30 | 37 |
|
|
33 | 40 | settings.integrations.google_oauth_client_id = "TEST" |
34 | 41 | settings.secrets.google_oauth_client_secret = "TEST" |
35 | 42 | end.save |
36 | | - |
37 | | - OmniAuth.config.mock_auth[provider] = OmniAuth::AuthHash.new(auth_hash) |
38 | | - Rails.application.env_config["omniauth.auth"] = OmniAuth.config.mock_auth[provider] |
39 | 43 | end |
40 | 44 |
|
41 | 45 | describe "/auth/:provider/callback" do |
|
59 | 63 |
|
60 | 64 | context "with entitlements defined" do |
61 | 65 |
|
62 | | - |
63 | 66 | it "creates entitlements" do |
64 | 67 | expect do |
65 | 68 | post "/auth/google_oauth2" |
66 | 69 | follow_redirect! |
67 | 70 | end.to change(Entitlement, :count).by(1) |
68 | 71 | end |
69 | 72 | end |
| 73 | + |
| 74 | + context "with an existing user" do |
| 75 | + let!(:user) { FactoryBot.create(:user) } |
| 76 | + let!(:identity) { FactoryBot.create(:identity, user: user, provider: provider.to_s, uid: omniauth_uid) } |
| 77 | + |
| 78 | + context "with an existing user group membership associated with the identity" do |
| 79 | + let!(:user_group_membership) { FactoryBot.create(:user_group_membership, user_group:, user:, source: identity) } |
| 80 | + |
| 81 | + context "and an OAuth request that does NOT include the user group" do |
| 82 | + before do |
| 83 | + OmniAuth.config.mock_auth[provider][:info][:user_groups] = nil |
| 84 | + end |
| 85 | + it "removes the user group membership" do |
| 86 | + expect do |
| 87 | + post "/auth/google_oauth2" |
| 88 | + follow_redirect! |
| 89 | + end.to change(UserGroupMembership, :count).by(-1) |
| 90 | + end |
| 91 | + end |
| 92 | + end |
| 93 | + |
| 94 | + context "with an existing entitlement associated with the identity" do |
| 95 | + let!(:entitlement) do |
| 96 | + FactoryBot.create(:entitlement, |
| 97 | + :read_access, |
| 98 | + target: user, |
| 99 | + entitler: identity.to_upsertable_entitler, |
| 100 | + subject: entitleable |
| 101 | + ) |
| 102 | + end |
| 103 | + |
| 104 | + context "and an OAuth request that does NOT include the entitleable" do |
| 105 | + before do |
| 106 | + OmniAuth.config.mock_auth[provider][:info][:entitlements] = nil |
| 107 | + end |
| 108 | + it "removes the entitlement" do |
| 109 | + expect do |
| 110 | + post "/auth/google_oauth2" |
| 111 | + follow_redirect! |
| 112 | + end.to change(Entitlement, :count).by(-1) |
| 113 | + end |
| 114 | + end |
| 115 | + end |
| 116 | + end |
70 | 117 | end |
71 | 118 | end |
72 | 119 |
|
73 | 120 | context "as mocked SAML" do |
74 | | - let(:provider_name) { "saml" } |
| 121 | + let(:provider) { :saml } |
75 | 122 |
|
76 | 123 | before do |
77 | | - allow_any_instance_of(SamlConfig).to receive(:provider_names).and_return([provider_name]) |
78 | | - |
79 | | - OmniAuth.config.mock_auth[:saml] = OmniAuth::AuthHash.new({ |
80 | | - provider: provider_name, |
81 | | - uid: "12345", |
82 | | - info: { |
83 | | - first_name: Faker::Name.first_name, |
84 | | - last_name: Faker::Name.last_name, |
85 | | - email: Faker::Internet.email |
86 | | - } |
87 | | - }) |
88 | | - |
89 | | - Rails.application.env_config["omniauth.auth"] = OmniAuth.config.mock_auth[:saml] |
| 124 | + allow(SamlConfig).to receive(:provider_names).and_return([provider.to_s]) |
| 125 | + allow_any_instance_of(SamlConfig).to receive(:provider_names).and_return([provider.to_s]) |
| 126 | + |
| 127 | + # Clear any cached config |
| 128 | + SamlConfig.instance_variable_set("@instance", nil) |
90 | 129 | end |
91 | 130 |
|
92 | | - xit "does some shit" do |
| 131 | + it "creates the identity" do |
93 | 132 | expect do |
94 | 133 | post "/auth/saml" |
| 134 | + follow_redirect! |
95 | 135 | end.to change { Identity.count }.by(1) |
96 | 136 | end |
97 | 137 | end |
|
0 commit comments