Skip to content

Commit

Permalink
#27: fix testing commands throwing exceptions #28
Browse files Browse the repository at this point in the history
  • Loading branch information
thomaswinkler authored Nov 9, 2023
1 parent 945b2c5 commit 07f9770
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 47 deletions.
4 changes: 1 addition & 3 deletions cypress/e2e/administration.cy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,7 @@ describe("administration", () => {
cy.spy(Cypress, "log").log(false);

//@ts-ignore
cy.deleteUser({ user: "test" }).then(() => {
throw new Error("Expected error. Should not get here.");
});
cy.deleteUser({ user: "test" });
});
});

Expand Down
43 changes: 23 additions & 20 deletions cypress/e2e/c8yclient.cy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -307,10 +307,11 @@ describe("c8yclient", () => {
});
});

it("fails with error if no auth or cookie is provided", () => {
it("fails with error if no auth or cookie is provided", (done) => {
Cypress.once("fail", (err) => {
expect(err.message).to.contain("Missing authentication");
expect(window.fetchStub).to.not.have.been.called;
done();
});

cy.c8yclient<ICurrentTenant>((client) => client.tenant.current());
Expand All @@ -331,14 +332,18 @@ describe("c8yclient", () => {
// last log is the one from fetchStub, get the last c8yclient log
const args = spy.args[Math.max(spy.callCount - 2, 0)];
const logged = _.isArray(args) ? _.last(args) : args;

expect(logged).to.not.be.undefined;
expect(logged.consoleProps).to.not.be.undefined;
expect(_.isFunction(logged.consoleProps)).to.be.true;

const consoleProps = logged.consoleProps.call();
expect(consoleProps.CookieAuth).to.eq("XSRF-TOKEN fsETfgIBdAnEyOLbADTu22 (testuser)");
expect(consoleProps.BasicAuth).to.eq("Basic dDEyMzQ1L2FkbWluMzpteXBhc3N3b3Jk (t12345/admin3)");
expect(consoleProps.CookieAuth).to.eq(
"XSRF-TOKEN fsETfgIBdAnEyOLbADTu22 (testuser)"
);
expect(consoleProps.BasicAuth).to.eq(
"Basic dDEyMzQ1L2FkbWluMzpteXBhc3N3b3Jk (t12345/admin3)"
);
});
});
});
Expand Down Expand Up @@ -552,11 +557,10 @@ describe("c8yclient", () => {
done();
});

cy.getAuth({ user: "admin", password: "mypassword" })
.c8yclient<ICurrentTenant>((client) => client.tenant.current())
.then(() => {
throw new Error("Expected error. Should not get here.");
});
cy.getAuth({
user: "admin",
password: "mypassword",
}).c8yclient<ICurrentTenant>((client) => client.tenant.current());
});

it("should catch and process generic error response", (done) => {
Expand All @@ -575,11 +579,10 @@ describe("c8yclient", () => {
done();
});

cy.getAuth({ user: "admin", password: "mypassword" })
.c8yclient<ICurrentTenant>((client) => client.tenant.current())
.then(() => {
throw new Error("Expected error. Should not get here.");
});
cy.getAuth({
user: "admin",
password: "mypassword",
}).c8yclient<ICurrentTenant>((client) => client.tenant.current());
});

