Skip to content

Commit

Permalink
test: jestify jwt-unprotected-endpoint-authz
Browse files Browse the repository at this point in the history
Migrated test from Tap to Jest

File Path:
packages/cactus-cmd-api-server/src/test/
typescript/integration/
jwt-unprotected-endpoint-authz.test.ts

This is a PARTIAL resolution to issue hyperledger-cacti#238

Signed-off-by: Youngone Lee <[email protected]>
  • Loading branch information
Leeyoungone authored and petermetz committed May 16, 2022
1 parent 99ea2f6 commit c051d7f
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 101 deletions.
1 change: 0 additions & 1 deletion .taprc
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,6 @@ files:
- ./packages/cactus-cmd-api-server/src/test/typescript/integration/jwt-socketio-endpoint-authorization.test.ts
- ./packages/cactus-cmd-api-server/src/test/typescript/integration/jwt-endpoint-authz-scope-enforcement.test.ts
- ./packages/cactus-cmd-api-server/src/test/typescript/integration/remote-plugin-imports.test.ts
- ./packages/cactus-cmd-api-server/src/test/typescript/integration/jwt-unprotected-endpoint-authz.test.ts
- ./packages/cactus-cmd-api-server/src/test/typescript/integration/plugin-import-from-github.test.ts
- ./packages/cactus-cmd-api-server/src/test/typescript/integration/plugin-import-without-install.test.ts
- ./packages/cactus-cmd-api-server/src/test/typescript/unit/plugins/install-basic-plugin-keychain-memory.test.ts
Expand Down
1 change: 0 additions & 1 deletion jest.config.js

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

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import test, { Test } from "tape-promise/tape";
import "jest-extended";
import { v4 as uuidv4 } from "uuid";
import { generateKeyPair, exportSPKI } from "jose";
import expressJwt from "express-jwt";
Expand Down Expand Up @@ -26,102 +26,94 @@ const log = LoggerProvider.getOrCreate({
label: __filename,
});

