MCPcopy
hub / github.com/nats-io/nats.go / TestBasicUserJWTAuth

Function TestBasicUserJWTAuth

test/nats_test.go:202–241  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

200}
201
202func TestBasicUserJWTAuth(t *testing.T) {
203 if server.VERSION[0] == '1' {
204 t.Skip()
205 }
206 ts := runTrustServer()
207 defer ts.Shutdown()
208
209 url := fmt.Sprintf("nats://127.0.0.1:%d", TEST_PORT)
210 _, err := nats.Connect(url)
211 if err == nil {
212 t.Fatalf("Expecting an error on connect")
213 }
214
215 jwtCB := func() (string, error) {
216 return uJWT, nil
217 }
218 sigCB := func(nonce []byte) ([]byte, error) {
219 kp, _ := nkeys.FromSeed(uSeed)
220 sig, _ := kp.Sign(nonce)
221 return sig, nil
222 }
223
224 // Try with user jwt but no sig
225 _, err = nats.Connect(url, nats.UserJWT(jwtCB, nil))
226 if err == nil {
227 t.Fatalf("Expecting an error on connect")
228 }
229
230 // Try with user callback
231 _, err = nats.Connect(url, nats.UserJWT(nil, sigCB))
232 if err == nil {
233 t.Fatalf("Expecting an error on connect")
234 }
235
236 nc, err := nats.Connect(url, nats.UserJWT(jwtCB, sigCB))
237 if err != nil {
238 t.Fatalf("Expected to connect, got %v", err)
239 }
240 nc.Close()
241}
242
243func TestUserCredentialsTwoFiles(t *testing.T) {
244 if server.VERSION[0] == '1' {

Callers

nothing calls this directly

Calls 4

runTrustServerFunction · 0.85
ConnectMethod · 0.80
FatalfMethod · 0.80
CloseMethod · 0.45

Tested by

no test coverage detected