-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[F] Add the ability for admins to verify users
- Loading branch information
1 parent
44ada2a
commit 950c4fa
Showing
8 changed files
with
172 additions
and
35 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
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
46 changes: 46 additions & 0 deletions
46
api/db/migrate/20240223163849_add_verified_by_admin_to_users.rb
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,46 @@ | ||
# frozen_string_literal: true | ||
|
||
class AddVerifiedByAdminToUsers < ActiveRecord::Migration[6.1] | ||
def change | ||
change_table :users do |t| | ||
t.timestamp :verified_by_admin_at | ||
|
||
t.boolean :established, null: false, default: false | ||
t.boolean :trusted, null: false, default: false | ||
|
||
t.index :established | ||
t.index :trusted | ||
end | ||
|
||
reversible do |dir| | ||
dir.up do | ||
say_with_time "Calculating established for users" do | ||
exec_update <<~SQL | ||
UPDATE users SET established = email_confirmed_at IS NOT NULL; | ||
SQL | ||
end | ||
|
||
say_with_time "Calculating trusted for users" do | ||
exec_update <<~SQL | ||
WITH trustedness AS ( | ||
SELECT DISTINCT "users"."id" AS user_id | ||
FROM "users" | ||
INNER JOIN "users_roles" ON "users_roles"."user_id" = "users"."id" | ||
INNER JOIN "roles" ON "roles"."id" = "users_roles"."role_id" | ||
WHERE | ||
( | ||
(roles.name IN ('admin', 'editor', 'moderator', 'marketeer')) AND (roles.resource_type IS NULL) AND (roles.resource_id IS NULL) | ||
) | ||
OR | ||
( | ||
(roles.name = 'project_editor') AND (roles.resource_type = 'Project') | ||
) | ||
) | ||
UPDATE users SET "trusted" = TRUE | ||
WHERE id IN (SELECT user_id FROM trustedness); | ||
SQL | ||
end | ||
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 |
---|---|---|
@@ -1,17 +1,6 @@ | ||
require "rails_helper" | ||
# frozen_string_literal: true | ||
|
||
# rubocop:disable Layout/LineLength | ||
RSpec.describe User, type: :model do | ||
it "has a valid factory" do | ||
expect(FactoryBot.build(:user)).to be_valid | ||
end | ||
|
||
it "has many favorites" do | ||
user = User.new | ||
2.times { user.favorites.build } | ||
expect(user.favorites.length).to be 2 | ||
end | ||
|
||
it "reports whether or not a favoritable is among its favorites" do | ||
user = FactoryBot.create(:user) | ||
project = FactoryBot.create(:project) | ||
|
@@ -89,6 +78,24 @@ | |
expect(User.find_by(email: "[email protected]")).to eq user | ||
end | ||
|
||
context "when setting admin_verified" do | ||
let!(:user) { FactoryBot.create :user } | ||
|
||
it "updates other properties" do | ||
expect do | ||
user.admin_verified = true | ||
user.save! | ||
end.to change { user.verified_by_admin_at }.from(nil).to(a_kind_of(ActiveSupport::TimeWithZone)) | ||
.and change { user.established }.from(false).to(true) | ||
|
||
expect do | ||
user.admin_verified = ?0 | ||
user.save! | ||
end.to change { user.verified_by_admin_at }.from(a_kind_of(ActiveSupport::TimeWithZone)).to(nil) | ||
.and change { user.established }.from(true).to(false) | ||
end | ||
end | ||
|
||
context "when changing a role" do | ||
let!(:user) { FactoryBot.create :user, :editor } | ||
let!(:project) { FactoryBot.create :project } | ||
|
@@ -114,7 +121,9 @@ | |
it "updates the role and kind" do | ||
expect do | ||
user.add_role :editor | ||
end.to change { user.role.to_sym }.from(:reader).to(:editor).and change { user.kind.to_sym }.from(:reader).to(:editor) | ||
end.to change { user.role.to_sym }.from(:reader).to(:editor) | ||
.and change { user.kind.to_sym }.from(:reader).to(:editor) | ||
.and change { user.trusted }.from(false).to(true) | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe Users::MarkEmailConfirmed, type: :operation do | ||
let_it_be(:user, refind: true) { FactoryBot.create :user } | ||
|
||
let(:operation_args) { [user] } | ||
|
||
it "will confirm the user and mark them as trusted" do | ||
expect do | ||
expect_calling_the_operation.to succeed | ||
end.to change { user.reload.email_confirmed? }.from(false).to(true) | ||
.and change { user.reload.established? }.from(false).to(true) | ||
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