|
850 | 850 | end |
851 | 851 | end |
852 | 852 |
|
853 | | - describe '#find_by_username_or_email' do |
854 | | - it 'works correctly' do |
855 | | - bob = Fabricate(:user, username: 'bob', name: 'bobs', email: 'bob@bob.com') |
856 | | - bob2 = Fabricate(:user, username: 'bob2', name: 'bobs', email: 'bob2@bob.com') |
| 853 | + describe '.find_by_username_or_email' do |
| 854 | + it 'finds user by username' do |
| 855 | + bob = Fabricate(:user, username: 'bob') |
857 | 856 |
|
858 | | - expect(User.find_by_username_or_email('bob22@bob.com')).to eq(nil) |
859 | | - expect(User.find_by_username_or_email('bobs')).to eq(nil) |
| 857 | + found_user = User.find_by_username_or_email('bob') |
860 | 858 |
|
861 | | - expect(User.find_by_username_or_email('bob2')).to eq(bob2) |
862 | | - expect(User.find_by_username_or_email('bob2@BOB.com')).to eq(bob2) |
| 859 | + expect(found_user).to eq bob |
| 860 | + end |
| 861 | + |
| 862 | + it 'finds user by email' do |
| 863 | + bob = Fabricate(:user, email: 'bob@example.com') |
| 864 | + |
| 865 | + found_user = User.find_by_username_or_email('bob@example.com') |
| 866 | + |
| 867 | + expect(found_user).to eq bob |
| 868 | + end |
| 869 | + |
| 870 | + context 'when user does not exist' do |
| 871 | + it 'returns nil' do |
| 872 | + found_user = User.find_by_username_or_email('doesnotexist@example.com') || |
| 873 | + User.find_by_username_or_email('doesnotexist') |
| 874 | + |
| 875 | + expect(found_user).to be_nil |
| 876 | + end |
| 877 | + end |
| 878 | + |
| 879 | + context 'when username case does not match' do |
| 880 | + it 'finds user' do |
| 881 | + bob = Fabricate(:user, username: 'bob') |
| 882 | + |
| 883 | + found_user = User.find_by_username_or_email('Bob') |
863 | 884 |
|
864 | | - expect(User.find_by_username_or_email('bob')).to eq(bob) |
865 | | - expect(User.find_by_username_or_email('bob@BOB.com')).to eq(bob) |
| 885 | + expect(found_user).to eq bob |
| 886 | + end |
| 887 | + end |
| 888 | + |
| 889 | + context 'when email domain case does not match' do |
| 890 | + it 'finds user' do |
| 891 | + bob = Fabricate(:user, email: 'bob@example.com') |
| 892 | + |
| 893 | + found_user = User.find_by_username_or_email('bob@Example.com') |
| 894 | + |
| 895 | + expect(found_user).to eq bob |
| 896 | + end |
| 897 | + end |
| 898 | + |
| 899 | + context 'when multiple users are found' do |
| 900 | + it 'raises an exception' do |
| 901 | + user_query = stub(all: [stub, stub]) |
| 902 | + User.stubs(:where).with(username_lower: 'bob').returns(user_query) |
| 903 | + |
| 904 | + expect { User.find_by_username_or_email('bob') }.to raise_error(Discourse::TooManyMatches) |
| 905 | + end |
866 | 906 | end |
867 | 907 | end |
868 | 908 | end |
0 commit comments