Skip to content

Commit

Permalink
[TEST] added test to check for #268 (#290)
Browse files Browse the repository at this point in the history
* [TEST] added test to check for #268

* added assertion
  • Loading branch information
aricart authored May 9, 2022
1 parent b6b2950 commit 130476c
Showing 1 changed file with 58 additions and 0 deletions.
58 changes: 58 additions & 0 deletions tests/auth_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
connect,
credsAuthenticator,
ErrorCode,
Events,
jwtAuthenticator,
nkeyAuthenticator,
Status,
Expand All @@ -34,6 +35,16 @@ import { deferred, nkeys } from "../nats-base-client/internal_mod.ts";
import { NKeyAuth } from "../nats-base-client/authenticator.ts";
import { assert } from "../nats-base-client/denobuffer.ts";
import { cleanup, setup } from "./jstest_util.ts";
import {
createAccount,
createOperator,
createUser,
} from "https://raw.githubusercontent.com/nats-io/nkeys.js/main/src/nkeys.ts";
import {
encodeAccount,
encodeOperator,
encodeUser,
} from "https://raw.githubusercontent.com/nats-io/jwt.js/main/src/jwt.ts";

const conf = {
authorization: {
Expand Down Expand Up @@ -524,3 +535,50 @@ Deno.test("auth - creds authenticator validation", () => {
}
});
});

Deno.test("auth - expiration is notified", async () => {
const O = createOperator();
const A = createAccount();

const resolver: Record<string, string> = {};
resolver[A.getPublicKey()] = await encodeAccount("A", A, {
limits: {
conn: -1,
subs: -1,
},
}, { signer: O });
const conf = {
operator: await encodeOperator("O", O),
resolver: "MEMORY",
"resolver_preload": resolver,
};

const ns = await NatsServer.start(conf);

const U = createUser();
const ujwt = await encodeUser("U", U, A, { bearer_token: true }, {
exp: Math.round(Date.now() / 1000) + 3,
});

const nc = await connect({
port: ns.port,
maxReconnectAttempts: -1,
authenticator: jwtAuthenticator(ujwt),
});

let authErrors = 0;
(async () => {
for await (const s of nc.status()) {
if (
s.type === Events.Error && s.data === ErrorCode.AuthenticationExpired
) {
authErrors++;
}
}
})().then();

const err = await nc.closed();
assert(authErrors >= 1);
assertErrorCode(err!, ErrorCode.AuthenticationExpired);
await cleanup(ns);
});

0 comments on commit 130476c

Please sign in to comment.