Skip to content

Commit

Permalink
Allow passing client options to administration commands. Added basic …
Browse files Browse the repository at this point in the history
…tests for administration commands.
  • Loading branch information
thomaswinkler committed Nov 7, 2023
1 parent a6b4c16 commit 44e193b
Show file tree
Hide file tree
Showing 4 changed files with 257 additions and 82 deletions.
163 changes: 163 additions & 0 deletions cypress/e2e/administration.cy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
import {
expectC8yClientRequest,
initRequestStub,
stubResponses,
} from "../support/util";
const { _ } = Cypress;

declare global {
interface Window {
fetchStub: Cypress.Agent<sinon.SinonStub>;
}
}

describe("administration", () => {
beforeEach(() => {
Cypress.env("C8Y_USERNAME", undefined);
Cypress.env("C8Y_PASSWORD", undefined);
Cypress.env("C8Y_TENANT", undefined);

initRequestStub();
stubResponses([
new window.Response(JSON.stringify({ name: "t1234" }), {
status: 200,
statusText: "OK",
headers: { "content-type": "application/json" },
}),
]);
});

context("deleteUser", () => {
it("should delete user from user options", function () {
stubResponses([
new window.Response(null, {
status: 204,
statusText: "OK",
headers: { "content-type": "application/json" },
}),
]);
cy.getAuth({ user: "admin", password: "mypassword", tenant: "t12345678" })
.deleteUser({ userName: "test", displayName: "wewe" })
.then((response) => {
expect(response.status).to.eq(204);
expectC8yClientRequest({
url: `${Cypress.config().baseUrl}/user/t12345678/users/test`,
auth: {
user: "admin",
password: "mypassword",
tenant: "t12345678",
},
headers: { UseXBasic: true, "content-type": "application/json" },
method: "DELETE",
});
});
});

it("should use client options", function () {
stubResponses([
new window.Response(null, {
status: 404,
statusText: "OK",
headers: { "content-type": "application/json" },
}),
]);
cy.getAuth({ user: "admin", password: "mypassword", tenant: "t12345678" })
.deleteUser(
{ userName: "test", displayName: "wewe" },
{ baseUrl: "https://abc.def.com", failOnStatusCode: true },
)
.then((response) => {
expect(response.status).to.eq(404);
expectC8yClientRequest({
url: `https://abc.def.com/user/t12345678/users/test`,
auth: {
user: "admin",
password: "mypassword",
tenant: "t12345678",
},
headers: { UseXBasic: true, "content-type": "application/json" },
method: "DELETE",
});
});
});
});

context("getCurrentTenant", () => {
it("should get current tenant using c8yclient", function () {
cy.getAuth({ user: "admin", password: "mypassword", tenant: "t12345678" })
.getCurrentTenant()
.then((response) => {
expect(response.status).to.eq(200);
expect(response.body.name).to.eq("t1234");
expectC8yClientRequest({
url: `${Cypress.config().baseUrl}/tenant/currentTenant`,
auth: {
user: "admin",
password: "mypassword",
tenant: "t12345678",
},
headers: { UseXBasic: true, "content-type": "application/json" },
});
});
});

it("should use client options", function () {
cy.getAuth({ user: "admin", password: "mypassword", tenant: "t12345678" })
.getCurrentTenant({
baseUrl: "https://abc.def.com",
})
.then((response) => {
expect(response.status).to.eq(200);
expectC8yClientRequest({
url: `https://abc.def.com/tenant/currentTenant`,
auth: {
user: "admin",
password: "mypassword",
tenant: "t12345678",
},
headers: { UseXBasic: true, "content-type": "application/json" },
});
});
});
});

context("getTenantId", () => {
it("should get tenant id using c8yclient", function () {
cy.getAuth({ user: "admin", password: "mypassword" })
.getTenantId()
.then((id) => {
expect(id).to.eq("t1234");
expect(window.fetchStub.callCount).to.equal(1);
});
});

it("should use tenant id from auth", function () {
cy.getAuth({ user: "admin", password: "mypassword", tenant: "t247652" })
.getTenantId()
.then((id) => {
expect(id).to.eq("t247652");
expect(window.fetchStub.callCount).to.equal(0);
});
});

it("should use tenant id from env variable", function () {
Cypress.env("C8Y_TENANT", "t232447652")
cy.getAuth({ user: "admin", password: "mypassword" })
.getTenantId()
.then((id) => {
expect(id).to.eq("t232447652");
expect(window.fetchStub.callCount).to.equal(0);
});
});

it("should prefer tenant id from auth", function () {
Cypress.env("C8Y_TENANT", "t232447652")
cy.getAuth({ user: "admin", password: "mypassword", tenant: "t324678" })
.getTenantId()
.then((id) => {
expect(id).to.eq("t324678");
expect(window.fetchStub.callCount).to.equal(0);
});
});
});
});
31 changes: 17 additions & 14 deletions lib/commands/administration.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,25 +20,28 @@ declare global {
* displayName: "New User",
* });
*
* @param {C8yAuthOptions} authOptions the authentication options to use for the request
* @param {C8yUserOptions} userOptions the user options defining the user to be created
* @param {C8yAuthOptions} authOptions the C8yAuthOptions authentication options including username and password
* @param {IUser} userOptions the user options defining the user to be created
* @param {string[]} permissions the permissions to be assigned to the user
* @param {string[] | IApplication[]} applications the name of applications to subscribe the user to
*
* @param {C8yClientOptions} c8yoptions the C8yClientOptions options passed to cy.c8yclient
*
* @returns {[C8yAuthOptions, string]} the auth options and id of the user created for chaining
*/
createUser(
...args:
| [
authOptions: C8yAuthOptions,
userOptions: C8yUserOptions,
userOptions: IUser,
permissions?: string[],
applications?: string[] | IApplication[]
applications?: string[] | IApplication[],
c8yoptions?: C8yClientOptions
]
| [
userOptions: C8yUserOptions,
userOptions: IUser,
permissions?: string[],
applications?: string[] | IApplication[]
applications?: string[] | IApplication[],
c8yoptions?: C8yClientOptions
]
): Chainable<Cypress.Response<IUser>>;

