Skip to content

Commit

Permalink
feat: sign-in tests and cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
rushtong committed Sep 13, 2024
1 parent 59cd7be commit 20dad74
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 9 deletions.
90 changes: 84 additions & 6 deletions cypress/component/SignIn/sign_in_button.spec.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,96 @@
/* eslint-disable no-undef */

import React from 'react';
import { mount } from 'cypress/react';
import { mount } from 'cypress/react18';
import SignInButton from '../../../src/components/SignInButton';
import {User} from '../../../src/libs/ajax/User';
import {Auth} from '../../../src/libs/auth/auth';
import {Storage} from '../../../src/libs/storage';
import {Metrics} from '../../../src/libs/ajax/Metrics';
import {StackdriverReporter} from '../../../src/libs/stackdriverReporter';
import {ToS} from '../../../src/libs/ajax/ToS';

const signInText = 'Sign In';

// Note that we do not want to click the signin button
// in tests as that would trigger an auth-flow we cannot
// replicate in a test environment.
describe('Sign In Component', function() {
const claims = {
jti: undefined, nbf: undefined, sub: undefined,
iss: '',
aud: '',
exp: 0,
iat: 0
};

const user = {
access_token: '', get expires_in() {
return undefined;
}, session_state: undefined, state: undefined, token_type: '', get expired() {
return undefined;
}, get scopes() {
return [];
}, toStorageString() {
return '';
},
profile: claims
};

const duosUser = {
displayName: 'display name',
email: 'test@user.com',
roles: [{
name : 'Admin'
}]
};

const userStatus = {
'adminEnabled': true,
'enabled': true,
'inAllUsersGroup': true,
'inGoogleProxyGroup': true,
'tosAccepted': true
};

describe('Sign In: Component Loads', function() {

it('Sign In Button Loads', function () {
cy.viewport(600, 300);
mount(<SignInButton history={undefined} onSignIn={() => {}}/>);
mount(<SignInButton history={undefined} />);
cy.contains(signInText).should('exist');
});

it('Sign In: On Success', function () {
cy.viewport(600, 300);
cy.stub(Auth, 'signIn').returns(Promise.resolve(user));
cy.stub(User, 'getMe').returns(duosUser);
cy.stub(StackdriverReporter, 'report');
cy.stub(Metrics, 'identify');
cy.stub(Metrics, 'syncProfile');
cy.stub(Metrics, 'captureEvent');
cy.stub(ToS, 'getStatus').returns(userStatus);
mount(<SignInButton history={[]} />);
cy.get('button').click().then(() => {
expect(Storage.getCurrentUser()).to.deep.equal(duosUser);
expect(Storage.getAnonymousId()).to.not.be.null;
expect(StackdriverReporter.report).to.not.be.called;
expect(Metrics.identify).to.be.called;
expect(Metrics.syncProfile).to.be.called;
expect(Metrics.captureEvent).to.be.called;
});
});

it('Sign In: No Roles Error Reporter Is Called', function () {
const bareUser = {email: 'test@user.com'};
cy.viewport(600, 300);
cy.stub(Auth, 'signIn').returns(Promise.resolve(user));
cy.stub(User, 'getMe').returns(bareUser);
cy.stub(StackdriverReporter, 'report');
cy.stub(Metrics, 'identify');
cy.stub(Metrics, 'syncProfile');
cy.stub(Metrics, 'captureEvent');
cy.stub(ToS, 'getStatus').returns(userStatus);
mount(<SignInButton history={[]} />);
cy.get('button').click().then(() => {
expect(StackdriverReporter.report).to.be.called;
});
});

});
4 changes: 1 addition & 3 deletions src/components/SignInButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import {DuosUserResponse} from '../types/responseTypes';

interface SignInButtonProps {
history: History;
onSignIn: () => Promise<void>;
}

interface ErrorInfo {
Expand All @@ -35,7 +34,7 @@ interface HttpError extends Error {

export const SignInButton = (props: SignInButtonProps) => {
const [errorDisplay, setErrorDisplay] = useState<ErrorDisplay>({});
const {onSignIn, history} = props;
const {history} = props;
const [isLoading, setIsLoading] = useState<boolean>(false);

// Utility function called in the normal success case and in the undocumented 409 case
Expand Down Expand Up @@ -101,7 +100,6 @@ export const SignInButton = (props: SignInButtonProps) => {
const registerAndRedirectNewUser = async (redirectTo: string, shouldRedirect: boolean) => {
const registeredUser = await User.registerUser();
setUserRoleStatuses(registeredUser, Storage);
await onSignIn();
await Metrics.identify(Storage.getAnonymousId());
await Metrics.syncProfile();
await Metrics.captureEvent(eventList.userRegister);
Expand Down

0 comments on commit 20dad74

Please sign in to comment.