Skip to content

Commit db96fc0

Browse files
committed
Simplify AuthenticationOption factories [ui skip]
1 parent bc1119f commit db96fc0

File tree

3 files changed

+62
-23
lines changed

3 files changed

+62
-23
lines changed

dashboard/test/factories/factories.rb

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -492,32 +492,16 @@
492492

493493
factory :authentication_option do
494494
association :user
495-
email {''}
496-
hashed_email {''}
497-
credential_type {AuthenticationOption::EMAIL}
498-
authentication_id {''}
499-
500-
factory :email_authentication_option do
501-
sequence(:email) {|n| "testuser#{n}@example.com.xx"}
502-
after(:create) do |auth|
503-
auth.authentication_id = auth.hashed_email
504-
end
505-
end
495+
sequence(:email) {|n| "testuser#{n}@example.com.xx"}
496+
credential_type AuthenticationOption::EMAIL
497+
authentication_id {User.hash_email email}
506498

507499
factory :google_authentication_option do
508500
credential_type AuthenticationOption::GOOGLE
509-
sequence(:email) {|n| "testuser#{n}@example.com.xx"}
510-
after(:create) do |auth|
511-
auth.authentication_id = auth.hashed_email
512-
end
513501
end
514502

515503
factory :facebook_authentication_option do
516504
credential_type AuthenticationOption::FACEBOOK
517-
sequence(:email) {|n| "testuser#{n}@example.com.xx"}
518-
after(:create) do |auth|
519-
auth.authentication_id = auth.hashed_email
520-
end
521505
end
522506
end
523507

dashboard/test/models/authentication_option_test.rb

Lines changed: 58 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class AuthenticationOptionTest < ActiveSupport::TestCase
1717
original_teacher_email = '[email protected]'
1818
new_teacher_email = '[email protected]'
1919
teacher = create(:teacher, email: original_teacher_email)
20-
email_auth = create(:email_authentication_option, user: teacher, email: new_teacher_email)
20+
email_auth = create(:authentication_option, user: teacher, email: new_teacher_email)
2121
teacher.update(primary_contact_info: email_auth, provider: 'migrated')
2222
assert_equal teacher.primary_contact_info_id, email_auth.id
2323
assert_equal new_teacher_email, teacher.email
@@ -28,15 +28,15 @@ class AuthenticationOptionTest < ActiveSupport::TestCase
2828
teacher_email = '[email protected]'
2929
sanitized = '[email protected]'
3030
teacher = create(:teacher, email: teacher_email)
31-
email_auth = create(:email_authentication_option, user: teacher, email: teacher_email)
31+
email_auth = create(:authentication_option, user: teacher, email: teacher_email)
3232
assert_equal sanitized, email_auth.email
3333
assert_equal email_auth.hashed_email, AuthenticationOption.hash_email(sanitized)
3434
end
3535

3636
test 'student email is not stored but hashed_email is' do
3737
student_email = '[email protected]'
3838
student = create(:student, email: student_email)
39-
email_auth = create(:email_authentication_option, user: student, email: student_email)
39+
email_auth = create(:authentication_option, user: student, email: student_email)
4040
assert email_auth.user.student?
4141
assert_equal '', email_auth.email
4242
assert_equal student.hashed_email, email_auth.hashed_email
@@ -155,4 +155,59 @@ class AuthenticationOptionTest < ActiveSupport::TestCase
155155
}
156156
assert_equal new_data, auth_option.data_hash
157157
end
158+
159+
test "factory: :authentication_option" do
160+
option = create :authentication_option
161+
assert option.valid?
162+
assert option.persisted?
163+
assert_equal AuthenticationOption::EMAIL, option.credential_type
164+
165+
# Default user is a student so email is empty
166+
assert option.user.student?
167+
assert_empty option.email
168+
169+
refute_empty option.hashed_email
170+
assert_equal option.hashed_email, option.authentication_id
171+
end
172+
173+
test "factory: :authentication_option for teacher" do
174+
option = create :authentication_option, user: create(:teacher)
175+
assert option.valid?
176+
assert option.persisted?
177+
assert_equal AuthenticationOption::EMAIL, option.credential_type
178+
179+
assert option.user.teacher?
180+
refute_empty option.email
181+
182+
refute_empty option.hashed_email
183+
assert_equal option.hashed_email, option.authentication_id
184+
end
185+
186+
test "factory: :google_authentication_option" do
187+
option = create :google_authentication_option
188+
assert option.valid?
189+
assert option.persisted?
190+
assert_equal AuthenticationOption::GOOGLE, option.credential_type
191+
192+
# Default user is a student so email is empty
193+
assert option.user.student?
194+
assert_empty option.email
195+
196+
refute_empty option.hashed_email
197+
refute_empty option.authentication_id
198+
end
199+
200+
test "factory: :facebook_authentication_option" do
201+
option = create :facebook_authentication_option
202+
assert option.valid?
203+
assert option.persisted?
204+
assert_equal AuthenticationOption::FACEBOOK, option.credential_type
205+
206+
# Default user is a student so email is empty
207+
assert option.user.student?
208+
assert_empty option.email
209+
210+
refute_empty option.hashed_email
211+
refute_empty option.authentication_id
212+
end
158213
end

dashboard/test/models/user_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3815,7 +3815,7 @@ def hide_scripts_in_sections(section1, section2)
38153815

38163816
test 'find_by_email locates a multi-auth teacher by non-primary email' do
38173817
teacher = create :teacher, :with_migrated_email_authentication_option
3818-
second_option = create :email_authentication_option, user: teacher
3818+
second_option = create :authentication_option, user: teacher
38193819
assert_equal teacher, User.find_by_email(second_option.email)
38203820
end
38213821

0 commit comments

Comments
 (0)