Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SSO: Fallback to email if the username field is empty #1400

Merged
merged 3 commits into from
Jun 7, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions src/__tests__/connection/database/index.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import Immutable, { List, Map } from 'immutable';
import { databaseUsernameValue } from '../../../connection/database';

describe('databaseUsernameValue', () => {
const getModel = (email, username, usernameRequired) =>
Immutable.fromJS({
field: {
email: {
value: email
},
username: {
value: username
}
},
core: {
transient: {
connections: {
database: [
{
requireUsername: usernameRequired
}
]
}
}
}
});

beforeEach(() => {
jest.resetAllMocks();
});

describe('for database connection without username required', () => {
const model = getModel('user@contoso.com', null, false);

it('should get the email', () => {
expect(databaseUsernameValue(model)).toEqual('user@contoso.com');
});
});

describe('for database connection with username required', () => {
const model = getModel('user@contoso.com', 'user', true);

it('should get the username', () => {
expect(databaseUsernameValue(model)).toEqual('user');
});

describe('and only email address is filled in', () => {
const model = getModel('user@contoso.com', null, true);

it('should get the email address', () => {
expect(databaseUsernameValue(model)).toEqual('user@contoso.com');
});
});
});
});
7 changes: 6 additions & 1 deletion src/connection/database/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,12 @@ export function databaseLogInWithEmail(m) {
}

export function databaseUsernameValue(m) {
return getFieldValue(m, databaseLogInWithEmail(m) ? 'email' : 'username');
const isEmailOnly = databaseLogInWithEmail(m);
if (isEmailOnly) {
return getFieldValue(m, 'email');
}

return getFieldValue(m, 'username') || getFieldValue(m, 'email');
}

export function authWithUsername(m) {
Expand Down