|
1 | 1 | /* |
2 | | - * Copyright 2020 The NATS Authors |
| 2 | + * Copyright 2020-2024 The NATS Authors |
3 | 3 | * Licensed under the Apache License, Version 2.0 (the "License"); |
4 | 4 | * you may not use this file except in compliance with the License. |
5 | 5 | * You may obtain a copy of the License at |
@@ -251,6 +251,56 @@ func TestGetKeys(t *testing.T) { |
251 | 251 | } |
252 | 252 | } |
253 | 253 |
|
| 254 | +func TestScopeJSON(t *testing.T) { |
| 255 | + ac, apk := makeAccount(t, nil, nil) |
| 256 | + pk := publicKey(createAccountNKey(t), t) |
| 257 | + us := NewUserScope() |
| 258 | + us.Key = pk |
| 259 | + us.Role = "Admin" |
| 260 | + us.Description = "Admin Key" |
| 261 | + us.Template = UserPermissionLimits{ |
| 262 | + Permissions: Permissions{ |
| 263 | + Pub: Permission{Allow: []string{"foo"}}, |
| 264 | + }, |
| 265 | + } |
| 266 | + ac.SigningKeys.AddScopedSigner(us) |
| 267 | + |
| 268 | + token, err := ac.Encode(apk) |
| 269 | + if err != nil { |
| 270 | + t.Fatal(err) |
| 271 | + } |
| 272 | + |
| 273 | + ac, err = DecodeAccountClaims(token) |
| 274 | + if err != nil { |
| 275 | + t.Fatal(err) |
| 276 | + } |
| 277 | + |
| 278 | + s, ok := ac.SigningKeys.GetScope(pk) |
| 279 | + if !ok { |
| 280 | + t.Fatal("expected to find a scope admin") |
| 281 | + } |
| 282 | + us, ok = s.(*UserScope) |
| 283 | + if !ok { |
| 284 | + t.Fatal("expected to find an user scope") |
| 285 | + } |
| 286 | + |
| 287 | + if us.Key != pk { |
| 288 | + t.Fatal("expected public key to match") |
| 289 | + } |
| 290 | + |
| 291 | + if !us.Template.Permissions.Pub.Allow.Contains("foo") { |
| 292 | + t.Fatal("expected permissions to contain foo") |
| 293 | + } |
| 294 | + |
| 295 | + if us.Description != "Admin Key" { |
| 296 | + t.Fatal("expected description to match") |
| 297 | + } |
| 298 | + |
| 299 | + if us.Role != "Admin" { |
| 300 | + t.Fatal("expected role to match") |
| 301 | + } |
| 302 | +} |
| 303 | + |
254 | 304 | func TestJson(t *testing.T) { |
255 | 305 | ac, apk := makeAccount(t, nil, nil) |
256 | 306 | ac.SigningKeys.Add(publicKey(createAccountNKey(t), t)) |
@@ -278,5 +328,4 @@ func TestJson(t *testing.T) { |
278 | 328 | if len(myAcc.SigningKeys) != 3 { |
279 | 329 | t.Fatalf("Expected 3 signing keys got: %d", len(myAcc.SigningKeys)) |
280 | 330 | } |
281 | | - |
282 | 331 | } |
0 commit comments