Skip to content
This repository was archived by the owner on Nov 15, 2024. It is now read-only.

Commit b39a0dc

Browse files
committed
test: logout test
1 parent a933fef commit b39a0dc

File tree

7 files changed

+141
-109
lines changed

7 files changed

+141
-109
lines changed

.eslintrc

+10-2
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
}
2828
}
2929
},
30-
"plugins": [],
30+
"plugins": [],
3131
// add your custom rules here
3232
"rules": {
3333
"arrow-parens": [
@@ -54,5 +54,13 @@
5454
"endOfLine": "auto"
5555
}
5656
]
57-
}
57+
},
58+
"overrides": [
59+
{
60+
"files": "*.cy.ts",
61+
"rules": {
62+
"no-unused-expressions": "off"
63+
}
64+
}
65+
]
5866
}

components/AppHeader.vue

+2
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
:class="[isPopupOpen && 'nav-button--open']"
3535
aria-label="Navigation button"
3636
class="nav-button"
37+
data-testid="nav-button"
3738
@click.stop="togglePopup"
3839
>
3940
<span class="nav-button__dot nav-button__dot--1"></span>
@@ -97,6 +98,7 @@
9798
<li class="popup-menu__list">
9899
<button
99100
class="menu-text popup-menu__text popup-menu__text--red"
101+
data-testid="nav-logout-btn"
100102
@click="logout"
101103
>
102104
Logout

cypress/e2e/auth.cy.ts

+118
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
import localforage from 'localforage'
2+
import {
3+
FAKE_AUTH_KEY,
4+
FAKE_BRIGHT_ID,
5+
FAKE_BRIGHT_ID_PASSWORD,
6+
FAKE_USER_EXPLORER_CODE,
7+
LOCAL_FORAGE_DATA,
8+
} from '../utils/data'
9+
import { TOAST_ERROR } from '../../utils/constants'
10+
11+
describe('Auth', () => {
12+
beforeEach(() => {
13+
localforage.config({ storeName: 'nuxtLocalForage', name: 'nuxtJS' })
14+
localforage.removeItem('profileData')
15+
cy.on('window:before:load', win => {
16+
cy.spy(win.console, 'error').as('spyWinConsoleError')
17+
cy.spy(win.console, 'warn').as('spyWinConsoleWarn')
18+
})
19+
// @ts-ignore
20+
cy.blockApiRequests()
21+
// @ts-ignore
22+
cy.profileIntercepts()
23+
})
24+
25+
afterEach(() => {
26+
cy.get('@spyWinConsoleError').should('have.callCount', 0)
27+
cy.get('@spyWinConsoleWarn').should('have.callCount', 0)
28+
})
29+
30+
function doLogin() {
31+
cy.visit('/', {
32+
onBeforeLoad: function (window) {
33+
window.localStorage.removeItem('authKey')
34+
window.localStorage.removeItem('brightId')
35+
window.localStorage.removeItem('publicKey')
36+
window.localStorage.removeItem('privateKey')
37+
},
38+
})
39+
cy.get('[data-testid=login-explorer-code]').type(FAKE_USER_EXPLORER_CODE)
40+
cy.get('[data-testid=login-password]').type(FAKE_BRIGHT_ID_PASSWORD)
41+
cy.get('[data-testid=login-submit]').click()
42+
}
43+
44+
function doLoginSuccess() {
45+
cy.intercept(
46+
{
47+
url: `/v1/connect/explorer-code`,
48+
method: 'POST',
49+
},
50+
{
51+
body: 'OK',
52+
}
53+
).as('explorerCode')
54+
doLogin()
55+
}
56+
57+
it('login', () => {
58+
doLoginSuccess()
59+
cy.wait('@explorerCode')
60+
.its('request.body')
61+
.should(body => {
62+
expect(body.brightId).to.eq(FAKE_BRIGHT_ID)
63+
expect(body.password).to.eq(FAKE_BRIGHT_ID_PASSWORD)
64+
expect(body.key).to.eq(FAKE_AUTH_KEY)
65+
expect(body.publicKey).to.be.not.null
66+
})
67+
.then(() => {
68+
expect(localStorage.getItem('brightId')).to.eq(FAKE_BRIGHT_ID)
69+
expect(localStorage.getItem('authKey')).to.eq(FAKE_AUTH_KEY)
70+
expect(localStorage.getItem('publicKey')).to.be.not.null
71+
expect(localStorage.getItem('privateKey')).to.be.not.null
72+
})
73+
cy.url()
74+
.should('include', `/profile/${FAKE_BRIGHT_ID}`)
75+
.then(async () => {
76+
localforage.config({ storeName: 'nuxtLocalForage', name: 'nuxtJS' })
77+
const data = await localforage.getItem('profileData')
78+
expect(data).to.deep.eq(LOCAL_FORAGE_DATA)
79+
})
80+
// stays logged in
81+
cy.visit('/')
82+
cy.url().should('include', `/profile/${FAKE_BRIGHT_ID}`)
83+
})
84+
85+
async function isLoggedOut() {
86+
cy.url().should('eq', Cypress.config().baseUrl + `/`)
87+
expect(localStorage.getItem('brightId')).to.be.null
88+
expect(localStorage.getItem('authKey')).to.be.null
89+
expect(localStorage.getItem('publicKey')).to.be.null
90+
expect(localStorage.getItem('privateKey')).to.be.null
91+
localforage.config({ storeName: 'nuxtLocalForage', name: 'nuxtJS' })
92+
const data = await localforage.getItem('profileData')
93+
expect(data).to.be.null
94+
}
95+
96+
it('handle login failed response', () => {
97+
cy.intercept(
98+
{
99+
url: `/v1/connect/explorer-code`,
100+
method: 'POST',
101+
},
102+
{
103+
statusCode: 500,
104+
}
105+
).as('explorerCodeError')
106+
doLogin()
107+
cy.url().should('not.include', `/profile/${FAKE_BRIGHT_ID}`)
108+
cy.get(`.toast--${TOAST_ERROR}`).then(isLoggedOut)
109+
})
110+
111+
it('logout', () => {
112+
doLoginSuccess()
113+
cy.get('[data-testid=nav-button]').click()
114+
cy.get('[data-testid=nav-logout-btn]').click().then(isLoggedOut)
115+
// stays logged out
116+
cy.visit('/').then(isLoggedOut)
117+
})
118+
})

