Skip to content

Commit 2f1e954

Browse files
committed
Run credential tests with server started in tests
Signed-off-by: Tomasz Pietrek <tomasz@nats.io>
1 parent d1945dc commit 2f1e954

File tree

2 files changed

+30
-3
lines changed

2 files changed

+30
-3
lines changed

Tests/NatsSwiftTests/Integration/ConnectionTests.swift

+25-3
Original file line numberDiff line numberDiff line change
@@ -152,17 +152,39 @@ class CoreNatsTests: XCTestCase {
152152
}
153153

154154
func testUsernameAndPassword() async throws {
155-
natsServer.start()
156155
logger.logLevel = .debug
156+
let currentFile = URL(fileURLWithPath: #file)
157+
// Navigate up to the Tests directory
158+
let testsDir = currentFile.deletingLastPathComponent().deletingLastPathComponent()
159+
// Construct the path to the resource
160+
let resourceURL = testsDir
161+
.appendingPathComponent("Integration/Resources/creds.conf", isDirectory: false)
162+
natsServer.start(cfg: resourceURL.path)
157163
let client = ClientOptions()
158-
.url(URL(string: "nats://localhost:4222")!)
159-
.username_and_password("user", "pass")
164+
.url(URL(string:natsServer.clientURL)!)
165+
.username_and_password("derek", "s3cr3t")
160166
.maxReconnects(5)
161167
.build()
162168
try await client.connect()
163169
try client.publish("msg".data(using: .utf8)!, subject: "test")
164170
try await client.flush()
165171
try await client.subscribe(to: "test")
166172
XCTAssertNotNil(client, "Client should not be nil")
173+
174+
175+
// Test if client with bad credentials throws an error
176+
let bad_creds_client = ClientOptions()
177+
.url(URL(string:natsServer.clientURL)!)
178+
.username_and_password("derek", "badpassword")
179+
.maxReconnects(5)
180+
.build()
181+
182+
do {
183+
try await bad_creds_client.connect()
184+
XCTFail("Should have thrown an error")
185+
} catch {
186+
XCTAssertNotNil(error, "Error should not be nil")
187+
}
188+
167189
}
168190
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
authorization {
2+
user: derek
3+
password: s3cr3t
4+
}
5+

0 commit comments

Comments
 (0)