forked from electricimp/imp-central-impt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauth_user_pwd.spec.js
179 lines (160 loc) · 7.75 KB
/
auth_user_pwd.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
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
// MIT License
//
// Copyright 2018 Electric Imp
//
// SPDX-License-Identifier: MIT
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO
// EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES
// OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
'use strict';
require('jasmine-expect');
const FS = require('fs');
const config = require('../config');
const ImptTestingHelper = require('../ImptTestingHelper');
const DEFAULT_ENDPOINT = 'https://api.electricimp.com/v5';
// Test suite for 'impt auth login --user <user_id> --pwd <password>', 'impt auth logout', 'impt auth info' commands.
// Runs impt auth commands with different combinations of options.
describe('impt auth by user/password test suite >', () => {
beforeAll((done) => {
ImptTestingHelper.init(false).
then(testSuiteInit).
then(done).
catch(error => done.fail(error));
}, ImptTestingHelper.TIMEOUT);
afterAll((done) => {
ImptTestingHelper.cleanUp().
then(done).
catch(error => done.fail(error));
}, ImptTestingHelper.TIMEOUT);
function testSuiteInit() {
return ImptTestingHelper.runCommand('impt auth logout', ImptTestingHelper.emptyCheck);
}
// negative test - run 'impt auth info' command without login
it('auth info without login', (done) => {
ImptTestingHelper.runCommand('impt auth info', ImptTestingHelper.checkFailStatus).
then(done).
catch(error => done.fail(error));
});
// negative test - run 'impt auth logout' command without login
it('global logout without login', (done) => {
ImptTestingHelper.runCommand('impt auth logout', ImptTestingHelper.checkFailStatus).
then(done).
catch(error => done.fail(error));
});
it('global login', (done) => {
const endpoint = config.apiEndpoint ? `--endpoint ${config.apiEndpoint}` : '';
ImptTestingHelper.runCommand(
`impt auth login --user ${config.email} --pwd ${config.password} ${endpoint}`,
ImptTestingHelper.checkSuccessStatus).
then(done).
catch(error => done.fail(error));
});
it('check global auth info', (done) => {
ImptTestingHelper.runCommand('impt auth info', (commandOut) => {
expect(commandOut).toMatch('Global');
expect(commandOut).toMatch(config.email);
ImptTestingHelper.checkSuccessStatus(commandOut);
}).
then(done).
catch(error => done.fail(error));
});
it('repeated global temp login', (done) => {
const endpoint = config.apiEndpoint ? `-e ${config.apiEndpoint}` : '';
ImptTestingHelper.runCommand(
`impt auth login -u ${config.email} -w ${config.password} ${endpoint} --temp --confirmed --debug`,
ImptTestingHelper.checkSuccessStatus).
then(done).
catch(error => done.fail(error));
});
it('check global temp auth info', (done) => {
ImptTestingHelper.runCommand('impt auth info', (commandOut) => {
expect(commandOut).toMatch('Global');
expect(commandOut).toMatch(config.email);
ImptTestingHelper.checkAttribute(commandOut, 'auto refresh', 'false');
ImptTestingHelper.checkSuccessStatus(commandOut);
}).
then(done).
catch(error => done.fail(error));
});
// negative test - run local logout command without login
it('local logout without login', (done) => {
ImptTestingHelper.runCommand('impt auth logout --local', ImptTestingHelper.checkFailStatus).
then(done).
catch(error => done.fail(error));
});
it('local login', (done) => {
const endpoint = config.apiEndpoint ? `--endpoint ${config.apiEndpoint}` : `--endpoint ${DEFAULT_ENDPOINT}`;
ImptTestingHelper.runCommand(
`impt auth login --local --user ${config.email} --pwd ${config.password} ${endpoint}`,
ImptTestingHelper.checkSuccessStatus).
then(done).
catch(error => done.fail(error));;
});
it('check local auth info', (done) => {
ImptTestingHelper.runCommand('impt auth info', (commandOut) => {
expect(commandOut).toMatch('Local');
expect(commandOut).toMatch(config.email);
ImptTestingHelper.checkSuccessStatus(commandOut);
}).
then(done).
catch(error => done.fail(error));
});
it('repeated local temp login', (done) => {
const endpoint = config.apiEndpoint ? `-e ${config.apiEndpoint}` : `-e ${DEFAULT_ENDPOINT}`;
ImptTestingHelper.runCommand(`impt auth login -l -u ${config.email} -w ${config.password} ${endpoint} -q -t -z`, (commandOut) => {
// login --temp must not save refreshToken and loginKey in the auth config file
const authFileName = ImptTestingHelper.TESTS_EXECUTION_FOLDER + '/.impt.auth';
if (FS.existsSync(authFileName)) {
try {
const json = JSON.parse(FS.readFileSync(authFileName).toString());
expect(json.refreshToken).toBeUndefined();
expect(json.loginKey).toBeUndefined();
} catch (error) {
done.fail('Local auth file reading failed: ' + error);
}
}
else {
done.fail('Local auth file not found');
}
ImptTestingHelper.checkSuccessStatus(commandOut);
}).
then(done).
catch(error => done.fail(error));
});
it('global logout', (done) => {
ImptTestingHelper.runCommand('impt auth logout', ImptTestingHelper.checkSuccessStatus).
then(done).
catch(error => done.fail(error));
});
it('local logout', (done) => {
ImptTestingHelper.runCommand('impt auth logout -l', ImptTestingHelper.checkSuccessStatus).
then(done).
catch(error => done.fail(error));
});
// negative tests
it('login without user/password', (done) => {
ImptTestingHelper.runCommand('impt auth login', ImptTestingHelper.checkFailStatus).
then(() => ImptTestingHelper.runCommand('impt auth login --local', ImptTestingHelper.checkFailStatus)).
then(() => ImptTestingHelper.runCommand('impt auth login -l -u', ImptTestingHelper.checkFailStatus)).
then(() => ImptTestingHelper.runCommand('impt auth login -l -w', ImptTestingHelper.checkFailStatus)).
then(() => ImptTestingHelper.runCommand(`impt auth login -l -u ${config.email}`, ImptTestingHelper.checkFailStatus)).
then(() => ImptTestingHelper.runCommand(`impt auth login -l -w ${config.password}`, ImptTestingHelper.checkFailStatus)).
then(done).
catch(error => done.fail(error));
});
});