-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathuser-test.js
More file actions
144 lines (129 loc) · 5.47 KB
/
user-test.js
File metadata and controls
144 lines (129 loc) · 5.47 KB
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
import { expect } from 'chai'
import { describe, it } from 'mocha'
import { contentstackClient } from '../../sanity-check/utility/ContentstackClient'
import { jsonWrite } from '../../sanity-check/utility/fileOperations/readwrite'
import axios from 'axios'
import dotenv from 'dotenv'
import * as contentstack from '../../../lib/contentstack.js'
dotenv.config()
var authtoken = ''
var loggedinUserID = ''
var client = contentstackClient()
describe('Contentstack User Session api Test', () => {
it('should check user login with wrong credentials', done => {
contentstackClient().login({ email: process.env.EMAIL, password: process.env.PASSWORD })
.then((response) => {
done()
}).catch((error) => {
const jsonMessage = JSON.parse(error.message)
const payload = JSON.parse(jsonMessage.request.data)
expect(jsonMessage.status).to.be.equal(422, 'Status code does not match')
expect(jsonMessage.errorMessage).to.not.equal(null, 'Error message not proper')
expect(jsonMessage.errorCode).to.be.equal(104, 'Error code does not match')
expect(payload.user.email).to.be.equal(process.env.EMAIL, 'Email id does not match')
expect(payload.user.password).to.be.equal('contentstack', 'Password does not match')
done()
})
})
it('should Login user', done => {
client.login({ email: process.env.EMAIL, password: process.env.PASSWORD }, { include_orgs: true, include_orgs_roles: true, include_stack_roles: true, include_user_settings: true }).then((response) => {
jsonWrite(response.user, 'loggedinuser.json')
expect(response.notice).to.be.equal('Login Successful.', 'Login success messsage does not match.')
done()
})
.catch(done)
})
it('should logout user', done => {
client.logout()
.then((response) => {
expect(axios.defaults.headers.common.authtoken).to.be.equal(undefined)
expect(response.notice).to.be.equal('You\'ve logged out successfully.')
done()
})
.catch(done)
})
it('should login with credentials', done => {
client.login({ email: process.env.EMAIL, password: process.env.PASSWORD }, { include_orgs: true, include_orgs_roles: true, include_stack_roles: true, include_user_settings: true }).then((response) => {
loggedinUserID = response.user.uid
jsonWrite(response.user, 'loggedinuser.json')
expect(response.notice).to.be.equal('Login Successful.', 'Login success messsage does not match.')
done()
})
.catch(done)
})
it('should get Current user info test', done => {
client.getUser().then((user) => {
authtoken = user.authtoken
expect(user.uid).to.be.equal(loggedinUserID)
done()
})
.catch(done)
})
it('should get user info from authtoken', done => {
contentstackClient(authtoken)
.getUser()
.then((user) => {
expect(user.uid).to.be.equal(loggedinUserID)
expect(true).to.be.equal(true)
done()
})
.catch(done)
})
it('should get host for NA region by default', done => {
const client = contentstack.client()
const baseUrl = client.axiosInstance.defaults.baseURL
expect(baseUrl).to.include('api.contentstack.io', 'region NA set correctly by default')
done()
})
it('should get host for NA region', done => {
const client = contentstack.client({ region: 'NA' })
const baseUrl = client.axiosInstance.defaults.baseURL
expect(baseUrl).to.include('api.contentstack.io', 'region NA set correctly')
done()
})
it('should get host for NA region on priority', done => {
const client = contentstack.client({ region: 'NA', host: 'dev11-api.csnonprod.com' })
const baseUrl = client.axiosInstance.defaults.baseURL
expect(baseUrl).to.include('api.contentstack.io', 'region NA set correctly with priority')
done()
})
it('should get custom host', done => {
const client = contentstack.client({ host: 'dev11-api.csnonprod.com' })
const baseUrl = client.axiosInstance.defaults.baseURL
expect(baseUrl).to.include('dev11-api.csnonprod.com', 'custom host set correctly')
done()
})
it('should get host for EU region', done => {
const client = contentstack.client({ region: 'EU' })
const baseUrl = client.axiosInstance.defaults.baseURL
expect(baseUrl).to.include('eu-api.contentstack.com', 'region EU set correctly')
done()
})
it('should get host for AU region', done => {
const client = contentstack.client({ region: 'AU' })
const baseUrl = client.axiosInstance.defaults.baseURL
expect(baseUrl).to.include('au-api.contentstack.com', 'region AU set correctly')
done()
})
it('should get host for AZURE_NA region', done => {
const client = contentstack.client({ region: 'AZURE_NA' })
const baseUrl = client.axiosInstance.defaults.baseURL
expect(baseUrl).to.include('azure-na-api.contentstack.com', 'region AZURE_NA set correctly')
done()
})
it('should get host for GCP_NA region', done => {
const client = contentstack.client({ region: 'GCP_NA' })
const baseUrl = client.axiosInstance.defaults.baseURL
expect(baseUrl).to.include('gcp-na-api.contentstack.com', 'region GCP_NA set correctly')
done()
})
it('should throw error for invalid region', done => {
try {
contentstack.client({ region: 'DUMMYREGION' })
done(new Error('Expected error was not thrown for invalid region'))
} catch (error) {
expect(error.message).to.include('Invalid region', 'Error message should indicate invalid region')
done()
}
})
})