Skip to content

Commit

Permalink
style(tests): applied ESLint formatting to unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Luke Carr committed Jun 3, 2020
1 parent 13be2a1 commit 04439da
Show file tree
Hide file tree
Showing 5 changed files with 285 additions and 158 deletions.
78 changes: 46 additions & 32 deletions tests/joodle.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,71 +10,83 @@ describe("The Joodle client class", () => {
let joodle: Joodle;

beforeAll(() => {
joodle = new Joodle({
baseURL,
token,
}, {
timeout,
retries,
rejectInvalidSSL,
});
joodle = new Joodle(
{
baseURL,
token,
},
{
timeout,
retries,
rejectInvalidSSL,
}
);

nock(baseURL)
.get(`/webservice/rest/server.php?wsfunction=auth_email_get_signup_settings&wstoken=${token}&moodlewsrestformat=json`)
.get(
`/webservice/rest/server.php?wsfunction=auth_email_get_signup_settings&wstoken=${token}&moodlewsrestformat=json`
)
.twice()
.reply(200, {
namefields: [
"firstname",
"lastname",
],
namefields: ["firstname", "lastname"],
passwordpolicy: "Password must exist!",
warnings: [],
});

nock(baseURL)
.get(`/webservice/rest/server.php?wsfunction=core_webservice_get_site_info&wstoken=${token}&moodlewsrestformat=json`)
.get(
`/webservice/rest/server.php?wsfunction=core_webservice_get_site_info&wstoken=${token}&moodlewsrestformat=json`
)
.reply(200, {
errorcode: "invalidtoken",
exception: "moodle_exception",
message: "Invalid token - token not found",
});
});

it("should be initialized with a baseURL and token", () => {
it("should be initialized with a baseURL and token", () => {
expect(joodle.got.defaults.options.prefixUrl).toBe(baseURL);
expect(joodle.got.defaults.options.searchParams!.get("wstoken")).toBe(token);
expect(joodle.got.defaults.options.searchParams!.get("moodlewsrestformat")).toBe("json");
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
expect(joodle.got.defaults.options.searchParams!.get("wstoken")).toBe(
token
);
expect(
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
joodle.got.defaults.options.searchParams!.get("moodlewsrestformat")
).toBe("json");
});

it("should allow initialization through environment variables", () => {
process.env["JOODLE_BASE_URL"] = "http://localhost";
process.env["JOODLE_TOKEN"] = "abc123";
process.env.JOODLE_BASE_URL = "http://localhost";
process.env.JOODLE_TOKEN = "abc123";

expect(() => new Joodle()).not.toThrowError();

delete process.env["JOODLE_BASE_URL"];
delete process.env["JOODLE_TOKEN"];
delete process.env.JOODLE_BASE_URL;
delete process.env.JOODLE_TOKEN;
});

it("should prioritize provided options over environment variables", () => {
process.env["JOODLE_BASE_URL"] = "http://localhost/";
process.env["JOODLE_TOKEN"] = "abc123";
process.env.JOODLE_BASE_URL = "http://localhost/";
process.env.JOODLE_TOKEN = "abc123";

const baseURL = "https://moodle.example.com/";
const joodle = new Joodle({
baseURL,
const newBaseURL = "https://moodle.example.com/";
const newJoodle = new Joodle({
baseURL: newBaseURL,
});

expect(joodle.got.defaults.options.prefixUrl).toBe(baseURL);
expect(newJoodle.got.defaults.options.prefixUrl).toBe(newBaseURL);

delete process.env["JOODLE_BASE_URL"];
delete process.env["JOODLE_TOKEN"];
delete process.env.JOODLE_BASE_URL;
delete process.env.JOODLE_TOKEN;
});

it("should allow HTTP options to be provided as a second constructor parameter", () => {
expect(joodle.got.defaults.options.timeout.request).toBe(timeout);
expect(joodle.got.defaults.options.retry.limit).toBe(retries);
expect(joodle.got.defaults.options.rejectUnauthorized).toBe(rejectInvalidSSL);
expect(joodle.got.defaults.options.rejectUnauthorized).toBe(
rejectInvalidSSL
);
});

it("should throw an error if the baseURL is not provided", () => {
Expand All @@ -94,6 +106,8 @@ describe("The Joodle client class", () => {
});

it("should expose the raw HTTP response through the getHttpResponse() function", () => {
return joodle.auth.email.getSignUpSettings().then((response) => expect(response.getHttpResponse()).toBeDefined());
return joodle.auth.email
.getSignUpSettings()
.then((response) => expect(response.getHttpResponse()).toBeDefined());
});
});
});
35 changes: 22 additions & 13 deletions tests/modules/auth/email.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,36 +21,45 @@ describe("The auth.email module", () => {
});

nock(baseURL)
.get(`/webservice/rest/server.php?wsfunction=auth_email_get_signup_settings&wstoken=${token}&moodlewsrestformat=json`)
.get(
`/webservice/rest/server.php?wsfunction=auth_email_get_signup_settings&wstoken=${token}&moodlewsrestformat=json`
)
.reply(200, {
namefields: [
"firstname",
"lastname",
],
namefields: ["firstname", "lastname"],
});

nock(baseURL)
.get(`/webservice/rest/server.php?wsfunction=auth_email_get_signup_settings&wstoken=xyz789&moodlewsrestformat=json`)
.get(
`/webservice/rest/server.php?wsfunction=auth_email_get_signup_settings&wstoken=xyz789&moodlewsrestformat=json`
)
.reply(200, error);

nock(baseURL)
.get(`/webservice/rest/server.php?wsfunction=auth_email_signup_user&username=test&firstname=Test&lastname=User&email=test%40example.com&password=password&wstoken=${token}&moodlewsrestformat=json`)
.get(
`/webservice/rest/server.php?wsfunction=auth_email_signup_user&username=test&firstname=Test&lastname=User&email=test%40example.com&password=password&wstoken=${token}&moodlewsrestformat=json`
)
.reply(200, {
success: 1,
});

nock(baseURL)
.get(`/webservice/rest/server.php?wsfunction=auth_email_signup_user&wstoken=xyz789&moodlewsrestformat=json`)
.get(
`/webservice/rest/server.php?wsfunction=auth_email_signup_user&wstoken=xyz789&moodlewsrestformat=json`
)
.reply(200, error);
});

describe("the getSignUpSettings() function", () => {
it("should handle successful responses", () => {
return expect(joodle.auth.email.getSignUpSettings()).resolves.toBeDefined();
return expect(
joodle.auth.email.getSignUpSettings()
).resolves.toBeDefined();
});

it("should handle erroneous responses", () => {
return expect(joodle.auth.email.getSignUpSettings()).rejects.toBeDefined();
return expect(
joodle.auth.email.getSignUpSettings()
).rejects.toBeDefined();
});
});

Expand All @@ -66,9 +75,9 @@ describe("The auth.email module", () => {
it("should handle successful responses", () => {
return expect(joodle.auth.email.signUpUser(user)).resolves.toBeDefined();
});

it("should handle erroneous responses", () => {
return expect(joodle.auth.email.signUpUser(user)).rejects.toBeDefined();
});
});
});
});
82 changes: 49 additions & 33 deletions tests/modules/core/role.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,73 +13,89 @@ describe("The core.role module", () => {
});

