Skip to content

Commit d91a354

Browse files
committed
[C] Update oauth tests
1 parent 0590414 commit d91a354

File tree

5 files changed

+64
-24
lines changed

5 files changed

+64
-24
lines changed

api/app/controllers/concerns/manages_oauth_cookie.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ def clean_up_auth_code!(code = params[:auth_code])
3636
def cookie_domain
3737
domain = Rails.application.config.manifold.domain
3838

39-
if Rails.env.development? && domain.include?(":")
39+
if (Rails.env.development? || Rails.env.test?) && domain.include?(":")
4040
domain.split(":")[0]
4141
else
4242
".#{domain}"

api/app/models/identity.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ class Identity < ApplicationRecord
99

1010
has_many :user_group_memberships, as: :source
1111

12-
validates :provider, inclusion: { in: (ManifoldEnv.oauth.known_strategies + SamlConfig.provider_names) }
12+
validates :provider, inclusion: { in: ->(_) { (ManifoldEnv.oauth.known_strategies + SamlConfig.provider_names) } }
1313
validates :uid, :provider, presence: true
1414
validates :uid, uniqueness: { scope: %i(provider) }
1515

api/app/operations/user_groups/remove_member.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class RemoveMember
1414
def call(user_group, user_or_identity)
1515
parse_user_or_identity(user_or_identity)
1616

17-
user_group.memberships.where(user:, identity:).destroy_all
17+
user_group.memberships.where(user:, source: identity).destroy_all
1818

1919
Success()
2020
end

api/app/services/external_auth/auth_action.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ module AuthAction
1010
object :auth_hash, class: "OmniAuth::AuthHash", converter: :new
1111

1212
validates :provider,
13-
inclusion: { in: (ManifoldEnv.oauth.known_strategies + SamlConfig.provider_names) },
13+
inclusion: { in: ->(_) { (ManifoldEnv.oauth.known_strategies + SamlConfig.provider_names) } },
1414
presence: true
1515

1616
delegate :info, to: :auth_hash, prefix: :auth

api/spec/requests/oauth_spec.rb

Lines changed: 60 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
let(:user_group) { FactoryBot.create(:user_group) }
77
let(:entitleable) { FactoryBot.create(:project) }
88

9+
let!(:omniauth_uid) { "12345" }
10+
911
let!(:user_group_external_identifier) { FactoryBot.create(:external_identifier, identifiable: user_group) }
1012
let!(:entitleable_external_identifier) { FactoryBot.create(:external_identifier, identifiable: entitleable) }
1113

@@ -14,7 +16,7 @@
1416
let(:auth_hash) do
1517
{
1618
provider: provider.to_s,
17-
uid: "12345",
19+
uid: omniauth_uid,
1820
info: {
1921
first_name: Faker::Name.first_name,
2022
last_name: Faker::Name.last_name,
@@ -25,6 +27,11 @@
2527
}
2628
end
2729

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+
2835
context "as mocked google_oauth2 OAuth" do
2936
let(:provider) { :google_oauth2 }
3037

@@ -33,9 +40,6 @@
3340
settings.integrations.google_oauth_client_id = "TEST"
3441
settings.secrets.google_oauth_client_secret = "TEST"
3542
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]
3943
end
4044

4145
describe "/auth/:provider/callback" do
@@ -59,39 +63,75 @@
5963

6064
context "with entitlements defined" do
6165

62-
6366
it "creates entitlements" do
6467
expect do
6568
post "/auth/google_oauth2"
6669
follow_redirect!
6770
end.to change(Entitlement, :count).by(1)
6871
end
6972
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
70117
end
71118
end
72119

73120
context "as mocked SAML" do
74-
let(:provider_name) { "saml" }
121+
let(:provider) { :saml }
75122

76123
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)
90129
end
91130

92-
xit "does some shit" do
131+
it "creates the identity" do
93132
expect do
94133
post "/auth/saml"
134+
follow_redirect!
95135
end.to change { Identity.count }.by(1)
96136
end
97137
end

0 commit comments

Comments
 (0)