Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions lib-es5/provisioning/account.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
'use strict';

var utils = require("../utils");
var call_account_api = require('../api_client/call_account_api');

var pickOnlyExistingValues = utils.pickOnlyExistingValues;

/**
* @desc Lists sub-accounts.
* @param [enabled] {boolean} - Whether to only return enabled sub-accounts (true) or disabled accounts (false).
Expand All @@ -11,6 +14,7 @@ var call_account_api = require('../api_client/call_account_api');
* @param [options] {object} - See {@link https://cloudinary.com/documentation/cloudinary_sdks#configuration_parameters|Configuration parameters} in the SDK documentation.
* @param [callback] {function}
*/

function sub_accounts(enabled) {
var ids = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : [];
var prefix = arguments[2];
Expand Down Expand Up @@ -148,9 +152,12 @@ function users(pending, user_ids, prefix, sub_account_id) {

var uri = ['users'];
var params = {
ids: user_ids
ids: user_ids,
pending,
prefix,
sub_account_id
};
return call_account_api('GET', uri, params, callback, options);
return call_account_api('GET', uri, pickOnlyExistingValues(params, "ids", "pending", "prefix", "sub_account_id"), callback, options);
}

/**
Expand Down
10 changes: 8 additions & 2 deletions lib/provisioning/account.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
const utils = require("../utils");
const call_account_api = require('../api_client/call_account_api');

const { pickOnlyExistingValues } = utils;

/**
* @desc Lists sub-accounts.
* @param [enabled] {boolean} - Whether to only return enabled sub-accounts (true) or disabled accounts (false).
Expand Down Expand Up @@ -125,9 +128,12 @@ function user(user_id, options = {}, callback) {
function users(pending, user_ids, prefix, sub_account_id, options = {}, callback) {
let uri = ['users'];
let params = {
ids: user_ids
ids: user_ids,
pending,
prefix,
sub_account_id
};
return call_account_api('GET', uri, params, callback, options);
return call_account_api('GET', uri, pickOnlyExistingValues(params, "ids", "pending", "prefix", "sub_account_id"), callback, options);
}

/**
Expand Down
86 changes: 68 additions & 18 deletions test/integration/api/provisioning/account_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,13 @@ runOnlyForInternalPRs('account API - Provisioning', function () {
let CLOUD_API;
let CLOUD_NAME;
let CLOUD_ID;
let USER_NAME = `SDK TEST ${Date.now()}`;
let USER_EMAIL = `sdk-test+${Date.now()}@cloudinary.com`;
let USER_NAME_1 = `SDK TEST ${Date.now()}`;
let USER_NAME_2 = `SDK TEST 2 ${Date.now()}`;
let USER_EMAIL_1 = `sdk-test+${Date.now()}@cloudinary.com`;
let USER_EMAIL_2 = `sdk-test2+${Date.now()}@cloudinary.com`;
let USER_ROLE = 'billing';
let USER_ID;
let USER_ID_1;
let USER_ID_2;
let GROUP_ID;
let CLOUD_NAME_PREFIX = `justaname${process.hrtime()[1] % 10000}`;
this.timeout(TIMEOUT.LONG);
Expand All @@ -40,11 +43,17 @@ runOnlyForInternalPRs('account API - Provisioning', function () {
CLOUD_NAME = res.cloud_name;
CLOUD_ID = res.id;

let createUser = await cloudinary.provisioning.account.create_user(USER_NAME, USER_EMAIL, USER_ROLE, []).catch((err) => {
throw err;
});
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 = createUser.id;
USER_ID_1 = createdUsers[0].id;
USER_ID_2 = createdUsers[1].id;

// create a user group

Expand All @@ -56,19 +65,21 @@ runOnlyForInternalPRs('account API - Provisioning', function () {
return true;
});

after('Destroy the sub_account and user that was created', async () => {
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 delUserRes = await cloudinary.provisioning.account.delete_user(USER_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(delUserRes.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
});

Expand Down Expand Up @@ -132,39 +143,78 @@ runOnlyForInternalPRs('account API - Provisioning', function () {
it('Updates a user', async function () {
let NEW_EMAIL_ADDRESS = `updated+${Date.now()}@cloudinary.com`;

await cloudinary.provisioning.account.update_user(USER_ID, 'updated', NEW_EMAIL_ADDRESS).then((res) => {
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).then((res) => {
expect(res.id).to.eql(USER_ID);
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;
return userEntry.id === USER_ID_1;
});
expect(user.id).to.eql(USER_ID);
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]).then((res) => {
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('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);
Expand All @@ -174,13 +224,13 @@ runOnlyForInternalPRs('account API - Provisioning', function () {
});

it('Adds and remove a user from a group', async () => {
let res = await cloudinary.provisioning.account.add_user_to_group(GROUP_ID, USER_ID);
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);
let remUserFromGroupResp = await cloudinary.provisioning.account.remove_user_from_group(GROUP_ID, USER_ID_1);
expect(remUserFromGroupResp.users.length).to.eql(0);
});

Expand Down