nock(baseURL)
.get(`/webservice/rest/server.php?wsfunction=core_role_assign_roles&assignments%5B0%5D%5Broleid%5D=123&assignments%5B0%5D%5Buserid%5D=456&assignments%5B0%5D%5Bcontextlevel%5D=block&assignments%5B0%5D%5Binstanceid%5D=789&wstoken=${token}&moodlewsrestformat=json`)
.get(
`/webservice/rest/server.php?wsfunction=core_role_assign_roles&assignments%5B0%5D%5Broleid%5D=123&assignments%5B0%5D%5Buserid%5D=456&assignments%5B0%5D%5Bcontextlevel%5D=block&assignments%5B0%5D%5Binstanceid%5D=789&wstoken=${token}&moodlewsrestformat=json`
)
.reply(200);

nock(baseURL)
.get(`/webservice/rest/server.php?wsfunction=core_role_assign_roles&assignments%5B0%5D%5Broleid%5D=123&assignments%5B0%5D%5Buserid%5D=456&assignments%5B0%5D%5Bcontextlevel%5D=block&assignments%5B0%5D%5Binstanceid%5D=789&wstoken=xyz789&moodlewsrestformat=json`)
.get(
`/webservice/rest/server.php?wsfunction=core_role_assign_roles&assignments%5B0%5D%5Broleid%5D=123&assignments%5B0%5D%5Buserid%5D=456&assignments%5B0%5D%5Bcontextlevel%5D=block&assignments%5B0%5D%5Binstanceid%5D=789&wstoken=xyz789&moodlewsrestformat=json`
)
.reply(200, {
exception: "webservice_access_exception",
errorcode: "accessexception",
message: "Access control exception (Access to the function core_webservice_get_site_info() is not allowed.",
debuginfo: "Access to the function core_webservice_get_site_info() is not allowed.",
message:
"Access control exception (Access to the function core_webservice_get_site_info() is not allowed.",
debuginfo:
"Access to the function core_webservice_get_site_info() is not allowed.",
});

