|
8 | 8 | end.to change(User, :count).by(1)
|
9 | 9 | end.to change(Authorization, :count).by(1)
|
10 | 10 | end
|
| 11 | + |
| 12 | + describe 'special treatment for stale Twitter users' do |
| 13 | + let(:existing_twitter_auth) { Authorization.handle_authorization(nil, TWITTER_AUTH_HASH) } |
| 14 | + let(:existing_twitter_user) { existing_twitter_auth.user } |
| 15 | + let(:new_auth_hash) { GITHUB_AUTH_HASH } |
| 16 | + let(:date_of_test) { Authorization::DEFAULT_TWITTER_USER_FALLBACK_DEADLINE - 1 } |
| 17 | + |
| 18 | + around do |example| |
| 19 | + travel_to(date_of_test) do |
| 20 | + example.run |
| 21 | + end |
| 22 | + end |
| 23 | + |
| 24 | + shared_examples 'failing with duplicate nickname' do |
| 25 | + it 'raises duplication error' do |
| 26 | + expect do |
| 27 | + Authorization.handle_authorization(nil, new_auth_hash) |
| 28 | + end.to raise_error(User::DuplicateNickname) |
| 29 | + end |
| 30 | + end |
| 31 | + |
| 32 | + shared_examples 'updating the existing user' do |
| 33 | + it 'adds the auth to the existing Twitter account', :aggregate_failures do |
| 34 | + gh_auth = Authorization.handle_authorization(nil, new_auth_hash) |
| 35 | + expect(gh_auth.user).to eq(existing_twitter_user) |
| 36 | + expect(existing_twitter_user.reload.github).to eq(new_auth_hash.dig('info', 'nickname')) |
| 37 | + end |
| 38 | + end |
| 39 | + |
| 40 | + before do |
| 41 | + existing_twitter_user |
| 42 | + create(:authorization, provider: 'twitter', user: create(:user, email: nil)) |
| 43 | + end |
| 44 | + |
| 45 | + context 'when emails do not match' do |
| 46 | + context 'when nicknames are the same' do |
| 47 | + it_behaves_like 'failing with duplicate nickname' |
| 48 | + end |
| 49 | + |
| 50 | + context 'when nicknames are different' do |
| 51 | + before { existing_twitter_user.update!(nickname: existing_twitter_user.nickname * 2) } |
| 52 | + |
| 53 | + it 'creates a new user', :aggregate_failures do |
| 54 | + gh_user = Authorization.handle_authorization(nil, new_auth_hash).user |
| 55 | + expect(gh_user).to be_persisted |
| 56 | + expect(gh_user).not_to eq(existing_twitter_user) |
| 57 | + end |
| 58 | + end |
| 59 | + end |
| 60 | + |
| 61 | + context 'when emails match' do |
| 62 | + before { existing_twitter_user.update!(email: new_auth_hash.dig('info', 'email')) } |
| 63 | + |
| 64 | + context 'when email is unique and user has only Twiter auth' do |
| 65 | + around do |example| |
| 66 | + value_before = ENV['TWITTER_USER_FALLBACK_DEADLINE'] |
| 67 | + ENV['TWITTER_USER_FALLBACK_DEADLINE'] = deadline_value |
| 68 | + example.run |
| 69 | + ENV['TWITTER_USER_FALLBACK_DEADLINE'] = value_before |
| 70 | + end |
| 71 | + |
| 72 | + shared_examples 'updating the user before the deadline, failing afterwards' do |
| 73 | + context 'before the deadline' do |
| 74 | + let(:date_of_test) { effective_deadline - 1 } |
| 75 | + |
| 76 | + it_behaves_like 'updating the existing user' |
| 77 | + end |
| 78 | + |
| 79 | + context 'with a valid value and after that deadline' do |
| 80 | + let(:date_of_test) { effective_deadline + 1 } |
| 81 | + |
| 82 | + it_behaves_like 'failing with duplicate nickname' |
| 83 | + end |
| 84 | + end |
| 85 | + |
| 86 | + context 'with no explicit deadline' do |
| 87 | + let(:deadline_value) { nil } |
| 88 | + let(:effective_deadline) { Authorization::DEFAULT_TWITTER_USER_FALLBACK_DEADLINE } |
| 89 | + |
| 90 | + it_behaves_like 'updating the user before the deadline, failing afterwards' |
| 91 | + end |
| 92 | + |
| 93 | + context 'with a valid explicit deadline' do |
| 94 | + let(:deadline_value) { '2026-01-01' } |
| 95 | + let(:effective_deadline) { Date.parse('2026-01-01') } |
| 96 | + |
| 97 | + it_behaves_like 'updating the user before the deadline, failing afterwards' |
| 98 | + end |
| 99 | + |
| 100 | + context 'with an invalid explicit deadline' do |
| 101 | + let(:deadline_value) { 'bad date!' } |
| 102 | + let(:effective_deadline) { Authorization::DEFAULT_TWITTER_USER_FALLBACK_DEADLINE } |
| 103 | + |
| 104 | + it_behaves_like 'updating the user before the deadline, failing afterwards' |
| 105 | + end |
| 106 | + end |
| 107 | + |
| 108 | + context 'when several users have the same email' do |
| 109 | + before { create(:user, email: existing_twitter_user.email) } |
| 110 | + |
| 111 | + it_behaves_like 'failing with duplicate nickname' |
| 112 | + end |
| 113 | + |
| 114 | + context 'when the user has other authorizations' do |
| 115 | + before { create(:authorization, user: existing_twitter_user) } |
| 116 | + |
| 117 | + it_behaves_like 'failing with duplicate nickname' |
| 118 | + end |
| 119 | + |
| 120 | + context 'when the user has a single auth but it is not Twitter' do |
| 121 | + before { existing_twitter_user.authorizations = [create(:authorization)] } |
| 122 | + |
| 123 | + it_behaves_like 'failing with duplicate nickname' |
| 124 | + end |
| 125 | + end |
| 126 | + |
| 127 | + context 'when new auth has no email' do |
| 128 | + let(:new_auth_hash) { GITHUB_AUTH_HASH.deep_merge('info' => { 'email' => nil }) } |
| 129 | + |
| 130 | + it_behaves_like 'failing with duplicate nickname' |
| 131 | + end |
| 132 | + end |
11 | 133 | end
|
0 commit comments