Skip to content

Commit

Permalink
Tst slint fixes, refactor CLI commands, eslint jst plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
hrax committed Aug 18, 2024
1 parent 958217d commit 9441a93
Show file tree
Hide file tree
Showing 17 changed files with 178 additions and 176 deletions.
12 changes: 8 additions & 4 deletions eslint.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import eslint from "@eslint/js";
* npm i [email protected] --save-dev
*/
import globals from "globals";
import jest from "eslint-plugin-jest";

/**
* Plugins
Expand Down Expand Up @@ -118,10 +119,13 @@ export default [
}
},
{
name: "ts",
rules: {

}
name: "jest",
languageOptions: {
globals: {
...jest.environments.globals.globals
}
},
files: ["**/*.spec.ts"]
},
// ESLint config file ONLY!
{
Expand Down
26 changes: 26 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@
"@types/node": "^22.0.0",
"@types/prompts": "^2.4.9",
"eslint": ">=9.3.0",
"eslint-plugin-jest": "^28.8.0",
"globals": ">=15.9.0",
"jest": "^29.7.0",
"jest-when": "^3.6.0",
Expand Down
55 changes: 26 additions & 29 deletions spec/core/OAuthClient.spec.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable camelcase, no-magic-numbers */
import { OAuthClient, OAuthCodeExpired, OAuthRefreshTokenExpired, OAuthUsernamePasswordIncorrect } from "../../src/core/OAuthClient";
import { InstanceConfig, InstanceOAuthTokenData, Profile } from "../../src/core/ProfileManager";
import { Request, Response } from "../../src/core/Request";
Expand All @@ -18,7 +19,6 @@ describe("OAuthClientSpec", () => {
refresh_token: "bbb",
scope: "",
token_type: "Bearer",
// seconds
expires_in: 60
}
}
Expand All @@ -39,54 +39,51 @@ describe("OAuthClientSpec", () => {
const token: InstanceOAuthTokenData = {
clientID: "clientID",
clientSecret: "clientSecret",
// current time -1 hour
lastRetrieved: Date.now() - (60 * 60 * 1000),
token: {
access_token: "aaa",
refresh_token: "bbb",
scope: "",
token_type: "Bearer",
// seconds
expires_in: 60
}
};

expect(OAuthClient.isTokenExpired(token)).toBe(true);
});
it("should be valid", () => {
it("should be valid", () => {
const tokenValid: InstanceOAuthTokenData = {
clientID: "clientID",
clientSecret: "clientSecret",
// current time - 10 sec
// Current time - 10 sec
lastRetrieved: Date.now() - 10000,
token: {
access_token: "aaa",
refresh_token: "bbb",
scope: "",
token_type: "Bearer",
// seconds
expires_in: 60
}
};

expect(OAuthClient.isTokenExpired(tokenValid)).toBe(false);;
expect(OAuthClient.isTokenExpired(tokenValid)).toBe(false);
});
});

describe("request token by username", () => {
it("should resolve on 200", async () => {
let response = Response.empty(JSON.stringify(config.auth.token!));
it("should resolve on 200", async() => {
const response = Response.empty(JSON.stringify(config.auth.token));
jest.spyOn(response, "isOK").mockReturnValue(true);
jest.spyOn(Request, "execute").mockResolvedValue(response);

const client = new OAuthClient();
await expect(client.requestTokenByUsername(profile, "admin", "admin")).resolves.toStrictEqual(config.auth.token!);
await expect(client.requestTokenByUsername(profile, "admin", "admin")).resolves.toStrictEqual(config.auth.token);
});

it("should reject on 401", async () => {
let response = Response.empty(JSON.stringify({}));
jest.spyOn(response, "isEmpty").mockReturnValue(false);
jest.spyOn(response, "isUnauthorized").mockReturnValue(true);
it("should reject on 401", async() => {
const response = Response.empty(JSON.stringify({}));
jest.spyOn(response, "isEmpty").mockReturnValue(false);
jest.spyOn(response, "isUnauthorized").mockReturnValue(true);
jest.spyOn(Request, "execute").mockRejectedValue(response);

const client = new OAuthClient();
Expand All @@ -96,18 +93,18 @@ describe("OAuthClientSpec", () => {

describe("request token by code", () => {
const code = "1234";
it("should resolve on 200", async () => {
let response = Response.empty(JSON.stringify(config.auth.token!));
jest.spyOn(response, "isOK").mockReturnValue(true);
it("should resolve on 200", async() => {
const response = Response.empty(JSON.stringify(config.auth.token));
jest.spyOn(response, "isOK").mockReturnValue(true);
jest.spyOn(Request, "execute").mockResolvedValue(response);

const client = new OAuthClient();
await expect(client.requestTokenByCode(profile, code)).resolves.toStrictEqual(config.auth.token!);
await expect(client.requestTokenByCode(profile, code)).resolves.toStrictEqual(config.auth.token);
});

it("should reject on 401", async () => {
let response = Response.empty("{}");
jest.spyOn(response, "isEmpty").mockReturnValue(false);
it("should reject on 401", async() => {
const response = Response.empty("{}");
jest.spyOn(response, "isEmpty").mockReturnValue(false);
jest.spyOn(response, "isUnauthorized").mockReturnValue(true);
jest.spyOn(Request, "execute").mockRejectedValue(response);

Expand All @@ -117,17 +114,17 @@ describe("OAuthClientSpec", () => {
});

describe("refresh token", () => {
it("should resolve on 200", async () => {
let response = Response.empty(JSON.stringify(config.auth.token!));
it("should resolve on 200", async() => {
const response = Response.empty(JSON.stringify(config.auth.token));
jest.spyOn(response, "isOK").mockReturnValue(true);
jest.spyOn(Request, "execute").mockResolvedValue(response);
const client = new OAuthClient();
await expect(client.refreshToken(profile)).resolves.toStrictEqual(config.auth.token!);
await expect(client.refreshToken(profile)).resolves.toStrictEqual(config.auth.token);
});

it("should reject on 401", async () => {
let response = Response.empty("{}");
jest.spyOn(response, "isEmpty").mockReturnValue(false);
it("should reject on 401", async() => {
const response = Response.empty("{}");
jest.spyOn(response, "isEmpty").mockReturnValue(false);
jest.spyOn(response, "isUnauthorized").mockReturnValue(true);
jest.spyOn(Request, "execute").mockRejectedValue(response);
const client = new OAuthClient();
Expand All @@ -136,7 +133,7 @@ describe("OAuthClientSpec", () => {
});

it("should extend headers with authentication", async() => {
const token: SNOAuthTokenData = config.auth.token!;
const token: SNOAuthTokenData = config.auth.token as SNOAuthTokenData;
const options: RequestOptions = {
method: "GET"
};
Expand All @@ -145,6 +142,6 @@ describe("OAuthClientSpec", () => {
await client.handleAuthentication(profile, options);

expect(options.headers).not.toBeUndefined();
expect(options.headers!.authorization).toBe(`${token.token_type} ${token.access_token}`);
expect(options.headers?.authorization).toBe(`${token.token_type} ${token.access_token}`);
});
});
18 changes: 8 additions & 10 deletions spec/core/ProfileManager.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,13 @@ describe("ProfileManagerSpec", () => {
dev: 0,
gid: 0,
ino: 0,
isBlockDevice: function(){return false},
isCharacterDevice: function(){return false},
isDirectory: function(){return true},
isFIFO: function(){return false},
isFile: function(){return false},
isSocket: function(){return false},
isSymbolicLink: function(){return false},
isBlockDevice: () => false,
isCharacterDevice: () => false,
isDirectory: () => true,
isFIFO: () => false,
isFile: () => false,
isSocket: () => false,
isSymbolicLink: () => false,
mode: 0,
mtime: new Date(),
mtimeMs: Date.now(),
Expand All @@ -60,10 +60,9 @@ describe("ProfileManagerSpec", () => {

when(jest.spyOn(fs, "readdirSync"))
.defaultImplementation(jesthelpers.defaultWhenImplementationThrow)
// @ts-ignore
// @ts-expect-error wrong signature picked up
.calledWith(profilesHomePath).mockReturnValue(["dev1", "dev2"]);

// @ts-ignore
// eslint-disable-next-line no-unused-vars, @typescript-eslint/no-unused-vars
jest.spyOn(fs, "statSync").mockImplementation((path) => {
return dirStats;
Expand Down Expand Up @@ -194,7 +193,6 @@ describe("ProfileManagerSpec", () => {

expect(rmSpy).toHaveBeenCalledTimes(1);
});

});

// describe("Profile", () => {
Expand Down
Loading

0 comments on commit 9441a93

Please sign in to comment.