Skip to content

Commit

Permalink
Added bootstrapDeviceCredentials command for more convenient dealing …
Browse files Browse the repository at this point in the history
…with deviceCredentials.
  • Loading branch information
thomaswinkler committed Nov 27, 2023
1 parent 2d15a35 commit 90d4c81
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 1 deletion.
24 changes: 23 additions & 1 deletion lib/commands/administration.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { IUser, IApplication, ICurrentTenant } from "@c8y/client";
import { IUser, IApplication, ICurrentTenant, IDeviceCredentials } from "@c8y/client";

declare global {
namespace Cypress {
Expand Down Expand Up @@ -115,6 +115,28 @@ declare global {
authOptions?: C8yAuthOptions,
c8yoptions?: C8yClientOptions
): Chainable<string>;

/**
* 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<IDeviceCredentials | undefined>}
*/
bootstrapDeviceCredentials(
...args:
| [id: string | IUser, c8yoptions?: C8yClientOptions]
| [
authOptions: C8yAuthOptions,
id: string,
c8yoptions?: C8yClientOptions
]
): Chainable<IDeviceCredentials>;
}
}
}
53 changes: 53 additions & 0 deletions lib/commands/administration.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
}
);

0 comments on commit 90d4c81

Please sign in to comment.