|
4 | 4 |
|
5 | 5 | describe UniqueUsernameValidator do |
6 | 6 | describe '#validate' do |
| 7 | + context 'when local account' do |
| 8 | + it 'does not add errors if username is nil' do |
| 9 | + account = double(username: nil, domain: nil, persisted?: false, errors: double(add: nil)) |
| 10 | + subject.validate(account) |
| 11 | + expect(account.errors).to_not have_received(:add) |
| 12 | + end |
| 13 | + |
| 14 | + it 'does not add errors when existing one is subject itself' do |
| 15 | + account = Fabricate(:account, username: 'abcdef') |
| 16 | + expect(account).to be_valid |
| 17 | + end |
| 18 | + |
| 19 | + it 'adds an error when the username is already used with ignoring cases' do |
| 20 | + Fabricate(:account, username: 'ABCdef') |
| 21 | + account = double(username: 'abcDEF', domain: nil, persisted?: false, errors: double(add: nil)) |
| 22 | + subject.validate(account) |
| 23 | + expect(account.errors).to have_received(:add) |
| 24 | + end |
| 25 | + |
| 26 | + it 'does not add errors when same username remote account exists' do |
| 27 | + Fabricate(:account, username: 'abcdef', domain: 'example.com') |
| 28 | + account = double(username: 'abcdef', domain: nil, persisted?: false, errors: double(add: nil)) |
| 29 | + subject.validate(account) |
| 30 | + expect(account.errors).to_not have_received(:add) |
| 31 | + end |
| 32 | + end |
| 33 | + end |
| 34 | + |
| 35 | + context 'when remote account' do |
7 | 36 | it 'does not add errors if username is nil' do |
8 | | - account = double(username: nil, persisted?: false, errors: double(add: nil)) |
| 37 | + account = double(username: nil, domain: 'example.com', persisted?: false, errors: double(add: nil)) |
9 | 38 | subject.validate(account) |
10 | 39 | expect(account.errors).to_not have_received(:add) |
11 | 40 | end |
12 | 41 |
|
13 | 42 | it 'does not add errors when existing one is subject itself' do |
14 | | - account = Fabricate(:account, username: 'abcdef') |
| 43 | + account = Fabricate(:account, username: 'abcdef', domain: 'example.com') |
15 | 44 | expect(account).to be_valid |
16 | 45 | end |
17 | 46 |
|
|
24 | 53 | end |
25 | 54 |
|
26 | 55 | it 'adds an error when the username is already used with ignoring cases' do |
27 | | - Fabricate(:account, username: 'ABCdef') |
28 | | - account = double(username: 'abcDEF', persisted?: false, errors: double(add: nil)) |
| 56 | + Fabricate(:account, username: 'ABCdef', domain: 'example.com') |
| 57 | + account = double(username: 'abcDEF', domain: 'example.com', persisted?: false, errors: double(add: nil)) |
| 58 | + subject.validate(account) |
| 59 | + expect(account.errors).to have_received(:add) |
| 60 | + end |
| 61 | + |
| 62 | + it 'adds an error when the domain is already used with ignoring cases' do |
| 63 | + Fabricate(:account, username: 'ABCdef', domain: 'example.com') |
| 64 | + account = double(username: 'ABCdef', domain: 'EXAMPLE.COM', persisted?: false, errors: double(add: nil)) |
29 | 65 | subject.validate(account) |
30 | 66 | expect(account.errors).to have_received(:add) |
31 | 67 | end |
| 68 | + |
| 69 | + it 'does not add errors when account with the same username and another domain exists' do |
| 70 | + Fabricate(:account, username: 'abcdef', domain: 'example.com') |
| 71 | + account = double(username: 'abcdef', domain: 'example2.com', persisted?: false, errors: double(add: nil)) |
| 72 | + subject.validate(account) |
| 73 | + expect(account.errors).to_not have_received(:add) |
| 74 | + end |
32 | 75 | end |
33 | 76 | end |
0 commit comments