Skip to content

Commit

Permalink
apiKey.create: Require expiryDate parameter and require it before…
Browse files Browse the repository at this point in the history
… the optional description

See: https://balena.fibery.io/Work/Project/Default-API-key-expiry-860
Change-type: major
  • Loading branch information
myarmolinsky committed Nov 27, 2024
1 parent c0f280f commit 75588e0
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 36 deletions.
13 changes: 7 additions & 6 deletions DOCUMENTATION.md
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ const sdk = fromSharedOptions();
* [.getInstructions(deviceTypeSlugOrContract)](#balena.models.deviceType.getInstructions) ⇒ <code>Promise</code>
* [.getInstallMethod(deviceTypeSlug)](#balena.models.deviceType.getInstallMethod) ⇒ <code>Promise</code>
* [.apiKey](#balena.models.apiKey) : <code>object</code>
* [.create(name, [description])](#balena.models.apiKey.create) ⇒ <code>Promise</code>
* [.create(name, expiryDate, [description])](#balena.models.apiKey.create) ⇒ <code>Promise</code>
* [.getAll([options])](#balena.models.apiKey.getAll) ⇒ <code>Promise</code>
* [.getAllNamedUserApiKeys([options])](#balena.models.apiKey.getAllNamedUserApiKeys) ⇒ <code>Promise</code>
* [.getProvisioningApiKeysByApplication(slugOrUuidOrId, [options])](#balena.models.apiKey.getProvisioningApiKeysByApplication) ⇒ <code>Promise</code>
Expand Down Expand Up @@ -725,7 +725,7 @@ balena.models.device.get(123).catch(function (error) {
* [.getInstructions(deviceTypeSlugOrContract)](#balena.models.deviceType.getInstructions) ⇒ <code>Promise</code>
* [.getInstallMethod(deviceTypeSlug)](#balena.models.deviceType.getInstallMethod) ⇒ <code>Promise</code>
* [.apiKey](#balena.models.apiKey) : <code>object</code>
* [.create(name, [description])](#balena.models.apiKey.create) ⇒ <code>Promise</code>
* [.create(name, expiryDate, [description])](#balena.models.apiKey.create) ⇒ <code>Promise</code>
* [.getAll([options])](#balena.models.apiKey.getAll) ⇒ <code>Promise</code>
* [.getAllNamedUserApiKeys([options])](#balena.models.apiKey.getAllNamedUserApiKeys) ⇒ <code>Promise</code>
* [.getProvisioningApiKeysByApplication(slugOrUuidOrId, [options])](#balena.models.apiKey.getProvisioningApiKeysByApplication) ⇒ <code>Promise</code>
Expand Down Expand Up @@ -4460,7 +4460,7 @@ balena.models.deviceType.getInstallMethod('raspberry-pi').then(function(method)
**Kind**: static namespace of [<code>models</code>](#balena.models)

* [.apiKey](#balena.models.apiKey) : <code>object</code>
* [.create(name, [description])](#balena.models.apiKey.create) ⇒ <code>Promise</code>
* [.create(name, expiryDate, [description])](#balena.models.apiKey.create) ⇒ <code>Promise</code>
* [.getAll([options])](#balena.models.apiKey.getAll) ⇒ <code>Promise</code>
* [.getAllNamedUserApiKeys([options])](#balena.models.apiKey.getAllNamedUserApiKeys) ⇒ <code>Promise</code>
* [.getProvisioningApiKeysByApplication(slugOrUuidOrId, [options])](#balena.models.apiKey.getProvisioningApiKeysByApplication) ⇒ <code>Promise</code>
Expand All @@ -4470,7 +4470,7 @@ balena.models.deviceType.getInstallMethod('raspberry-pi').then(function(method)

<a name="balena.models.apiKey.create"></a>

##### apiKey.create(name, [description]) ⇒ <code>Promise</code>
##### apiKey.create(name, expiryDate, [description]) ⇒ <code>Promise</code>
This method registers a new api key for the current user with the name given.

**Kind**: static method of [<code>apiKey</code>](#balena.models.apiKey)
Expand All @@ -4481,17 +4481,18 @@ This method registers a new api key for the current user with the name given.
| Param | Type | Default | Description |
| --- | --- | --- | --- |
| name | <code>String</code> | | the API key name |
| expiryDate | <code>String</code> | | the API key expiry date |
| [description] | <code>String</code> | <code></code> | the API key description |

**Example**
```js
balena.models.apiKey.create(apiKeyName).then(function(apiKey) {
balena.models.apiKey.create(apiKeyName, 2030-10-12).then(function(apiKey) {
console.log(apiKey);
});
```
**Example**
```js
balena.models.apiKey.create(apiKeyName, apiKeyDescription).then(function(apiKey) {
balena.models.apiKey.create(apiKeyName, 2030-10-12, apiKeyDescription).then(function(apiKey) {
console.log(apiKey);
});
```
Expand Down
13 changes: 6 additions & 7 deletions src/models/api-key.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,39 +39,38 @@ const getApiKeysModel = function (
* @description This method registers a new api key for the current user with the name given.
*
* @param {String} name - the API key name
* @param {String} expiryDate - the API key expiry date
* @param {String} [description=null] - the API key description
*
* @fulfil {String} - API key
* @returns {Promise}
*
* @example
* balena.models.apiKey.create(apiKeyName).then(function(apiKey) {
* balena.models.apiKey.create(apiKeyName, 2030-10-12).then(function(apiKey) {
* console.log(apiKey);
* });
*
* @example
* balena.models.apiKey.create(apiKeyName, apiKeyDescription).then(function(apiKey) {
* balena.models.apiKey.create(apiKeyName, 2030-10-12, apiKeyDescription).then(function(apiKey) {
* console.log(apiKey);
* });
*/
async create(
name: string,
expiryDate: string,
description: string | null = null,
expiryDate: string | null = null,
): Promise<string> {
const apiKeyBody: {
name: string;
expiryDate: string;
description?: string | null;
expiryDate?: string | null;
} = {
name,
expiryDate,
};
if (typeof description === 'string' && !!description) {
apiKeyBody.description = description;
}
if (typeof expiryDate === 'string' && !!expiryDate) {
apiKeyBody.expiryDate = expiryDate;
}
try {
const { body } = await request.send({
method: 'POST',
Expand Down
1 change: 1 addition & 0 deletions tests/integration/auth.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ describe('SDK authentication', function () {
await balena.auth.loginWithToken(token);
const apiKey = await balena.models.apiKey.create(
`${TEST_KEY_NAME_PREFIX}_apiKey`,
new Date(Date.now() + 1000 * 60 * 60 * 24).toISOString(),
);
await balena.auth.logout();
await balena.auth.loginWithToken(apiKey);
Expand Down
1 change: 1 addition & 0 deletions tests/integration/balena.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,7 @@ describe('Balena SDK', function () {
before(async function () {
const testApiKey = await balena.models.apiKey.create(
`${TEST_KEY_NAME_PREFIX}_apiKey`,
new Date(Date.now() + 1000 * 60 * 60 * 24).toISOString(),
'apiKeyDescription',
);
this.testApiKey = testApiKey;
Expand Down
40 changes: 17 additions & 23 deletions tests/integration/models/api-key.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,40 +18,25 @@ describe('API Key model', function () {

parallel('', function () {
it('should be able to create a new api key', async function () {
const tomorrowDate = new Date(
Date.now() + 1000 * 60 * 60 * 24,
).toISOString();
const key = await balena.models.apiKey.create(
`${TEST_KEY_NAME_PREFIX}_apiKey`,
tomorrowDate,
);
expect(key).to.be.a('string');
expect(key).to.have.property('expiry_date').to.be.equal(tomorrowDate);
});

it('should be able to create a new api key with description', async function () {
const key = await balena.models.apiKey.create(
`${TEST_KEY_NAME_PREFIX}_apiKey2`,
new Date(Date.now() + 1000 * 60 * 60 * 24).toISOString(),
'apiKeyDescription',
);
expect(key).to.be.a('string');
});

it('should be able to create a new api key with expiry-date', async function () {
const tomorrowDate = new Date(Date.now() + 86400000).toISOString(); // one day in future
const key = await balena.models.apiKey.create(
`${TEST_KEY_NAME_PREFIX}_apiKeyWithExpiry`,
'apiKeyDescription',
tomorrowDate,
);
expect(key).to.be.a('string');

const userKeys = await balena.models.apiKey.getAllNamedUserApiKeys();

expect(userKeys).to.be.an('array');
const userKeyWithExpiry = userKeys.filter(
(elem) => elem.name === `${TEST_KEY_NAME_PREFIX}_apiKeyWithExpiry`,
);
expect(userKeyWithExpiry).to.not.be.empty;
expect(userKeyWithExpiry[0])
.to.have.property('expiry_date')
.to.be.equal(tomorrowDate);
});
});
});

Expand All @@ -74,9 +59,13 @@ describe('API Key model', function () {
describe('given two named api keys', function () {
before(() =>
Promise.all([
balena.models.apiKey.create(`${TEST_KEY_NAME_PREFIX}_apiKey1`),
balena.models.apiKey.create(
`${TEST_KEY_NAME_PREFIX}_apiKey1`,
new Date(Date.now() + 1000 * 60 * 60 * 24).toISOString(),
),
balena.models.apiKey.create(
`${TEST_KEY_NAME_PREFIX}_apiKey2`,
new Date(Date.now() + 1000 * 60 * 60 * 24).toISOString(),
'apiKey2Description',
),
]),
Expand Down Expand Up @@ -122,9 +111,13 @@ describe('API Key model', function () {
describe('given two named api keys', function () {
before(() =>
Promise.all([
balena.models.apiKey.create(`${TEST_KEY_NAME_PREFIX}_apiKey1`),
balena.models.apiKey.create(
`${TEST_KEY_NAME_PREFIX}_apiKey1`,
new Date(Date.now() + 1000 * 60 * 60 * 24).toISOString(),
),
balena.models.apiKey.create(
`${TEST_KEY_NAME_PREFIX}_apiKey2`,
new Date(Date.now() + 1000 * 60 * 60 * 24).toISOString(),
'apiKey2Description',
),
]),
Expand Down Expand Up @@ -171,6 +164,7 @@ describe('API Key model', function () {
before(async function () {
await balena.models.apiKey.create(
`${TEST_KEY_NAME_PREFIX}_apiKeyToBeUpdated`,
new Date(Date.now() + 1000 * 60 * 60 * 24).toISOString(),
'apiKeyDescriptionToBeUpdated',
);
const [apiKey] = await balena.models.apiKey.getAll({
Expand Down

0 comments on commit 75588e0

Please sign in to comment.