Skip to content
Open
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
27 changes: 18 additions & 9 deletions src/components/Pages/EmailVerification.vue
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
<v-card-text>
{{state.message}}
</v-card-text>
<v-card-text v-if="showLoginLink">
<router-link to="/login">Log in to your account.</router-link>
</v-card-text>
</v-card>
</v-main>
</template>
Expand All @@ -22,23 +25,29 @@ const STATES = Object.freeze({

export default {
name: 'EmailVerification',
computed: {},
computed: {
showLoginLink () {
return this.state === STATES.VERIFIED && !this.$store.getters.isLoggedIn
},
},
data () {
return {
state: this.$store.getters.currentUser.verified ? STATES.VERIFIED : STATES.PENDING,
state: STATES.PENDING,
}
},
created () {
if (!this.$store.getters.currentUser.verified) {
this.$api.verifyEmail({ token: this.$route.params.token })
.then(message => this.success())
.catch(expired => this.fail(expired))
}
this.$api.verifyEmail({ token: this.$route.params.token })
.then(() => this.success())
.catch(expired => this.fail(expired))
},
methods: {
success (message) {
success () {
this.state = STATES.VERIFIED
this.$store.dispatch('markAsVerified', {})
if (this.$store.getters.isLoggedIn) {
this.$api.checkVerified()
.then(verified => verified && this.$store.dispatch('markAsVerified'))
.catch(err => console.error(err))
}
},
fail (expired) {
if (expired) {
Expand Down
3 changes: 1 addition & 2 deletions src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,7 @@ enableMocking().then(() => {
return new Promise(function (resolve, reject) {
// Unauthenticated. is the exact error message returned by the API for the auth middle ware
// which is why we check for that message here...
if (err.response.config && !err.response.config.__isRetryRequest && err.response.data && err.response.data.error && err.response.data.error === 'Unauthenticated.') {
// TODO this IF should also have a condition for is logged in....
if (store.getters.isLoggedIn && err.response.config && !err.response.config.__isRetryRequest && err.response.data && err.response.data.error && err.response.data.error === 'Unauthenticated.') {
console.log('Detected logged out state, so logging out...')
store
.dispatch('logout')
Expand Down
5 changes: 0 additions & 5 deletions src/router/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,11 +128,6 @@ const router = new Router({
path: '/emailVerification/:token',
name: 'EmailVerification',
component: EmailVerification,
meta: {
requiresAuth: {
excludeFromPolicyChecks: true,
},
},
},
{
path: '/user',
Expand Down
73 changes: 73 additions & 0 deletions tests/unit/email-verification.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import { shallowMount } from '@vue/test-utils'
import EmailVerification from '@/components/Pages/EmailVerification.vue'

const flushPromises = () => new Promise(resolve => setTimeout(resolve))

const mountEmailVerification = ({ isLoggedIn = false, verifyEmail, checkVerified } = {}) => {
return shallowMount(EmailVerification, {
mocks: {
$api: {
verifyEmail: verifyEmail || jest.fn().mockResolvedValue(),
checkVerified: checkVerified || jest.fn().mockResolvedValue(true),
},
$route: {
params: {
token: 'email-verification-token',
},
},
$store: {
getters: {
isLoggedIn,
},
dispatch: jest.fn(),
},
},
stubs: {
'v-main': true,
'v-card': true,
'v-toolbar': true,
'v-toolbar-title': true,
'v-card-text': true,
'router-link': {
props: ['to'],
template: '<a :href="to"><slot /></a>',
},
},
})
}

describe('EmailVerification.vue', () => {
it('verifies a token without a login and offers a login link', async () => {
const verifyEmail = jest.fn().mockResolvedValue()
const wrapper = mountEmailVerification({ verifyEmail })

await flushPromises()

expect(verifyEmail).toHaveBeenCalledWith({ token: 'email-verification-token' })
expect(wrapper.text()).toContain('Email verified!')
expect(wrapper.find('a').attributes('href')).toBe('/login')
})

it('does not offer a login link to an existing authenticated session', async () => {
const checkVerified = jest.fn().mockResolvedValue(false)
const wrapper = mountEmailVerification({ isLoggedIn: true, checkVerified })

await flushPromises()

expect(wrapper.text()).toContain('Email verified!')
expect(wrapper.find('a').exists()).toBe(false)
expect(checkVerified).toHaveBeenCalled()
expect(wrapper.vm.$store.dispatch).not.toHaveBeenCalled()
})

it('does not offer a login link when the token cannot be verified', async () => {
const wrapper = mountEmailVerification({
verifyEmail: jest.fn().mockRejectedValue(true),
})

await flushPromises()

expect(wrapper.text()).toContain('Verification token expired, or you are already verified!')
expect(wrapper.find('a').exists()).toBe(false)
})
})
Loading