Skip to content

Commit

Permalink
fix: in-component navigation guards should not redirect
Browse files Browse the repository at this point in the history
  • Loading branch information
emuvente committed Dec 3, 2021
1 parent e712d7a commit 4346689
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 14 deletions.
13 changes: 4 additions & 9 deletions src/pages/LoginAndRegister/RegisterSocial.vue
Original file line number Diff line number Diff line change
Expand Up @@ -127,22 +127,17 @@ export default {
}
return validations;
},
beforeRouteEnter(to, from, next) {
// Redirect to error page if query parameters are missing
const { state, terms, names } = to.query ?? {};
if (!state || !(terms || names)) {
next('/error');
} else {
next();
}
},
created() {
if (this.$route.query.terms) {
this.needsTerms = true;
}
if (this.$route.query.names) {
this.needsNames = true;
}
// Support legacy behavior of this page, which was to show the terms checkbox only
if (!this.$route.query.terms && !this.$route.query.names) {
this.needsTerms = true;
}
},
methods: {
postRegisterSocialForm() {
Expand Down
14 changes: 13 additions & 1 deletion src/router/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,19 @@ export default [
}),
},
{ path: '/protocol', component: () => import('@/pages/Protocol') },
{ path: '/register/social', component: () => import('@/pages/LoginAndRegister/RegisterSocial') },
{
path: '/register/social',
component: () => import('@/pages/LoginAndRegister/RegisterSocial'),
beforeEnter(to, from, next) {
// Redirect to error page if state parameter is missing
const { state } = to.query ?? {};
if (!state) {
next('/error');
} else {
next();
}
},
},
{ path: '/register/guest', component: () => import('@/pages/LoginAndRegister/GuestAccountClaim') },
{
path: '/register/guest-redirect',
Expand Down
10 changes: 6 additions & 4 deletions test/unit/specs/pages/LoginAndRegister/RegisterSocial.spec.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
import RegisterSocial from '@/pages/LoginAndRegister/RegisterSocial';
import routes from '@/router/routes';

const beforeEnterGuard = routes.find(route => route.path === '/register/social').beforeEnter;

// Test that RegisterSocial.beforeRouteEnter redirects to the error page for route object `to`
const testRedirectToError = to => {
const next = jest.fn();
RegisterSocial.beforeRouteEnter(to, {}, next);
beforeEnterGuard(to, {}, next);
expect(next.mock.calls.length).toBe(1);
expect(next.mock.calls[0][0]).toBe('/error');
};

// Test that RegisterSocial.beforeRouteEnter does not redirect for route object `to`
const testNoRedirect = to => {
const next = jest.fn();
RegisterSocial.beforeRouteEnter(to, {}, next);
beforeEnterGuard(to, {}, next);
expect(next.mock.calls.length).toBe(1);
expect(next.mock.calls[0][0]).not.toBeDefined();
};
Expand All @@ -23,8 +25,8 @@ describe('RegisterSocial page', () => {
testRedirectToError({ query: { names: 1 } });
testRedirectToError({ query: { terms: 1 } });
testRedirectToError({ query: { names: 1, terms: 1 } });
testRedirectToError({ query: { state: 'abc' } });

testNoRedirect({ query: { state: 'abc' } });
testNoRedirect({ query: { state: 'abc', names: 1 } });
testNoRedirect({ query: { state: 'abc', terms: 1 } });
testNoRedirect({ query: { state: 'abc', names: 1, terms: 1 } });
Expand Down

0 comments on commit 4346689

Please sign in to comment.