• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

suculent / thinx-device-api / #252646133

07 May 2025 05:29PM UTC coverage: 55.702% (-16.3%) from 72.01%
#252646133

push

suculent
updated connect-redis

1249 of 2805 branches covered (44.53%)

Branch coverage included in aggregate %.

1 of 1 new or added line in 1 file covered. (100.0%)

1839 existing lines in 30 files now uncovered.

5365 of 9069 relevant lines covered (59.16%)

5.4 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

30.16
/spec/jasmine/ZZ-RouterAPIKeySpec.js
1
/* Router integration test only; does not have to cover full unit functionality. */
2

3
const THiNX = require("../../thinx-core.js");
1✔
4

5
let chai = require('chai');
1✔
6
var expect = require('chai').expect;
1✔
7
let chaiHttp = require('chai-http');
1✔
8
chai.use(chaiHttp);
1✔
9

10
//
11
// Unauthenticated
12
//
13

14
let thx;
15
let agent;
16
let jwt;
17

18
describe("API Keys (noauth)", function () {
1✔
19

20
    beforeAll((done) => {
1✔
21
        console.log(`🚸 [chai] >>> running API Keys (noauth) spec`);
1✔
22
        thx = new THiNX();
1✔
23
        thx.init(() => {
1✔
UNCOV
24
            agent = chai.request.agent(thx.app);
×
UNCOV
25
            agent
×
26
                .post('/api/login')
27
                .send({ username: 'dynamic', password: 'dynamic', remember: false })
28
                .catch((e) => { console.log(e); })
×
29
                .then(function (res) {
UNCOV
30
                    console.log(`🚸 [chai] beforeAll POST /api/login (valid) response: ${res}`);
×
UNCOV
31
                    expect(res).to.have.cookie('x-thx-core');
×
UNCOV
32
                    let body = JSON.parse(res.text);
×
UNCOV
33
                    jwt = 'Bearer ' + body.access_token;
×
UNCOV
34
                    done();
×
35
                });
36
        });
37
    });
38

39

40

41
    // create
42
    it("POST /api/user/apikey", function (done) {
1✔
UNCOV
43
        chai.request(thx.app)
×
44
            .post('/api/user/apikey')
45
            .send({
46
                'alias': 'mock-apikey-alias'
47
            })
48
            .end((err, res) => {
UNCOV
49
                expect(res.status).to.equal(401);
×
UNCOV
50
                done();
×
51
            });
52
    }, 30000);
53

54
    // revoke
55
    it("POST /api/user/apikey/revoke", function (done) {
1✔
UNCOV
56
        chai.request(thx.app)
×
57
            .post('/api/user/apikey/revoke')
58
            .send({
59
                'alias': 'mock-apikey-alias'
60
            })
61
            .end((err, res) => {
UNCOV
62
                expect(res.status).to.equal(401);
×
UNCOV
63
                done();
×
64
            });
65
    }, 30000);
66

67
    // list
68
    it("GET /api/user/apikey/list", function (done) {
1✔
UNCOV
69
        chai.request(thx.app)
×
70
            .get('/api/user/apikey/list')
71
            .end((err, res) => {
UNCOV
72
                expect(res.status).to.equal(401);
×
UNCOV
73
                done();
×
74
            });
75
    }, 30000);
76
});
77

78
//
79
// Authenticated
80
//
81

82

