| 1 | package g8 |
| 2 | |
| 3 | import "testing" |
| 4 | |
| 5 | func TestAuthorizationService_Authorize(t *testing.T) { |
| 6 | authorizationService := NewAuthorizationService().WithToken("token") |
| 7 | if _, authorized := authorizationService.Authorize("token", nil); !authorized { |
| 8 | t.Error("should've returned true") |
| 9 | } |
| 10 | if _, authorized := authorizationService.Authorize("bad-token", nil); authorized { |
| 11 | t.Error("should've returned false") |
| 12 | } |
| 13 | if _, authorized := authorizationService.Authorize("token", []string{"admin"}); authorized { |
| 14 | t.Error("should've returned false") |
| 15 | } |
| 16 | if _, authorized := authorizationService.Authorize("", nil); authorized { |
| 17 | t.Error("should've returned false") |
| 18 | } |
| 19 | } |
| 20 | |
| 21 | func TestAuthorizationService_AuthorizeWithPermissions(t *testing.T) { |
| 22 | authorizationService := NewAuthorizationService().WithClient(NewClient("token").WithPermissions([]string{"a", "b"})) |
| 23 | if _, authorized := authorizationService.Authorize("token", nil); !authorized { |
| 24 | t.Error("should've returned true") |
| 25 | } |
| 26 | if _, authorized := authorizationService.Authorize("token", []string{"a"}); !authorized { |
| 27 | t.Error("should've returned true") |
| 28 | } |
| 29 | if _, authorized := authorizationService.Authorize("token", []string{"b"}); !authorized { |
| 30 | t.Error("should've returned true") |
| 31 | } |
| 32 | if _, authorized := authorizationService.Authorize("token", []string{"a", "b"}); !authorized { |
| 33 | t.Error("should've returned true") |
| 34 | } |
| 35 | if _, authorized := authorizationService.Authorize("token", []string{"c"}); authorized { |
| 36 | t.Error("should've returned false") |
| 37 | } |
| 38 | if _, authorized := authorizationService.Authorize("token", []string{"a", "c"}); authorized { |
| 39 | t.Error("should've returned false") |
| 40 | } |
| 41 | if _, authorized := authorizationService.Authorize("bad-token", nil); authorized { |
| 42 | t.Error("should've returned false") |
| 43 | } |
| 44 | if _, authorized := authorizationService.Authorize("bad-token", []string{"a"}); authorized { |
| 45 | t.Error("should've returned false") |
| 46 | } |
| 47 | if _, authorized := authorizationService.Authorize("", []string{"a"}); authorized { |
| 48 | t.Error("should've returned false") |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | func TestAuthorizationService_WithToken(t *testing.T) { |
| 53 | authorizationService := NewAuthorizationService().WithToken("token") |
| 54 | if _, authorized := authorizationService.Authorize("token", nil); !authorized { |
| 55 | t.Error("should've returned true") |
| 56 | } |
| 57 | if _, authorized := authorizationService.Authorize("bad-token", nil); authorized { |
| 58 | t.Error("should've returned false") |
| 59 | } |
| 60 | if _, authorized := authorizationService.Authorize("token", []string{"admin"}); authorized { |
| 61 | t.Error("should've returned false") |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | func TestAuthorizationService_WithTokens(t *testing.T) { |
| 66 | authorizationService := NewAuthorizationService().WithTokens([]string{"1", "2"}) |
| 67 | if _, authorized := authorizationService.Authorize("1", nil); !authorized { |
| 68 | t.Error("should've returned true") |
| 69 | } |
| 70 | if _, authorized := authorizationService.Authorize("2", nil); !authorized { |
| 71 | t.Error("should've returned true") |
| 72 | } |
| 73 | if _, authorized := authorizationService.Authorize("3", nil); authorized { |
| 74 | t.Error("should've returned false") |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | func TestAuthorizationService_WithClient(t *testing.T) { |
| 79 | authorizationService := NewAuthorizationService().WithClient(NewClient("token").WithPermissions([]string{"a", "b"})) |
| 80 | if _, authorized := authorizationService.Authorize("token", []string{"a", "b"}); !authorized { |
| 81 | t.Error("should've returned true") |
| 82 | } |
| 83 | if _, authorized := authorizationService.Authorize("token", []string{"a"}); !authorized { |
| 84 | t.Error("should've returned true") |
| 85 | } |
| 86 | if _, authorized := authorizationService.Authorize("token", []string{"b"}); !authorized { |
| 87 | t.Error("should've returned true") |
| 88 | } |
| 89 | if _, authorized := authorizationService.Authorize("token", []string{"c"}); authorized { |
| 90 | t.Error("should've returned false") |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | func TestAuthorizationService_WithClients(t *testing.T) { |
| 95 | authorizationService := NewAuthorizationService().WithClients([]*Client{NewClient("1").WithPermission("a"), NewClient("2").WithPermission("b")}) |
| 96 | if _, authorized := authorizationService.Authorize("1", []string{"a"}); !authorized { |
| 97 | t.Error("should've returned true") |
| 98 | } |
| 99 | if _, authorized := authorizationService.Authorize("2", []string{"b"}); !authorized { |
| 100 | t.Error("should've returned true") |
| 101 | } |
| 102 | if _, authorized := authorizationService.Authorize("1", []string{"b"}); authorized { |
| 103 | t.Error("should've returned false") |
| 104 | } |
| 105 | if _, authorized := authorizationService.Authorize("2", []string{"a"}); authorized { |
| 106 | t.Error("should've returned false") |
| 107 | } |
| 108 | } |