| 1 | package g8 |
| 2 | |
| 3 | import "testing" |
| 4 | |
| 5 | func TestClient_HasPermission(t *testing.T) { |
| 6 | client := NewClientWithPermissions("token", []string{"a", "b"}) |
| 7 | if !client.HasPermission("a") { |
| 8 | t.Errorf("client has permissions %s, therefore HasPermission(a) should've been true", client.Permissions) |
| 9 | } |
| 10 | if !client.HasPermission("b") { |
| 11 | t.Errorf("client has permissions %s, therefore HasPermission(b) should've been true", client.Permissions) |
| 12 | } |
| 13 | if client.HasPermission("c") { |
| 14 | t.Errorf("client has permissions %s, therefore HasPermission(c) should've been false", client.Permissions) |
| 15 | } |
| 16 | if client.HasPermission("ab") { |
| 17 | t.Errorf("client has permissions %s, therefore HasPermission(ab) should've been false", client.Permissions) |
| 18 | } |
| 19 | } |
| 20 | |
| 21 | func TestClient_HasPermissions(t *testing.T) { |
| 22 | client := NewClientWithPermissions("token", []string{"a", "b"}) |
| 23 | if !client.HasPermissions(nil) { |
| 24 | t.Errorf("client has permissions %s, therefore HasPermissions(nil) should've been true", client.Permissions) |
| 25 | } |
| 26 | if !client.HasPermissions([]string{"a"}) { |
| 27 | t.Errorf("client has permissions %s, therefore HasPermissions([a]) should've been true", client.Permissions) |
| 28 | } |
| 29 | if !client.HasPermissions([]string{"b"}) { |
| 30 | t.Errorf("client has permissions %s, therefore HasPermissions([b]) should've been true", client.Permissions) |
| 31 | } |
| 32 | if !client.HasPermissions([]string{"a", "b"}) { |
| 33 | t.Errorf("client has permissions %s, therefore HasPermissions([a, b]) should've been true", client.Permissions) |
| 34 | } |
| 35 | if client.HasPermissions([]string{"a", "b", "c"}) { |
| 36 | t.Errorf("client has permissions %s, therefore HasPermissions([a, b, c]) should've been false", client.Permissions) |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | func TestClient_WithData(t *testing.T) { |
| 41 | client := NewClient("token") |
| 42 | if client.Data != nil { |
| 43 | t.Error("expected client data to be nil") |
| 44 | } |
| 45 | client.WithData(5) |
| 46 | if client.Data != 5 { |
| 47 | t.Errorf("expected client data to be 5, got %d", client.Data) |
| 48 | } |
| 49 | client.WithData(map[string]string{"key": "value"}) |
| 50 | if data, ok := client.Data.(map[string]string); !ok || data["key"] != "value" { |
| 51 | t.Errorf("expected client data to be map[string]string{key: value}, got %v", client.Data) |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | func TestNewClientWithData(t *testing.T) { |
| 56 | client := NewClientWithData("token", 5) |
| 57 | if client.Data != 5 { |
| 58 | t.Errorf("expected client data to be 5, got %d", client.Data) |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | func TestNewClientWithPermissionsAndData(t *testing.T) { |
| 63 | client := NewClientWithPermissionsAndData("token", []string{"a", "b"}, 5) |
| 64 | if client.Data != 5 { |
| 65 | t.Errorf("expected client data to be 5, got %d", client.Data) |
| 66 | } |
| 67 | if !client.HasPermission("a") { |
| 68 | t.Errorf("client has permissions %s, therefore HasPermission(a) should've been true", client.Permissions) |
| 69 | } |
| 70 | if !client.HasPermission("b") { |
| 71 | t.Errorf("client has permissions %s, therefore HasPermission(b) should've been true", client.Permissions) |
| 72 | } |
| 73 | if client.HasPermission("c") { |
| 74 | t.Errorf("client has permissions %s, therefore HasPermission(c) should've been false", client.Permissions) |
| 75 | } |
| 76 | } |