Expand All @@ -52,15 +55,16 @@ declare global {
* @example
* cy.deleteUser("newuser");
*
* @param {C8yAuthOptions} authOptions the authentication options to use for the request
* @param {C8yAuthOptions} authOptions the C8yAuthOptions authentication options including username and password
* @param {string} username the name of the user to be deleted
* @param {C8yClientOptions} c8yoptions the C8yClientOptions options passed to cy.c8yclient
*
* @returns {C8yAuthOptions} the auth options for chaining
*/
deleteUser(
...args:
| [username: string | C8yUserOptions]
| [authOptions: C8yAuthOptions, username: string]
| [username: string | IUser, c8yoptions?: C8yClientOptions]
| [authOptions: C8yAuthOptions, username: string | IUser, c8yoptions?: C8yClientOptions]
): Chainable<Cypress.Response<null>>;

/**
Expand All @@ -72,9 +76,10 @@ declare global {
* cy.getCurrentTenant();
* cy.getAuth("admin").getCurrentTenant();
*
* @param {C8yAuthOptions} options the authentication options including username and password
* @param {C8yAuthOptions} authOptions the C8yAuthOptions authentication options including username and password
* @param {C8yClientOptions} c8yoptions the C8yClientOptions options passed to cy.c8yclient
*/
getCurrentTenant(authOptions?: C8yAuthOptions): Chainable<Cypress.Response<ICurrentTenant>>;
getCurrentTenant(authOptions?: C8yAuthOptions, c8yoptions?: C8yClientOptions): Chainable<Cypress.Response<ICurrentTenant>>;

/**
* Convenience getter for name of the current tenant.
Expand All @@ -94,6 +99,4 @@ declare global {
getTenantId(authOptions?: C8yAuthOptions): Chainable<string>;
}
}

type C8yUserOptions = Omit<IUser, "id" | "self">;
}
Loading

0 comments on commit 44e193b

Please sign in to comment.