cypress/e2e/login.cy.ts

-81
This file was deleted.

cypress/support/commands.js

-13
Original file line numberDiff line numberDiff line change
@@ -152,18 +152,5 @@ Cypress.Commands.add('setupProfile', () => {
152152
window.localStorage.setItem('brightId', FAKE_BRIGHT_ID)
153153
window.localStorage.setItem('publicKey', FAKE_PUBLIC_KEY)
154154
window.localStorage.setItem('privateKey', FAKE_PRIVATE_KEY)
155-
window.localStorage.setItem('isAuth', '{"value":true}')
156-
})
157-
})
158-
159-
Cypress.Commands.add('clearProfile', () => {
160-
localforage.config({ storeName: 'nuxtLocalForage', name: 'nuxtJS' })
161-
localforage.removeItem('profileData')
162-
cy.on('window:before:load', _win => {
163-
window.localStorage.removeItem('authKey')
164-
window.localStorage.removeItem('brightId')
165-
window.localStorage.removeItem('publicKey')
166-
window.localStorage.removeItem('privateKey')
167-
window.localStorage.removeItem('isAuth')
168155
})
169156
})

layouts/default.vue

+11-12
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
<template>
22
<div id="app">
3-
<app-toast />
4-
<ui-loader />
5-
<app-header />
6-
<Nuxt />
3+
<app-toast/>
4+
<ui-loader/>
5+
<app-header/>
6+
<Nuxt/>
77
</div>
88
</template>
99

@@ -13,26 +13,25 @@ import AppHeader from '~/components/AppHeader.vue'
1313
import AppToast from '~/components/AppToast.vue'
1414
1515
export default {
16-
components: { UiLoader, AppHeader, AppToast },
16+
components: {UiLoader, AppHeader, AppToast},
1717
1818
async mounted() {
19-
const { default: supportsWebP } = await import('supports-webp')
19+
const {default: supportsWebP} = await import('supports-webp')
2020
2121
if (await supportsWebP) {
2222
this.$store.commit('app/setIsWebp', true)
2323
} else {
2424
this.$store.commit('app/setIsWebp', false)
2525
}
2626
27-
const { winSizes } = await import('~/scripts/utils/winSizes')
28-
const { resize } = await import('@emotionagency/utils')
27+
const {winSizes} = await import('~/scripts/utils/winSizes')
28+
const {resize} = await import('@emotionagency/utils')
2929
resize.on(winSizes)
3030
31-
const isAuth = JSON.parse(localStorage.getItem('isAuth') || '[]')
31+
const brightId = localStorage.getItem('brightId')
3232
33-
if (isAuth.value) {
33+
if (brightId) {
3434
this.$store.commit('app/setIsAuth', true)
35-
const brightId = localStorage.getItem('brightId')
3635
if (this.$route.name === 'index') {
3736
this.$router.push('/profile/' + brightId)
3837
}
@@ -45,4 +44,4 @@ export default {
4544
}
4645
},
4746
}
48-
</script>
47+
</script>

store/app.ts

-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,5 @@ export const mutations: MutationTree<AppState> = {
1616
},
1717
setIsAuth(state, value) {
1818
state.isAuth = value
19-
localStorage.setItem('isAuth', JSON.stringify({ value }))
2019
},
2120
}

0 commit comments

Comments
 (0)