83
describe("API Keys (JWT)", function () {
1✔
84

85
    afterAll((done) => {
1✔
86
        agent.close();
1✔
UNCOV
87
        console.log(`🚸 [chai] <<< completed API Keys (noauth) spec`);
×
UNCOV
88
        done();
×
89
    });
90

91
    var created_api_key = null;
1✔
92
    var created_api_key_2 = null;
1✔
93

94
    // create
95
    it("POST /api/user/apikey (1)", function (done) {
1✔
96
        chai.request(thx.app)
1✔
97
            .post('/api/user/apikey')
98
            .set('Authorization', jwt)
99
            .send({
100
                'alias': 'mock-apikey-alias'
101
            })
102
            .end((err, res) => {
103
                //  {"success":true,"api_key":"9b7bd4f4eacf63d8453b32dbe982eea1fb8bbc4fc8e3bcccf2fc998f96138629","hash":"0a920b2e99a917a04d7961a28b49d05524d10cd8bdc2356c026cfc1c280ca22c"}
UNCOV
104
                expect(res.status).to.equal(200);
×
UNCOV
105
                let j = JSON.parse(res.text);
×
UNCOV
106
                expect(j.success).to.equal(true);
×
UNCOV
107
                expect(j.response.api_key).to.be.a('string');
×
UNCOV
108
                expect(j.response.hash).to.be.a('string');
×
UNCOV
109
                created_api_key = j.response.hash;
×
UNCOV
110
                console.log("[spec] saving apikey (1)", j.response.api_key);
×
UNCOV
111
                done();
×
112
            });
113
    }, 30000);
114

115
    it("POST /api/user/apikey (2)", function (done) {
1✔
116
        chai.request(thx.app)
1✔
117
            .post('/api/user/apikey')
118
            .set('Authorization', jwt)
119
            .send({
120
                'alias': 'mock-apikey-alias-2'
121
            })
122
            .end((err, res) => {
UNCOV
123
                expect(res.status).to.equal(200);
×
UNCOV
124
                let j = JSON.parse(res.text);
×
UNCOV
125
                expect(j.success).to.equal(true);
×
UNCOV
126
                expect(j.response.api_key).to.be.a('string');
×
UNCOV
127
                expect(j.response.hash).to.be.a('string');
×
UNCOV
128
                console.log("[spec] saving apikey (2)", j.hash);
×
UNCOV
129
                created_api_key_2 = j.hash;
×
UNCOV
130
                done();
×
131
            });
132
    }, 30000);
133

134
    it("POST /api/user/apikey (3)", function (done) {
1✔
135
        chai.request(thx.app)
1✔
136
            .post('/api/user/apikey')
137
            .set('Authorization', jwt)
138
            .send({
139
                'alias': 'mock-apikey-alias-3'
140
            })
141
            .end((err, res) => {
UNCOV
142
                expect(res.status).to.equal(200);
×
UNCOV
143
                let j = JSON.parse(res.text);
×
UNCOV
144
                expect(j.success).to.equal(true);
×
UNCOV
145
                expect(j.response.api_key).to.be.a('string');
×
UNCOV
146
                expect(j.response.hash).to.be.a('string');
×
UNCOV
147
                done();
×
148
            });
149
    }, 30000);
150

151
    // revoke
152
    it("POST /api/user/apikey/revoke (single)", function (done) {
1✔
153
        expect(created_api_key).not.to.be.null;
1✔
UNCOV
154
        chai.request(thx.app)
×
155
            .post('/api/user/apikey/revoke')
156
            .set('Authorization', jwt)
157
            .send({
158
                'fingerprint': created_api_key
159
            })
160
            .end((err, res) => {
UNCOV
161
                expect(res.status).to.equal(200);
×
UNCOV
162
                let j = JSON.parse(res.text);
×
UNCOV
163
                expect(j.success).to.equal(true);
×
UNCOV
164
                expect(j.response).to.be.an('array');
×
UNCOV
165
                console.log(`🚸 [chai] API Keys in revocation:", ${JSON.stringify(j)} from res ${res.text}`);
×
166
                //expect(aks.length >= 1);
UNCOV
167
                done();
×
168
            });
169
    }, 30000);
170

171
    it("POST /api/user/apikey/revoke (multiple, fault)", function (done) {
1✔
172
        expect(created_api_key_2).not.to.be.null;
1✔
UNCOV
173
        chai.request(thx.app)
×
174
            .post('/api/user/apikey/revoke')
175
            .set('Authorization', jwt)
176
            .send({
177
                'fingerprints': created_api_key_2
178
            })
179
            .end((err, res) => {
180
                //  {"revoked":["7663ca65a23d759485fa158641727597256fd7eac960941fbb861ab433ab056f"],"success":true}
UNCOV
181
                console.log(`🚸 [chai] POST /api/user/apikey/revoke (multiple) response: ${res.text}, status ${res.status}`);
×
UNCOV
182
                expect(res.status).to.equal(200);
×
UNCOV
183
                let j = JSON.parse(res.text);
×
UNCOV
184
                expect(j.success).to.equal(true);
×
UNCOV
185
                expect(j.response).to.be.an('array');
×
UNCOV
186
                expect(j.response.length).to.equal(0);
×
UNCOV
187
                done();
×
188
            });
189
    }, 30000);
190

191
    it("POST /api/user/apikey/revoke (multiple)", function (done) {
1✔
192
        expect(created_api_key_2).not.to.be.null;
1✔
UNCOV
193
        chai.request(thx.app)
×
194
            .post('/api/user/apikey/revoke')
195
            .set('Authorization', jwt)
196
            .send({
197
                'fingerprints': [created_api_key_2]
198
            })
199
            .end((err, res) => {
200
                //  {"revoked":["7663ca65a23d759485fa158641727597256fd7eac960941fbb861ab433ab056f"],"success":true}
UNCOV
201
                console.log(`🚸 [chai] POST /api/user/apikey/revoke (multiple) response: ${res.text}, status ${res.status}`);
×
UNCOV
202
                expect(res.status).to.equal(200);
×
UNCOV
203
                let j = JSON.parse(res.text);
×
UNCOV
204
                expect(j.success).to.equal(true);
×
UNCOV
205
                expect(j.response).to.be.an('array');
×
206
                // TODO: fixme: does not delete anything... expect(j.revoked.length).to.equal(1);
UNCOV
207
                done();
×
208
            });
209
    }, 30000);
210

211
    // list
212
    it("GET /api/user/apikey/list", function (done) {
1✔
213
        chai.request(thx.app)
1✔
214
            .get('/api/user/apikey/list')
215
            .set('Authorization', jwt)
216
            .end((err, res) => {
UNCOV
217
                expect(res.status).to.equal(200);
×
UNCOV
218
                let j = JSON.parse(res.text);
×
UNCOV
219
                expect(j.success).to.equal(true);
×
UNCOV
220
                expect(j.response).to.be.an('array');
×
UNCOV
221
                expect(j.response.length >= 1);
×
UNCOV
222
                done();
×
223
            });
224
    }, 30000);
225

226
    // API v2
227

228
    it("POST /api/v2/apikey", function (done) {
1✔
229
        chai.request(thx.app)
1✔
230
            .post('/api/v2/apikey')
231
            .set('Authorization', jwt)
232
            .send({
233
                'alias': 'mock-apikey-alias-4'
234
            })
235
            .end((err, res) => {
UNCOV
236
                expect(res.status).to.equal(200);
×
UNCOV
237
                let j = JSON.parse(res.text);
×
UNCOV
238
                expect(j.success).to.equal(true);
×
UNCOV
239
                expect(j.response.api_key).to.be.a('string');
×
UNCOV
240
                expect(j.response.hash).to.be.a('string');
×
UNCOV
241
                done();
×
242
            });
243
    }, 30000);
244

245
    it("GET /api/v2/apikey", function (done) {
1✔
246
        chai.request(thx.app)
1✔
247
            .get('/api/v2/apikey')
248
            .set('Authorization', jwt)
249
            .end((err, res) => {
UNCOV
250
                expect(res.status).to.equal(200);
×
UNCOV
251
                let j = JSON.parse(res.text);
×
UNCOV
252
                expect(j.success).to.equal(true);
×
UNCOV
253
                expect(j.response).to.be.an('array');
×
UNCOV
254
                expect(j.response.length >= 1);
×
UNCOV
255
                done();
×
256
            });
257
    }, 30000);
258

259
    it("DELETE /api/v2/apikey", function (done) {
1✔
260
        expect(created_api_key).not.to.be.null;
1✔
UNCOV
261
        chai.request(thx.app)
×
262
            .delete('/api/v2/apikey')
263
            .set('Authorization', jwt)
264
            .send({
265
                'fingerprint': 'mock-apikey-alias-4'
266
            })
267
            .end((err, res) => {
UNCOV
268
                expect(res.status).to.equal(200);
×
UNCOV
269
                let j = JSON.parse(res.text);
×
UNCOV
270
                expect(j.success).to.equal(true);
×
UNCOV
271
                expect(j.response).to.be.an('array');
×
UNCOV
272
                console.log(`🚸 [chai] API Keys in V2 revocation:", ${JSON.stringify(j)} from res ${res.text}`);
×
273
                //expect(aks.length >= 1);
UNCOV
274
                done();
×
275
            });
276
    }, 30000);
277

278
});
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc