From 90d4c81d6b98800b1be40b8e7d256962f82d6690 Mon Sep 17 00:00:00 2001 From: Thomas Winkler Date: Mon, 27 Nov 2023 13:19:42 +0100 Subject: [PATCH] Added bootstrapDeviceCredentials command for more convenient dealing with deviceCredentials. --- lib/commands/administration.d.ts | 24 ++++++++++++++- lib/commands/administration.js | 53 ++++++++++++++++++++++++++++++++ 2 files changed, 76 insertions(+), 1 deletion(-) diff --git a/lib/commands/administration.d.ts b/lib/commands/administration.d.ts index 058734b..a4659a2 100644 --- a/lib/commands/administration.d.ts +++ b/lib/commands/administration.d.ts @@ -1,4 +1,4 @@ -import { IUser, IApplication, ICurrentTenant } from "@c8y/client"; +import { IUser, IApplication, ICurrentTenant, IDeviceCredentials } from "@c8y/client"; declare global { namespace Cypress { @@ -115,6 +115,28 @@ declare global { authOptions?: C8yAuthOptions, c8yoptions?: C8yClientOptions ): Chainable; + + /** + * Bootstrap device credentials. Doing the same as c.deviceRegistration.bootstrap(), but works + * with getAuth(). Requires bootstrap credentials to be passed via getAuth(). + * + * @example + * cy.getAuth("devicebootstrap") + * .bootstrapDeviceCredentials(id) + * .useAuth() + * + * @param {C8yAuthOptions} options - The authentication options including username and password + * @returns {Chainable} + */ + bootstrapDeviceCredentials( + ...args: + | [id: string | IUser, c8yoptions?: C8yClientOptions] + | [ + authOptions: C8yAuthOptions, + id: string, + c8yoptions?: C8yClientOptions + ] + ): Chainable; } } } diff --git a/lib/commands/administration.js b/lib/commands/administration.js index 868f9df..c058535 100644 --- a/lib/commands/administration.js +++ b/lib/commands/administration.js @@ -230,3 +230,56 @@ Cypress.Commands.add( }); } ); + +Cypress.Commands.add( + "bootstrapDeviceCredentials", + { prevSubject: "optional" }, + function (...args) { + const $args = normalizedArgumentsWithAuth(args); + const [auth, id, clientOptions] = $args; + + const consoleProps = { + auth, + clientOptions, + }; + Cypress.log({ + name: "bootstrapDeviceCredentials", + id, + consoleProps: () => consoleProps, + }); + + consoleProps.auth = auth; + + const success = 201; + const failure = 404; + + return cy + .wrap(auth, { log: false }) + .c8yclientf( + (c) => + c.core.fetch("/devicecontrol/deviceCredentials", { + method: "POST", + headers: { + accept: + "application/vnd.com.nsn.cumulocity.devicecredentials+json", + }, + body: JSON.stringify({ id }), + }), + clientOptions + ) + .then((response) => { + console.log(response); + expect(response.status).to.be.oneOf([success, failure]); + let result; + if ( + response.status === success && + response.body && + response.body.username + ) { + result = response.body; + } + consoleProps.Yielded = result; + return cy.wrap(result); + }); + } +);