it("should not throw on 404 response with failOnStatusCode false", () => {
Expand All @@ -605,7 +608,7 @@ describe("c8yclient", () => {
});

// https://github.com/SoftwareAG/cumulocity-cypress/issues/1
it("should wrap client authentication errors into CypressError", () => {
it("should wrap client authentication errors into CypressError", (done) => {
// {"responseObj":{"status":504,"isOkStatusCode":false,"statusText":"Gateway Timeout","headers":{"connection":"keep-alive","date":"Tue, 24 Oct 2023 07:57:52 GMT","keep-alive":"timeout=5","transfer-encoding":"chunked","x-powered-by":"Express"},"requestHeaders":{"Authorization":"Basic dHdpOkRlbW8yMDE5ISE=","content-type":"application/json","UseXBasic":true},"duration":92,"url":"http://localhost:9000/tenant/currentTenant","body":"Error occurred while trying to proxy: localhost:9000/tenant/currentTenant"}}'

stubResponse(
Expand All @@ -625,13 +628,13 @@ describe("c8yclient", () => {
"Error occurred while trying to proxy: localhost:9000/tenant/currentTenant"
);
expect(window.fetchStub).to.have.been.calledOnce;
done();
});

cy.getAuth({ user: "admin", password: "mypassword" })
.c8yclient<ICurrentTenant>((client) => client.tenant.current())
.then(() => {
throw new Error("Expected error. Should not get here.");
});
cy.getAuth({
user: "admin",
password: "mypassword",
}).c8yclient<ICurrentTenant>((client) => client.tenant.current());
});
});

Expand Down
9 changes: 2 additions & 7 deletions cypress/e2e/dates.cy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -302,9 +302,7 @@ describe("dates", () => {
"2023-03-25T12:00:00.000Z",
],
{ invalid: "throw" }
).then(() => {
throw new Error("Expected error. Should not get here.");
});
);
});

it("throws error on locale id not being registered", (done) => {
Expand All @@ -317,9 +315,7 @@ describe("dates", () => {
done();
});

cy.toISODate("15 June 2015 at 9:03:01 +01").then(() => {
throw new Error("Expected error. Should not get here.");
});
cy.toISODate("15 June 2015 at 9:03:01 +01");
});
});

Expand Down Expand Up @@ -394,7 +390,6 @@ describe("dates", () => {

beforeEach(() => {
cy.setLanguage("en");

});

it("fails with error for invalid source and throw option enabled", (done) => {
Expand Down
6 changes: 1 addition & 5 deletions cypress/e2e/general.cy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ describe("general", () => {
});

it("gainsight api.key request will throw exception", (done) => {
Cypress.on("fail", (err) => {
Cypress.once("fail", (err) => {
expect(err.message).to.eq(
"Intercepted Gainsight API key call, but Gainsight should have been disabled. Failing..."
);
Expand All @@ -31,10 +31,6 @@ describe("general", () => {
.then(() => {
$.get(url(`/tenant/system/options/gainsight/api.key`));
})
.wait("@interception")
.then(() => {
throw new Error("Expected error. Should not get here.");
});
});
});
});
10 changes: 3 additions & 7 deletions cypress/e2e/login.cy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ describe("login", () => {
useSession: false,
validationFn: cy.stub(() => {
validationCalled = true;
}),
}),
})
.then(() => {
expectHttpRequest({
Expand Down Expand Up @@ -252,9 +252,7 @@ describe("login", () => {
done();
});

cy.getAuth().then((result) => {
throw new Error("Expected error. Should not get here.");
});
cy.getAuth();
});
});

Expand Down Expand Up @@ -293,9 +291,7 @@ describe("login", () => {
done();
});

cy.useAuth("userthatdoesnotexist").then(() => {
throw new Error("Expected error. Should not get here.");
});
cy.useAuth("userthatdoesnotexist");
});
});
});
4 changes: 1 addition & 3 deletions cypress/e2e/request.cy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -321,9 +321,7 @@ describe("request", () => {
(response) => {
return response.status === 200;
}
).then(() => {
throw "Expected request to throw, but got response...";
});
);
});

it("should fail when retry requests exceeds max - failOnStatusCode false", () => {
Expand Down
4 changes: 2 additions & 2 deletions lib/commands/general.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ Cypress.Commands.add(

Cypress.Commands.add("setLanguage", (lang) => {
setLocale(lang);

Cypress.log({
name: "setLanguage",
consoleProps: () => {},
Expand Down Expand Up @@ -76,7 +76,7 @@ Cypress.Commands.add("disableGainsight", () => {
});

cy.intercept("/tenant/system/options/gainsight/api.key*", (req) => {
req.destroy();
req.reply({ statusCode: 404, body: {} });
throw new Error(
"Intercepted Gainsight API key call, but Gainsight should have been disabled. Failing..."
);
Expand Down

0 comments on commit 07f9770

Please sign in to comment.