nock(baseURL)
.get(`/webservice/rest/server.php?wsfunction=core_role_unassign_roles&unassignments%5B0%5D%5Broleid%5D=123&unassignments%5B0%5D%5Buserid%5D=456&unassignments%5B0%5D%5Bcontextlevel%5D=block&unassignments%5B0%5D%5Binstanceid%5D=789&wstoken=${token}&moodlewsrestformat=json`)
.get(
`/webservice/rest/server.php?wsfunction=core_role_unassign_roles&unassignments%5B0%5D%5Broleid%5D=123&unassignments%5B0%5D%5Buserid%5D=456&unassignments%5B0%5D%5Bcontextlevel%5D=block&unassignments%5B0%5D%5Binstanceid%5D=789&wstoken=${token}&moodlewsrestformat=json`
)
.reply(200);

nock(baseURL)
.get(`/webservice/rest/server.php?wsfunction=core_role_unassign_roles&unassignments%5B0%5D%5Broleid%5D=123&unassignments%5B0%5D%5Buserid%5D=456&unassignments%5B0%5D%5Bcontextlevel%5D=block&unassignments%5B0%5D%5Binstanceid%5D=789&wstoken=xyz789&moodlewsrestformat=json`)
.get(
`/webservice/rest/server.php?wsfunction=core_role_unassign_roles&unassignments%5B0%5D%5Broleid%5D=123&unassignments%5B0%5D%5Buserid%5D=456&unassignments%5B0%5D%5Bcontextlevel%5D=block&unassignments%5B0%5D%5Binstanceid%5D=789&wstoken=xyz789&moodlewsrestformat=json`
)
.reply(200, {
exception: "webservice_access_exception",
errorcode: "accessexception",
message: "Access control exception (Access to the function core_webservice_get_site_info() is not allowed.",
debuginfo: "Access to the function core_webservice_get_site_info() is not allowed.",
message:
"Access control exception (Access to the function core_webservice_get_site_info() is not allowed.",
debuginfo:
"Access to the function core_webservice_get_site_info() is not allowed.",
});
});

describe("the assignRoles() function", () => {
it("should handle successful responses", () => {
return expect(joodle.core.role.assignRoles({
roleid: 123,
userid: 456,
contextlevel: "block",
instanceid: 789
})).resolves.toBeDefined();
return expect(
joodle.core.role.assignRoles({
roleid: 123,
userid: 456,
contextlevel: "block",
instanceid: 789,
})
).resolves.toBeDefined();
});

it("should handle erroneous responses", () => {
return expect(joodle.core.role.assignRoles(
{
return expect(
joodle.core.role.assignRoles({
roleid: 123,
userid: 456,
contextlevel: "block",
instanceid: 789
}
)).rejects.toBeDefined();
instanceid: 789,
})
).rejects.toBeDefined();
});
});

describe("the unassignRoles() function", () => {
it("should handle successful responses", () => {
return expect(joodle.core.role.unassignRoles({
roleid: 123,
userid: 456,
contextlevel: "block",
instanceid: 789
})).resolves.toBeDefined();
return expect(
joodle.core.role.unassignRoles({
roleid: 123,
userid: 456,
contextlevel: "block",
instanceid: 789,
})
).resolves.toBeDefined();
});

it("should handle erroneous responses", () => {
return expect(joodle.core.role.unassignRoles(
{
return expect(
joodle.core.role.unassignRoles({
roleid: 123,
userid: 456,
contextlevel: "block",
instanceid: 789
}
)).rejects.toBeDefined();
instanceid: 789,
})
).rejects.toBeDefined();
});
});
});
});
Loading

0 comments on commit 04439da

Please sign in to comment.