test(testCase, async (t: Test) => {
try {
const jwtKeyPair = await generateKeyPair("RS256", { modulusLength: 4096 });
const jwtPublicKey = await exportSPKI(jwtKeyPair.publicKey);
const expressJwtOptions: expressJwt.Options = {
algorithms: ["RS256"],
secret: jwtPublicKey,
audience: uuidv4(),
issuer: uuidv4(),
};
t.ok(expressJwtOptions, "Express JWT config truthy OK");

const ep = new UnprotectedActionEndpoint({
connector: {} as PluginLedgerConnectorStub,
logLevel,
});

const authorizationConfig: IAuthorizationConfig = {
unprotectedEndpointExemptions: [ep.getPath()],
expressJwtOptions,
socketIoJwtOptions: { secret: jwtPublicKey },
};

const pluginRegistry = new PluginRegistry();
const plugin = new PluginLedgerConnectorStub({
logLevel,
pluginRegistry,
instanceId: uuidv4(),
});
pluginRegistry.add(plugin);

const configService = new ConfigService();
const apiSrvOpts = await configService.newExampleConfig();
apiSrvOpts.authorizationProtocol = AuthorizationProtocol.JSON_WEB_TOKEN;
apiSrvOpts.authorizationConfigJson = authorizationConfig;
apiSrvOpts.configFile = "";
apiSrvOpts.apiCorsDomainCsv = "*";
apiSrvOpts.apiPort = 0;
apiSrvOpts.cockpitPort = 0;
apiSrvOpts.grpcPort = 0;
apiSrvOpts.apiTlsEnabled = false;
apiSrvOpts.plugins = [];
const config = await configService.newExampleConfigConvict(apiSrvOpts);

const apiServer = new ApiServer({
config: config.getProperties(),
pluginRegistry,
});
test.onFinish(async () => await apiServer.shutdown());

const startResponse = apiServer.start();
await t.doesNotReject(
startResponse,
"failed to start API server with dynamic plugin imports configured for it...",
);
t.ok(startResponse, "startResponse truthy OK");

const addressInfoApi = (await startResponse).addressInfoApi;
const protocol = apiSrvOpts.apiTlsEnabled ? "https" : "http";
const { address, port } = addressInfoApi;
const apiHost = `${protocol}://${address}:${port}`;

const req1 = {
requestId: uuidv4(),
};

// look Ma, no access token
const res1 = await axios.request({
data: req1,
url: `${apiHost}${ep.getPath()}`,
method: ep.getVerbLowerCase() as Method,
});
t.ok(res1, "stub unprotected action response truthy OK");
t.equal(
res1.status,
StatusCodes.OK,
"stub unprotected action response status === 200 OK",
);
t.equal(typeof res1.data, "object", "typeof res1.data is 'object' OK");
t.ok(typeof res1.data.data, "res1.data.data truthy OK");
t.ok(
typeof res1.data.data.reqBodyrequestId,
"res1.data.data.reqBody truthy OK",
);
t.ok(
typeof res1.data.data.reqBody.requestId,
"res1.data.data.reqBody.requestId truthy OK",
);
t.equal(
res1.data.data.reqBody.requestId,
req1.requestId,
"res1.data.requestId === req1.requestId OK",
);
} catch (ex) {
log.error(ex);
t.fail("Exception thrown during test execution, see above for details!");
throw ex;
}
describe(testCase, () => {
let apiServer: ApiServer;

afterAll(async () => {
await apiServer.shutdown();
});

test(testCase, async () => {
try {
const jwtKeyPair = await generateKeyPair("RS256", {
modulusLength: 4096,
});
const jwtPublicKey = await exportSPKI(jwtKeyPair.publicKey);
const expressJwtOptions: expressJwt.Options = {
algorithms: ["RS256"],
secret: jwtPublicKey,
audience: uuidv4(),
issuer: uuidv4(),
};
expect(expressJwtOptions).toBeTruthy();

const ep = new UnprotectedActionEndpoint({
connector: {} as PluginLedgerConnectorStub,
logLevel,
});

const authorizationConfig: IAuthorizationConfig = {
unprotectedEndpointExemptions: [ep.getPath()],
expressJwtOptions,
socketIoJwtOptions: { secret: jwtPublicKey },
};

const pluginRegistry = new PluginRegistry();
const plugin = new PluginLedgerConnectorStub({
logLevel,
pluginRegistry,
instanceId: uuidv4(),
});
pluginRegistry.add(plugin);

const configService = new ConfigService();
const apiSrvOpts = await configService.newExampleConfig();
apiSrvOpts.authorizationProtocol = AuthorizationProtocol.JSON_WEB_TOKEN;
apiSrvOpts.authorizationConfigJson = authorizationConfig;
apiSrvOpts.configFile = "";
apiSrvOpts.apiCorsDomainCsv = "*";
apiSrvOpts.apiPort = 0;
apiSrvOpts.cockpitPort = 0;
apiSrvOpts.grpcPort = 0;
apiSrvOpts.apiTlsEnabled = false;
apiSrvOpts.plugins = [];
const config = await configService.newExampleConfigConvict(apiSrvOpts);

apiServer = new ApiServer({
config: config.getProperties(),
pluginRegistry,
});

const startResponse = apiServer.start();
await expect(startResponse).not.toReject();
expect(startResponse).toBeTruthy();

const addressInfoApi = (await startResponse).addressInfoApi;
const protocol = apiSrvOpts.apiTlsEnabled ? "https" : "http";
const { address, port } = addressInfoApi;
const apiHost = `${protocol}://${address}:${port}`;

const req1 = {
requestId: uuidv4(),
};

// look Ma, no access token
const res1 = await axios.request({
data: req1,
url: `${apiHost}${ep.getPath()}`,
method: ep.getVerbLowerCase() as Method,
});
expect(res1).toBeTruthy();
expect(res1.status).toBe(StatusCodes.OK);
expect(typeof res1.data).toBe("object");
expect(typeof res1.data.data).toBeTruthy();
expect(typeof res1.data.data.reqBodyrequestId).toBeTruthy();
expect(typeof res1.data.data.reqBody.requestId).toBeTruthy();
expect(res1.data.data.reqBody.requestId).toBe(req1.requestId);
} catch (ex) {
log.error(ex);
fail("Exception thrown during test execution, see above for details!");
throw ex;
}
});
});

0 comments on commit c051d7f

Please sign in to comment.