Skip to content
This repository was archived by the owner on Dec 14, 2023. It is now read-only.

Commit 378e396

Browse files
authored
Merge pull request #281 from CoderDojo/feature/profile-redirect
Redirect to edit profile if not complete
2 parents eae0b95 + 35b11e3 commit 378e396

File tree

5 files changed

+122
-39
lines changed

5 files changed

+122
-39
lines changed

cypress/integration/authenticatiion/login_spec.js

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,20 @@ import page from '../../pages/login';
22

33
const setupLoggedInServer = () => {
44
cy.server();
5+
56
cy.route('POST', '/api/2.0/users/login', 'fx:loginSuccess').as('login');
67
cy.route('/api/2.0/users/instance', 'fx:parentLoggedIn').as('instance');
7-
cy.route('POST', '/api/2.0/dojos/users', 'fx:userDojosChampion').as('userDojosChampion');
8+
cy.route('POST', '/api/2.0/dojos/users', 'fx:userDojosChampion').as(
9+
'userDojosChampion',
10+
);
811
};
912

1013
describe('Login Page', () => {
1114
describe('Form Validation', () => {
1215
beforeEach(() => {
16+
cy.server();
17+
cy.route('/api/2.0/users/instance', 'fx:loggedOutUser');
18+
cy.route('/locale/languages', [{ name: 'en', code: 'en_US' }]);
1319
cy.visit('/login');
1420
});
1521

@@ -28,15 +34,19 @@ describe('Login Page', () => {
2834
it('should show an error when no email is provided on a blur event', () => {
2935
cy.get(page.emailFormatError).should('not.be.visible'); // TODO: Should be emailReqError?
3036
cy.get(page.email).click();
31-
cy.get(page.email).invoke('text').should('eq', '');
37+
cy.get(page.email)
38+
.invoke('text')
39+
.should('eq', '');
3240
cy.get(page.password).click();
3341
cy.get(page.emailFormatError).should('be.visible');
3442
});
3543

3644
it('should show an error when no password is provided on a blur event', () => {
3745
cy.get(page.passwordReqError).should('not.be.visible');
3846
cy.get(page.password).click();
39-
cy.get(page.password).invoke('text').should('eq', '');
47+
cy.get(page.password)
48+
.invoke('text')
49+
.should('eq', '');
4050
cy.get(page.loginBox).click();
4151
cy.get(page.passwordReqError).should('be.visible');
4252
});
@@ -93,6 +103,9 @@ describe('Login Page', () => {
93103

94104
describe('Successful Login', () => {
95105
it('should login successfully without a referrer query param', () => {
106+
cy.server();
107+
cy.route('/api/2.0/users/instance', 'fx:loggedOutUser');
108+
cy.route('/locale/languages', [{ name: 'en', code: 'en_US' }]);
96109
cy.visit('/login');
97110
setupLoggedInServer();
98111
cy.get(page.email).type('[email protected]');
@@ -102,6 +115,9 @@ describe('Login Page', () => {
102115
});
103116

104117
it('should login successfully with a referrer query param', () => {
118+
cy.server();
119+
cy.route('/api/2.0/users/instance', 'fx:loggedOutUser');
120+
cy.route('/locale/languages', [{ name: 'en', code: 'en_US' }]);
105121
cy.visit('/login?referrer=%2Fdashboard%2Ftickets');
106122
setupLoggedInServer();
107123
cy.get(page.email).type('[email protected]');

cypress/integration/dashboard/stats_spec.js

Lines changed: 58 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -3,62 +3,81 @@ import homePage from '../../pages/home';
33
describe('Homepage stats', () => {
44
beforeEach(() => {
55
cy.server();
6-
cy.route('POST', '/api/2.0/dojos/users', [{ userTypes: ['champion'] }]).as('userDojos');
6+
cy.route('POST', '/api/2.0/dojos/users', [{ userTypes: ['champion'] }]).as(
7+
'userDojos',
8+
);
9+
cy.route(
10+
'POST',
11+
'/api/2.0/profiles/user-profile-data',
12+
'fx:profiles/parent1',
13+
).as('userProfile');
714
});
815

916
it('should show a message pushing champions to use Zen when no stats is available', () => {
1017
cy.route('/api/2.0/users/instance', 'fx:parentLoggedIn').as('loggedIn');
11-
cy.route('POST', '/api/2.0/dojos/load-dojo-users', { response: [] }).as('dojoUsers');
12-
cy.route('POST', '/api/2.0/events/applications/search', []).as('bookedChildren');
18+
cy.route('POST', '/api/2.0/dojos/load-dojo-users', { response: [] }).as(
19+
'dojoUsers',
20+
);
21+
cy.route('POST', '/api/2.0/events/applications/search', []).as(
22+
'bookedChildren',
23+
);
1324
cy.visit('/home');
1425
cy.wait('@loggedIn');
1526
cy.wait('@userDojos');
1627
cy.wait('@bookedChildren');
1728
cy.wait('@dojoUsers');
18-
cy.wait('@dojoUsers');
19-
cy.get(homePage.statsUseZenMessage).invoke('text').should('eq', "No statistics are available at the moment.\n The more Zen is used, the more you\'ll find out about your Dojo!");
29+
cy.get(homePage.statsUseZenMessage)
30+
.invoke('text')
31+
.should(
32+
'eq',
33+
"No statistics are available at the moment.\n The more Zen is used, the more you'll find out about your Dojo!",
34+
);
2035
});
2136
describe('Youth', () => {
2237
it('should show the stats for the number of children booked', () => {
2338
cy.route('/api/2.0/users/instance', 'fx:parentLoggedIn').as('loggedIn');
24-
cy.route('POST', '/api/2.0/dojos/load-dojo-users', { response: [
25-
{ gender: 'Female'},
26-
{ gender: 'Male' },
27-
{ gender: 'Male' }
28-
] }).as('dojoUsers');
29-
cy.route('POST', '/api/2.0/events/applications/search', [{ userId: 'kid1' }]).as('bookedChildren');;
39+
cy.route('POST', '/api/2.0/dojos/load-dojo-users', {
40+
response: [{ gender: 'Female' }, { gender: 'Male' }, { gender: 'Male' }],
41+
}).as('dojoUsers');
42+
cy.route('POST', '/api/2.0/events/applications/search', [
43+
{ userId: 'kid1' },
44+
]).as('bookedChildren');
3045
cy.visit('/home');
3146
cy.wait('@loggedIn');
3247
cy.wait('@bookedChildren');
3348
cy.wait('@userDojos');
3449
cy.wait('@dojoUsers');
3550
cy.wait('@dojoUsers');
36-
cy.get(homePage.statsNbYouthChart).invoke('text').should('eq', '1 ninjas attended your events');
51+
cy.get(homePage.statsNbYouthChart)
52+
.invoke('text')
53+
.should('eq', '1 ninjas attended your events');
3754
});
3855
it('should show the stats chart of youth gender', () => {
3956
cy.route('/api/2.0/users/instance', 'fx:parentLoggedIn').as('loggedIn');
40-
cy.route('POST', '/api/2.0/events/applications/search', [{ userId: 'kid1' }]).as('bookedChildren');;
41-
cy.route('POST', '/api/2.0/dojos/load-dojo-users', { response: [
42-
{ gender: 'Female'},
43-
{ gender: 'Male' },
44-
{ gender: 'Male' }
45-
] }).as('dojoUsers');
57+
cy.route('POST', '/api/2.0/events/applications/search', [
58+
{ userId: 'kid1' },
59+
]).as('bookedChildren');
60+
cy.route('POST', '/api/2.0/dojos/load-dojo-users', {
61+
response: [{ gender: 'Female' }, { gender: 'Male' }, { gender: 'Male' }],
62+
}).as('dojoUsers');
4663
cy.visit('/home');
4764
cy.wait('@loggedIn');
4865
cy.wait('@bookedChildren');
4966
cy.wait('@userDojos');
5067
cy.wait('@dojoUsers');
5168
cy.wait('@dojoUsers');
52-
cy.get(homePage.statsYouthGenderChart).find('svg').should('be.visible');
69+
cy.get(homePage.statsYouthGenderChart)
70+
.find('svg')
71+
.should('be.visible');
5372
});
5473
it('should not display the hint for girls message when the girls ratio > 30%', () => {
5574
cy.route('/api/2.0/users/instance', 'fx:parentLoggedIn').as('loggedIn');
56-
cy.route('POST', '/api/2.0/events/applications/search', [{ userId: 'kid1' }]).as('bookedChildren');;
57-
cy.route('POST', '/api/2.0/dojos/load-dojo-users', { response: [
58-
{ gender: 'Female'},
59-
{ gender: 'Male' },
60-
{ gender: 'Male' }
61-
] }).as('dojoUsers');
75+
cy.route('POST', '/api/2.0/events/applications/search', [
76+
{ userId: 'kid1' },
77+
]).as('bookedChildren');
78+
cy.route('POST', '/api/2.0/dojos/load-dojo-users', {
79+
response: [{ gender: 'Female' }, { gender: 'Male' }, { gender: 'Male' }],
80+
}).as('dojoUsers');
6281
cy.visit('/home');
6382
cy.wait('@loggedIn');
6483
cy.wait('@bookedChildren');
@@ -69,20 +88,26 @@ describe('Homepage stats', () => {
6988
});
7089
it('should display the hint for girls message when the girls ratio < 30%', () => {
7190
cy.route('/api/2.0/users/instance', 'fx:parentLoggedIn').as('loggedIn');
72-
cy.route('POST', '/api/2.0/events/applications/search', [{ userId: 'kid1' }]).as('bookedChildren');;
73-
cy.route('POST', '/api/2.0/dojos/load-dojo-users', { response: [
74-
{ gender: 'Female'},
75-
{ gender: 'Male' },
76-
{ gender: 'Male' },
77-
{ gender: 'Male' },
78-
] }).as('dojoUsers');
91+
cy.route('POST', '/api/2.0/events/applications/search', [
92+
{ userId: 'kid1' },
93+
]).as('bookedChildren');
94+
cy.route('POST', '/api/2.0/dojos/load-dojo-users', {
95+
response: [
96+
{ gender: 'Female' },
97+
{ gender: 'Male' },
98+
{ gender: 'Male' },
99+
{ gender: 'Male' },
100+
],
101+
}).as('dojoUsers');
79102
cy.visit('/home');
80103
cy.wait('@loggedIn');
81104
cy.wait('@bookedChildren');
82105
cy.wait('@userDojos');
83106
cy.wait('@dojoUsers');
84107
cy.wait('@dojoUsers');
85-
cy.get(homePage.statsYouthGirlsHint).invoke('text').should('eq', 'More information about girls in Dojos');
108+
cy.get(homePage.statsYouthGirlsHint)
109+
.invoke('text')
110+
.should('eq', 'More information about girls in Dojos');
86111
});
87112
});
88113
});

src/dashboard/cd-dashboard.vue

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,8 @@
7979
},
8080
},
8181
async created() {
82-
this.loadProfile();
82+
await this.loadProfile();
83+
8384
await this.getUserDojos();
8485
this.setUserDimension();
8586
},

src/router/loggedInNavGuard.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,19 @@ import UserService from '@/users/service';
22

33
export default async function (to, from, next) {
44
const loggedInUser = (await UserService.getCurrentUser()).body;
5-
next(loggedInUser.login ? undefined : { name: 'Login', query: { referer: to.fullPath } });
5+
if (loggedInUser.login === null) {
6+
next({ name: 'Login', query: { referer: to.fullPath } });
7+
return null;
8+
}
9+
10+
if (
11+
loggedInUser.user &&
12+
loggedInUser.user.termsConditionsAccepted === false
13+
) {
14+
next({ path: `/dashboard/profile/${loggedInUser.user.id}/edit` });
15+
return null;
16+
}
17+
18+
next(undefined);
19+
return null;
620
}

test/unit/specs/router/loggedInNavGuard.spec.js

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,12 @@ describe('loggedInNavGuard', () => {
1717

1818
it('should continue to the route if the user is logged in', async () => {
1919
// ARRANGE
20-
MockUserService.getCurrentUser.resolves({ body: { login: { token: 'blah' } } });
20+
MockUserService.getCurrentUser.resolves({
21+
body: {
22+
login: { token: 'blah' },
23+
user: { termsConditionsAccepted: true },
24+
},
25+
});
2126

2227
// ACT
2328
await loggedInNavGuardWithMock({}, {}, nextMock);
@@ -44,4 +49,26 @@ describe('loggedInNavGuard', () => {
4449
query: { referer: '/some/path' },
4550
});
4651
});
52+
53+
it('should redirect to profile page if user has not accepted terms and conditions', async () => {
54+
// ARRANGE
55+
MockUserService.getCurrentUser.resolves({
56+
body: {
57+
login: { token: 'blah' },
58+
user: { id: 'userid', termsConditionsAccepted: false },
59+
},
60+
});
61+
const toMock = {
62+
fullPath: '/some/path',
63+
};
64+
65+
// ACT
66+
await loggedInNavGuardWithMock(toMock, {}, nextMock);
67+
68+
// ASSERT
69+
expect(nextMock).to.have.been.calledOnce;
70+
expect(nextMock).to.have.been.calledWith({
71+
path: '/dashboard/profile/userid/edit',
72+
});
73+
});
4774
});

0 commit comments

Comments
 (0)