-
Notifications
You must be signed in to change notification settings - Fork 78
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #914 from alphagov/lowercase-admin-user-emails
Lowercase admin user email addresses
- Loading branch information
Showing
4 changed files
with
93 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
class LowercaseAdminUserEmails < ActiveRecord::Migration[6.1] | ||
class AdminUser < ActiveRecord::Base; end | ||
|
||
def change | ||
up_only do | ||
AdminUser.find_each do |user| | ||
user.email = user.email.downcase | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -33,11 +33,6 @@ | |
it { is_expected.to allow_value("[email protected]").for(:email)} | ||
it { is_expected.not_to allow_value("jimbo").for(:email) } | ||
|
||
it "should validate uniqueness of email" do | ||
FactoryBot.create(:moderator_user) | ||
is_expected.to validate_uniqueness_of(:email).case_insensitive | ||
end | ||
|
||
it "should allow sysadmin role" do | ||
u = FactoryBot.build(:admin_user, :role => 'sysadmin') | ||
expect(u).to be_valid | ||
|
@@ -73,6 +68,72 @@ | |
end | ||
end | ||
|
||
describe "class methods" do | ||
describe ".find_or_create_from!" do | ||
let(:provider) { IdentityProvider.providers.first } | ||
|
||
context "when a user doesn't exist" do | ||
let(:auth_data1) do | ||
OmniAuth::AuthHash.new( | ||
uid: "[email protected]", | ||
provider: "example", | ||
info: { | ||
first_name: "Anne", | ||
last_name: "Admin", | ||
groups: ["sysadmins"] | ||
} | ||
) | ||
end | ||
|
||
let(:auth_data2) do | ||
OmniAuth::AuthHash.new( | ||
uid: "[email protected]", | ||
provider: "example", | ||
info: { | ||
first_name: "Anne", | ||
last_name: "Admin", | ||
groups: ["sysadmins"] | ||
} | ||
) | ||
end | ||
|
||
it "creates only one new user" do | ||
expect { | ||
AdminUser.find_or_create_from!(provider, auth_data1) | ||
}.to change(AdminUser, :count).by(1) | ||
|
||
expect { | ||
AdminUser.find_or_create_from!(provider, auth_data2) | ||
}.not_to change(AdminUser, :count) | ||
end | ||
end | ||
|
||
context "when a user exists" do | ||
let(:auth_data) do | ||
OmniAuth::AuthHash.new( | ||
uid: "[email protected]", | ||
provider: "example", | ||
info: { | ||
first_name: "Anne", | ||
last_name: "Admin", | ||
groups: ["sysadmins"] | ||
} | ||
) | ||
end | ||
|
||
before do | ||
FactoryBot.create(:sysadmin_user, email: "[email protected]", first_name: "Anne", last_name: "Admin") | ||
end | ||
|
||
it "doesn't create a new user" do | ||
expect { | ||
AdminUser.find_or_create_from!(provider, auth_data) | ||
}.not_to change(AdminUser, :count) | ||
end | ||
end | ||
end | ||
end | ||
|
||
describe "instance methods" do | ||
describe "#destroy" do | ||
context "when there is no current user and there is more than one" do | ||
|
@@ -114,6 +175,13 @@ | |
end | ||
end | ||
|
||
describe "#email=" do | ||
it "should downcase the email address" do | ||
user = FactoryBot.create(:moderator_user, :email => '[email protected]') | ||
expect(user.email).to eq('[email protected]') | ||
end | ||
end | ||
|
||
describe "#name" do | ||
it "should return a user's name" do | ||
user = FactoryBot.create(:moderator_user, :first_name => 'Jo', :last_name => 'Public') | ||
|