-
Notifications
You must be signed in to change notification settings - Fork 320
Expand file tree
/
Copy pathaccount_spec.js
More file actions
263 lines (224 loc) · 9.04 KB
/
account_spec.js
File metadata and controls
263 lines (224 loc) · 9.04 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
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
const cloudinary = require("../../../../cloudinary");
const TIMEOUT = require('../../../testUtils/testConstants').TIMEOUT;
let runOnlyForInternalPRs = process.env.TRAVIS_SECURE_ENV_VARS ? describe : describe.skip;
runOnlyForInternalPRs('account API - Provisioning', function () {
let CLOUD_SECRET;
let CLOUD_API;
let CLOUD_NAME;
let CLOUD_ID;
let USER_NAME_1 = `NODE TEST ${Date.now()}`;
let USER_NAME_2 = `NODE TEST 2 ${Date.now()}`;
let USER_EMAIL_1 = `node-test+${Date.now()}@cloudinary.com`;
let USER_EMAIL_2 = `node-test2+${Date.now()}@cloudinary.com`;
let USER_ROLE = 'billing';
let USER_ID_1;
let USER_ID_2;
let GROUP_ID;
let CLOUD_NAME_PREFIX = `justaname${process.hrtime()[1] % 10000}`;
this.timeout(TIMEOUT.LONG);
before("Setup the required test", async function () {
let config = cloudinary.config(true);
if (!(config.provisioning_api_key && config.provisioning_api_secret && config.account_id)) {
// For external PRs the env variables are not availble, so we skip the provisioning API
this.skip();
}
let CLOUD_TO_CREATE = CLOUD_NAME_PREFIX + Date.now();
// Create a sub account(sub cloud)
let res = await cloudinary.provisioning.account.create_sub_account(CLOUD_TO_CREATE, CLOUD_TO_CREATE, {}, true).catch((err) => {
throw err;
});
CLOUD_API = res.api_access_keys[0].key;
CLOUD_SECRET = res.api_access_keys[0].secret;
CLOUD_NAME = res.cloud_name;
CLOUD_ID = res.id;
let createdUsers = await Promise.all([
cloudinary.provisioning.account.create_user(USER_NAME_1, USER_EMAIL_1, USER_ROLE, []).catch((err) => {
throw err;
}),
cloudinary.provisioning.account.create_user(USER_NAME_2, USER_EMAIL_2, USER_ROLE, []).catch((err) => {
throw err;
})
]);
USER_ID_1 = createdUsers[0].id;
USER_ID_2 = createdUsers[1].id;
// create a user group
let createGroupRes = await cloudinary.provisioning.account.create_user_group(`test-group-${Date.now()}`).catch((err) => {
throw err;
});
GROUP_ID = createGroupRes.id;
return true;
});
after('Destroy the sub_account and users that were created', async () => {
// Skip 'after' in case we don't have account configuration available
// This means that the beforeHook also didn't run
let config = cloudinary.config(true);
if (!(config.provisioning_api_key && config.provisioning_api_secret && config.account_id)) {
return;
}
let delRes = await cloudinary.provisioning.account.delete_sub_account(CLOUD_ID);
let delUser1Res = await cloudinary.provisioning.account.delete_user(USER_ID_1);
let delUser2Res = await cloudinary.provisioning.account.delete_user(USER_ID_2);
let delGroupRes = await cloudinary.provisioning.account.delete_user_group(GROUP_ID);
expect(delRes.message).to.eql('ok');
expect(delUser1Res.message).to.eql('ok');
expect(delUser2Res.message).to.eql('ok');
expect(delGroupRes.ok).to.eql(true); // notice the different response structure
});
it('Accepts credentials as an argument', async () => {
let NEW_NAME = 'This wont be created';
let options = {
provisioning_api_key: 'abc',
provisioning_api_secret: 'abc'
};
await cloudinary.provisioning.account.create_sub_account(CLOUD_ID, NEW_NAME, {}, null, null, options).catch((errRes) => {
expect(errRes.error.http_code).to.eql(401);
});
});
it('Updates a sub_account', async () => {
let NEW_NAME = CLOUD_NAME_PREFIX + Date.now();
await cloudinary.provisioning.account.update_sub_account(CLOUD_ID, NEW_NAME);
let subAccRes = await cloudinary.provisioning.account.sub_account(CLOUD_ID);
expect(subAccRes.name).to.eql(NEW_NAME);
});
it('Get all sub_accounts', async function () {
return cloudinary.provisioning.account.sub_accounts(true).then((res) => {
// ensure the cloud we created exists (there might be other clouds there...
let item = res.sub_accounts.find((subAccount) => {
return subAccount.id === CLOUD_ID;
});
expect(item.id).to.eql(CLOUD_ID);
}).catch((err) => {
throw err;
});
});
it('Get a specific sub_account', async function () {
return cloudinary.provisioning.account.sub_accounts(true, [CLOUD_ID]).then((res) => {
expect(res.sub_accounts.length).to.eql(1);
}).catch((err) => {
throw err;
});
});
it('Get sub_accounts by prefix', async function () {
return cloudinary.provisioning.account.sub_accounts(true, [], CLOUD_NAME_PREFIX).then((res) => {
expect(res.sub_accounts.length).to.eql(1);
}).catch((err) => {
throw err;
});
});
it('Gets a specific sub_account', async function () {
return cloudinary.provisioning.account.sub_account(CLOUD_ID).then((res) => {
expect(res.id).to.eql(CLOUD_ID);
}).catch((err) => {
throw err;
});
});
it('Updates a user', async function () {
let NEW_EMAIL_ADDRESS = `updated+${Date.now()}@cloudinary.com`;
await cloudinary.provisioning.account.update_user(USER_ID_1, 'updated', NEW_EMAIL_ADDRESS).then((res) => {
expect(res.name).to.eql('updated');
expect(res.email).to.eql(NEW_EMAIL_ADDRESS);
}).catch((err) => {
throw err;
});
await cloudinary.provisioning.account.user(USER_ID_1).then((res) => {
expect(res.id).to.eql(USER_ID_1);
expect(res.email).to.eql(NEW_EMAIL_ADDRESS);
}).catch((err) => {
throw err;
});
await cloudinary.provisioning.account.users().then((res) => {
let user = res.users.find((userEntry) => {
return userEntry.id === USER_ID_1;
});
expect(user.id).to.eql(USER_ID_1);
expect(user.email).to.eql(NEW_EMAIL_ADDRESS);
}).catch((err) => {
throw err;
});
});
it('Gets users in a list of userIDs', async () => {
await cloudinary.provisioning.account.users(null, [USER_ID_1]).then((res) => {
expect(res.users.length).to.eql(1);
}).catch((err) => {
throw err;
});
});
it('Gets pending users', async () => {
const result = await cloudinary.provisioning.account.users(true, [USER_ID_1]);
expect(result.users.length).to.eql(1);
});
it('Gets non-pending users', async () => {
const result = await cloudinary.provisioning.account.users(false, [USER_ID_1]);
expect(result.users.length).to.eql(0);
});
it('Gets pending and non-pending users', async () => {
const result = await cloudinary.provisioning.account.users(null, [USER_ID_1]);
expect(result.users.length).to.eql(1);
});
it('Gets users by prefix', async () => {
const [result_1, result_2] = await Promise.all([
cloudinary.provisioning.account.users(true, null, USER_NAME_2.slice(0, -1)),
cloudinary.provisioning.account.users(true, null, USER_NAME_2+'zzz')
]);
expect(result_1.users.length).to.eql(1);
expect(result_2.users.length).to.eql(0);
});
it('Gets users by sub_account_id', async () => {
const result = await cloudinary.provisioning.account.users(true, null, USER_NAME_2, CLOUD_ID);
expect(result.users.length).to.eql(1);
});
it("Gets users by last login", async () => {
const today = new Date();
const result1 = await cloudinary.provisioning.account.users(
null,
[USER_ID_1],
null,
null,
true,
today,
today
);
expect(result1.users.length).to.eql(0);
const result2 = await cloudinary.provisioning.account.users(
null,
[USER_ID_1],
null,
null,
false
);
expect(result2.users.length).to.eql(1);
});
it('Should throw an error when attempting to get users by a nonexistent sub_account_id', async () => {
const random_id = Math.floor(Math.random() * 100000);
try {
await cloudinary.provisioning.account.users(true, null, null, random_id);
expect().fail()
} catch ({error}) {
expect(error.message).to.eql(`Cannot find sub account with id ${random_id}`);
}
});
it('Updates the user group', async () => {
let NEW_NAME = `new-test-name_${Date.now()}`;
let res = await cloudinary.provisioning.account.update_user_group(GROUP_ID, NEW_NAME);
expect(res.id).to.eql(GROUP_ID);
let groupData = await cloudinary.provisioning.account.user_group((GROUP_ID));
expect(groupData.name).to.eql(NEW_NAME);
});
it('Adds and remove a user from a group', async () => {
let res = await cloudinary.provisioning.account.add_user_to_group(GROUP_ID, USER_ID_1);
expect(res.users.length).to.eql(1);
let groupUserData = await cloudinary.provisioning.account.user_group_users((GROUP_ID));
expect(groupUserData.users.length).to.eql(1);
//
let remUserFromGroupResp = await cloudinary.provisioning.account.remove_user_from_group(GROUP_ID, USER_ID_1);
expect(remUserFromGroupResp.users.length).to.eql(0);
});
it('Tests userGroups in account', async () => {
let res = await cloudinary.provisioning.account.user_groups();
let matchedGroup = res.user_groups.find((group) => {
return group.id === GROUP_ID;
});
// Ensure we can find our ID in the list(Which means we got a real list as a response)
expect(matchedGroup.id).to.eql(GROUP_ID);
});
});