forked from cypress-io/cypress-example-recipes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
spec.js
59 lines (53 loc) · 1.36 KB
/
spec.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/// <reference types="cypress" />
// login just once using API
let user
before(function fetchUser () {
cy.request('POST', 'http://localhost:4000/users/authenticate', {
username: Cypress.env('username'),
password: Cypress.env('password'),
})
.its('body')
.then((res) => {
user = res
})
})
// but set the user before visiting the page
// so the app thinks it is already authenticated
beforeEach(function setUser () {
cy.visit('/', {
onBeforeLoad (win) {
// and before the page finishes loading
// set the user object in local storage
win.localStorage.setItem('user', JSON.stringify(user))
},
})
// the page should be opened and the user should be logged in
})
describe('JWT', () => {
it('makes authenticated request', () => {
// we can make authenticated request ourselves
// since we know the token
cy.request({
url: 'http://localhost:4000/users',
auth: {
bearer: user.token,
},
})
.its('body')
.should('deep.equal', [
{
id: 1,
username: 'test',
firstName: 'Test',
lastName: 'User',
},
])
})
it('is logged in', () => {
cy.contains('Hi Test!').should('be.visible')
})
it('shows loaded user', () => {
// this user information came from authenticated XHR call
cy.contains('li', 'Test User').should('be.visible')
})
})