From 43466890450a8dde3016b5958d2b00d317d2992d Mon Sep 17 00:00:00 2001 From: Joshua Kiwiet-Pantaleoni Date: Fri, 3 Dec 2021 08:27:04 -0800 Subject: [PATCH] fix: in-component navigation guards should not redirect --- src/pages/LoginAndRegister/RegisterSocial.vue | 13 ++++--------- src/router/routes.js | 14 +++++++++++++- .../pages/LoginAndRegister/RegisterSocial.spec.js | 10 ++++++---- 3 files changed, 23 insertions(+), 14 deletions(-) diff --git a/src/pages/LoginAndRegister/RegisterSocial.vue b/src/pages/LoginAndRegister/RegisterSocial.vue index 4166433958..1cc5e53775 100644 --- a/src/pages/LoginAndRegister/RegisterSocial.vue +++ b/src/pages/LoginAndRegister/RegisterSocial.vue @@ -127,15 +127,6 @@ 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; @@ -143,6 +134,10 @@ export default { 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() { diff --git a/src/router/routes.js b/src/router/routes.js index fc9332b143..1c62fb558e 100644 --- a/src/router/routes.js +++ b/src/router/routes.js @@ -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', diff --git a/test/unit/specs/pages/LoginAndRegister/RegisterSocial.spec.js b/test/unit/specs/pages/LoginAndRegister/RegisterSocial.spec.js index 7796ab6696..9230276467 100644 --- a/test/unit/specs/pages/LoginAndRegister/RegisterSocial.spec.js +++ b/test/unit/specs/pages/LoginAndRegister/RegisterSocial.spec.js @@ -1,9 +1,11 @@ -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'); }; @@ -11,7 +13,7 @@ const testRedirectToError = to => { // 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(); }; @@ -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 } });