-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapi-keys.js
221 lines (198 loc) · 7 KB
/
api-keys.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
const utils = require("./assets/utils");
describe(utils.suiteName(__filename), function () {
const APIKeys = utils.reqSrc("api-keys");
const APIKey = utils.reqSrc("procedures/utils/api-key");
const CloudClient = require("./assets/mock-cloud-client");
const assert = require("assert");
const groupId = "g1";
const username = `user_api_key_test`;
const groups = [{ id: groupId, owner: username }];
beforeEach(() =>
cloudClient = CloudClient.builder()
.withGroups(groups)
.build()
);
it("should create user keys", async function () {
const { username, type, value } = newKey();
const apiKeys = new APIKeys(cloudClient);
await apiKeys.create(username, type, value);
const keys = await apiKeys.list(username);
const key = keys.find((key) => key.provider === type);
assert(key, `new key (${type}) not found`);
});
it("should create group keys", async function () {
const { username, type, value } = newKey();
const apiKeys = new APIKeys(cloudClient);
await apiKeys.create(username, type, value, groupId);
const groupKeys = await apiKeys.list(username, groupId);
const key = groupKeys.find((key) => key.provider === type);
assert(key, `new key (${type}) not found`);
});
it("should get API keys", async function () {
const { username, type, value } = newKey();
const apiKeys = new APIKeys(cloudClient);
await apiKeys.create(username, type, value);
const apiKey = APIKey.GoogleMapsKey;
const key = await apiKeys.get(username, apiKey);
assert.notEqual(key.value, apiKey.value);
});
it("should delete user keys", async function () {
const { username, type, value } = newKey();
const apiKeys = new APIKeys(cloudClient);
await apiKeys.create(username, type, value);
await apiKeys.delete(username, type);
const keys = await apiKeys.list(username);
const key = keys.find((key) => key.provider === type);
assert(!key, `new key (${type}) not deleted`);
});
it("should delete group keys", async function () {
const { username, type, value } = newKey();
const apiKeys = new APIKeys(cloudClient);
await apiKeys.create(username, type, value, groupId);
await apiKeys.delete(username, type, groupId);
const keys = await apiKeys.list(username, id);
const key = keys.find((key) => key.provider === type);
assert(!key, `key not removed from each group`);
});
it("should throw error if not-group owner", async function () {
const { username, type, value } = newKey();
const apiKeys = new APIKeys(cloudClient);
const otherUser = "someUser";
await assert.rejects(apiKeys.create(otherUser, type, value, groupId));
});
let id = 1;
function newKey() {
return {
username,
type: "Google Maps",
value: `value_${id++}`,
};
}
describe("router", function () {
const axios = require("axios");
const routerUtils = require("../src/procedures/utils/router-utils");
const port = process.env.PORT || 8934;
let app;
beforeEach(() => {
const userData = {};
userData[username] = { apiKeys: { "Google Maps": "abc" } };
const groupData = {};
groupData[groupId] = { apiKeys: { "Pixabay": "def" } };
cloudClient = CloudClient.builder()
.withGroups(groups)
.withUserSettings(userData)
.withGroupSettings(groupData)
.build();
apiKeys = new APIKeys(cloudClient);
const router = apiKeys.router();
const server = require("express")();
server.use(routerUtils.json());
// Add a middleware for setting the username in the tests
server.use((req, _res, next) => {
req.session = {
username: req.query.username,
};
next();
});
server.use(router);
app = server.listen(port);
});
afterEach(() => app.close());
function url(path) {
return `http://localhost:${port}` + path;
}
it("should list API providers", async function () {
const resp = await axios.get(url("/providers"));
assert.equal(resp.status, 200);
assert(Array.isArray(resp.data));
assert(resp.data.every((k) => k.provider && k.url));
});
it("should create user key", async function () {
const prov = "Data.gov";
const value = "abcdef";
const resp = await axios.post(
url(
`/${encodeURIComponent(prov)}?username=${username}`,
),
{ value },
);
assert.equal(resp.status, 200);
const keys = await apiKeys.list(username);
assert.equal(keys.length, 2);
});
it("should create group key", async function () {
const prov = "Data.gov";
const value = "abcdef123";
const resp = await axios.post(
url(
`/${encodeURIComponent(prov)}?username=${username}&group=${groupId}`,
),
{ value },
);
assert.equal(resp.status, 200);
const keys = await apiKeys.list(username, groupId);
assert.equal(keys.length, 2);
});
it("should return error if non-owner sets group key", async function () {
try {
const prov = "Data.gov";
const value = "abcdef123";
const resp = await axios.post(
url(
`/${encodeURIComponent(prov)}?username=otherUser&group=${groupId}`,
),
{ value },
);
} catch (err) {
assert.equal(err.response.status, 403);
}
});
it("should list user keys", async function () {
const resp = await axios.get(url(`/?username=${username}`));
assert(Array.isArray(resp.data));
assert.equal(resp.data.length, 1);
assert.equal(resp.data[0].provider, "Google Maps");
});
it("should list group keys", async function () {
const resp = await axios.get(
url(`/?username=${username}&group=${groupId}`),
);
assert(Array.isArray(resp.data));
assert.equal(resp.data.length, 1);
assert.equal(resp.data[0].provider, "Pixabay");
});
it("should delete user keys", async function () {
const prov = "Google Maps";
const resp = await axios.delete(
url(`/${encodeURIComponent(prov)}?username=${username}`),
);
assert.equal(resp.status, 200);
const keys = await apiKeys.list(username);
assert.equal(keys.length, 0);
});
it("should delete group keys", async function () {
const prov = "Pixabay";
const resp = await axios.delete(
url(
`/${encodeURIComponent(prov)}?username=${username}&group=${groupId}`,
),
);
assert.equal(resp.status, 200);
const keys = await apiKeys.list(username, groupId);
assert.equal(keys.length, 0);
});
it("should return error if non-owner deletes group key", async function () {
const prov = "Pixabay";
try {
await axios.delete(
url(
`/${encodeURIComponent(prov)}?username=otherUser&group=${groupId}`,
),
);
assert(false, "Request returned successful status code");
} catch (err) {
assert.equal(err.response.status, 403);
}
});
});
});