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

Function TestObjectPutPermissionViolation

jetstream/test/object_test.go:1320–1376  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

1318}
1319
1320func TestObjectPutPermissionViolation(t *testing.T) {
1321 conf := createConfFile(t, []byte(`
1322 listen: 127.0.0.1:-1
1323 jetstream: {max_mem_store: 64GB, max_file_store: 10TB}
1324 no_auth_user: guest
1325 accounts: {
1326 JS: {
1327 jetstream: enabled
1328 users: [ {user: guest, password: "", permissions: {
1329 publish: { deny: "$O.>" }
1330 }}]
1331 }
1332 }
1333 `))
1334 defer os.Remove(conf)
1335
1336 s, _ := RunServerWithConfig(conf)
1337 defer shutdownJSServerAndRemoveStorage(t, s)
1338
1339 nc, err := nats.Connect(s.ClientURL())
1340 if err != nil {
1341 t.Fatalf("Unexpected error: %v", err)
1342 }
1343 defer nc.Close()
1344
1345 js, err := jetstream.New(nc, jetstream.WithDefaultTimeout(200*time.Millisecond))
1346 if err != nil {
1347 t.Fatal(err)
1348 }
1349
1350 obs, err := js.CreateObjectStore(context.Background(), jetstream.ObjectStoreConfig{Bucket: "TEST"})
1351 expectOk(t, err)
1352
1353 // default timeout from js
1354 start := time.Now()
1355 _, err = obs.PutBytes(context.Background(), "test-object", []byte("test data"))
1356
1357 if !errors.Is(err, jetstream.ErrAsyncPublishTimeout) {
1358 t.Fatal("Expected timeout waiting for publish ack")
1359 }
1360 if elapsed := time.Since(start); elapsed < 200*time.Millisecond {
1361 t.Fatalf("Expected at least 200ms timeout, got: %v", elapsed)
1362 }
1363
1364 // context timeout
1365 ctx, cancel := context.WithTimeout(context.Background(), 100*time.Millisecond)
1366 defer cancel()
1367
1368 start = time.Now()
1369 _, err = obs.PutBytes(ctx, "test-object", []byte("test data"))
1370 if !errors.Is(err, nats.ErrTimeout) {
1371 t.Fatal("Expected timeout waiting for publish ack")
1372 }
1373 if elapsed := time.Since(start); elapsed < 100*time.Millisecond || elapsed > 200*time.Millisecond {
1374 t.Fatalf("Expected at least 100ms timeout, got: %v", elapsed)
1375 }
1376}

Callers

nothing calls this directly

Calls 12

NewFunction · 0.92
WithDefaultTimeoutFunction · 0.92
ConnectMethod · 0.80
FatalfMethod · 0.80
createConfFileFunction · 0.70
RunServerWithConfigFunction · 0.70
expectOkFunction · 0.70
CreateObjectStoreMethod · 0.65
PutBytesMethod · 0.65
CloseMethod · 0.45
IsMethod · 0.45

Tested by

no test coverage detected