diff --git a/docs/modules/nats.md b/docs/modules/nats.md index b79f13299..ec66e015e 100644 --- a/docs/modules/nats.md +++ b/docs/modules/nats.md @@ -22,6 +22,10 @@ npm install @testcontainers/nats --save-dev [Set credentials:](../../packages/modules/nats/src/nats-container.test.ts) inside_block:credentials + +[No credentials:](../../packages/modules/nats/src/nats-container.test.ts) inside_block:noCredentials + + [Enable JetStream:](../../packages/modules/nats/src/nats-container.test.ts) inside_block:jetstream \ No newline at end of file diff --git a/packages/modules/nats/src/nats-container.test.ts b/packages/modules/nats/src/nats-container.test.ts index ef73b2a5a..5bd9aca74 100644 --- a/packages/modules/nats/src/nats-container.test.ts +++ b/packages/modules/nats/src/nats-container.test.ts @@ -9,7 +9,23 @@ describe("NatsContainer", { timeout: 180_000 }, () => { // connect { it("should start, connect and close", async () => { const container = await new NatsContainer(IMAGE).start(); + expect(container.getConnectionOptions().pass).toEqual("test"); + expect(container.getConnectionOptions().user).toEqual("test"); + // establish connection + const nc = await connect(container.getConnectionOptions()); + await nc.close(); + // check if the close was OK + const err = await nc.closed(); + expect(err).toBe(undefined); + + await container.stop(); + }); + // noCredentials { + it("should start, connect and close when noCredentials is true", async () => { + const container = await new NatsContainer(IMAGE).withCredentials(false).start(); + expect(container.getConnectionOptions().user).toBeUndefined(); + expect(container.getConnectionOptions().pass).toBeUndefined(); // establish connection const nc = await connect(container.getConnectionOptions()); // close the connection diff --git a/packages/modules/nats/src/nats-container.ts b/packages/modules/nats/src/nats-container.ts index c3c5eb356..9ea52a288 100755 --- a/packages/modules/nats/src/nats-container.ts +++ b/packages/modules/nats/src/nats-container.ts @@ -10,6 +10,7 @@ const PASS_ARGUMENT_KEY = "--pass"; export class NatsContainer extends GenericContainer { private args = new Set(); private values = new Map(); + private useCredentials = true; constructor(image: string) { super(image); @@ -42,6 +43,18 @@ export class NatsContainer extends GenericContainer { return this; } + public withCredentials(useCredentials: boolean): this { + this.useCredentials = useCredentials; + if (!useCredentials) { + this.args.delete(USER_ARGUMENT_KEY); + this.args.delete(PASS_ARGUMENT_KEY); + } else { + this.withUsername("test"); + this.withPass("test"); + } + return this; + } + public withArg(name: string, value: string): this; public withArg(name: string): this; public withArg(...args: [string, string] | [string]): this { @@ -69,7 +82,11 @@ export class NatsContainer extends GenericContainer { public override async start(): Promise { this.withCommand(this.getNormalizedCommand()); - return new StartedNatsContainer(await super.start(), this.getUser(), this.getPass()); + return new StartedNatsContainer( + await super.start(), + (this.useCredentials && this.getUser()) || undefined, + (this.useCredentials && this.getPass()) || undefined + ); } private getUser(): string | undefined {