diff --git a/DOCUMENTATION.md b/DOCUMENTATION.md
index 5afbb8f4c..92902f83f 100644
--- a/DOCUMENTATION.md
+++ b/DOCUMENTATION.md
@@ -323,7 +323,7 @@ const sdk = fromSharedOptions();
* [.getInstructions(deviceTypeSlugOrContract)](#balena.models.deviceType.getInstructions) ⇒ Promise
* [.getInstallMethod(deviceTypeSlug)](#balena.models.deviceType.getInstallMethod) ⇒ Promise
* [.apiKey](#balena.models.apiKey) : object
- * [.create(name, [description])](#balena.models.apiKey.create) ⇒ Promise
+ * [.create(name, expiryDate, [description])](#balena.models.apiKey.create) ⇒ Promise
* [.getAll([options])](#balena.models.apiKey.getAll) ⇒ Promise
* [.getAllNamedUserApiKeys([options])](#balena.models.apiKey.getAllNamedUserApiKeys) ⇒ Promise
* [.getProvisioningApiKeysByApplication(slugOrUuidOrId, [options])](#balena.models.apiKey.getProvisioningApiKeysByApplication) ⇒ Promise
@@ -725,7 +725,7 @@ balena.models.device.get(123).catch(function (error) {
* [.getInstructions(deviceTypeSlugOrContract)](#balena.models.deviceType.getInstructions) ⇒ Promise
* [.getInstallMethod(deviceTypeSlug)](#balena.models.deviceType.getInstallMethod) ⇒ Promise
* [.apiKey](#balena.models.apiKey) : object
- * [.create(name, [description])](#balena.models.apiKey.create) ⇒ Promise
+ * [.create(name, expiryDate, [description])](#balena.models.apiKey.create) ⇒ Promise
* [.getAll([options])](#balena.models.apiKey.getAll) ⇒ Promise
* [.getAllNamedUserApiKeys([options])](#balena.models.apiKey.getAllNamedUserApiKeys) ⇒ Promise
* [.getProvisioningApiKeysByApplication(slugOrUuidOrId, [options])](#balena.models.apiKey.getProvisioningApiKeysByApplication) ⇒ Promise
@@ -4460,7 +4460,7 @@ balena.models.deviceType.getInstallMethod('raspberry-pi').then(function(method)
**Kind**: static namespace of [models
](#balena.models)
* [.apiKey](#balena.models.apiKey) : object
- * [.create(name, [description])](#balena.models.apiKey.create) ⇒ Promise
+ * [.create(name, expiryDate, [description])](#balena.models.apiKey.create) ⇒ Promise
* [.getAll([options])](#balena.models.apiKey.getAll) ⇒ Promise
* [.getAllNamedUserApiKeys([options])](#balena.models.apiKey.getAllNamedUserApiKeys) ⇒ Promise
* [.getProvisioningApiKeysByApplication(slugOrUuidOrId, [options])](#balena.models.apiKey.getProvisioningApiKeysByApplication) ⇒ Promise
@@ -4470,7 +4470,7 @@ balena.models.deviceType.getInstallMethod('raspberry-pi').then(function(method)
-##### apiKey.create(name, [description]) ⇒ Promise
+##### apiKey.create(name, expiryDate, [description]) ⇒ Promise
This method registers a new api key for the current user with the name given.
**Kind**: static method of [apiKey
](#balena.models.apiKey)
@@ -4481,17 +4481,18 @@ This method registers a new api key for the current user with the name given.
| Param | Type | Default | Description |
| --- | --- | --- | --- |
| name | String
| | the API key name |
+| expiryDate | String
| | the API key expiry date |
| [description] | String
|
| 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);
});
```
diff --git a/src/models/api-key.ts b/src/models/api-key.ts
index 56a9eb33b..c234a0c1f 100644
--- a/src/models/api-key.ts
+++ b/src/models/api-key.ts
@@ -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 {
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',
diff --git a/tests/integration/auth.spec.ts b/tests/integration/auth.spec.ts
index a4a46ed58..39967448c 100644
--- a/tests/integration/auth.spec.ts
+++ b/tests/integration/auth.spec.ts
@@ -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);
diff --git a/tests/integration/balena.spec.ts b/tests/integration/balena.spec.ts
index 941b0fb47..1fda3b8c5 100644
--- a/tests/integration/balena.spec.ts
+++ b/tests/integration/balena.spec.ts
@@ -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;
diff --git a/tests/integration/models/api-key.spec.ts b/tests/integration/models/api-key.spec.ts
index a8d77fd63..880705dab 100644
--- a/tests/integration/models/api-key.spec.ts
+++ b/tests/integration/models/api-key.spec.ts
@@ -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);
- });
});
});
@@ -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',
),
]),
@@ -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',
),
]),
@@ -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({