|
| 1 | +package account_access_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "encoding/json" |
| 6 | + "net/http" |
| 7 | + "net/http/httptest" |
| 8 | + "strings" |
| 9 | + "testing" |
| 10 | + |
| 11 | + "github.com/mailtrap/mailtrap-cli/internal/client" |
| 12 | + "github.com/mailtrap/mailtrap-cli/internal/cmdutil" |
| 13 | + "github.com/mailtrap/mailtrap-cli/internal/commands/account_access" |
| 14 | + "github.com/mailtrap/mailtrap-cli/internal/config" |
| 15 | + "github.com/spf13/viper" |
| 16 | +) |
| 17 | + |
| 18 | +func setupTest(handler http.HandlerFunc) (*cmdutil.Factory, *bytes.Buffer, func()) { |
| 19 | + server := httptest.NewServer(handler) |
| 20 | + c := client.New("test-token") |
| 21 | + c.SetBaseURL(client.BaseGeneral, server.URL) |
| 22 | + buf := &bytes.Buffer{} |
| 23 | + f := &cmdutil.Factory{ |
| 24 | + Config: func() *config.Config { |
| 25 | + return &config.Config{APIToken: "test-token", AccountID: "123"} |
| 26 | + }, |
| 27 | + IOStreams: &cmdutil.IOStreams{ |
| 28 | + Out: buf, |
| 29 | + ErrOut: &bytes.Buffer{}, |
| 30 | + }, |
| 31 | + ClientOverride: c, |
| 32 | + } |
| 33 | + viper.Set("api-token", "test-token") |
| 34 | + viper.Set("account-id", "123") |
| 35 | + viper.Set("output", "table") |
| 36 | + return f, buf, func() { |
| 37 | + server.Close() |
| 38 | + viper.Reset() |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +func TestAccountAccessList(t *testing.T) { |
| 43 | + f, buf, cleanup := setupTest(func(w http.ResponseWriter, r *http.Request) { |
| 44 | + if r.Method != http.MethodGet { |
| 45 | + t.Errorf("expected GET, got %s", r.Method) |
| 46 | + } |
| 47 | + if !strings.Contains(r.URL.Path, "/api/accounts/123/account_accesses") { |
| 48 | + t.Errorf("unexpected path: %s", r.URL.Path) |
| 49 | + } |
| 50 | + w.Header().Set("Content-Type", "application/json") |
| 51 | + json.NewEncoder(w).Encode([]map[string]interface{}{ |
| 52 | + { |
| 53 | + "id": 1, |
| 54 | + "specifier_type": "User", |
| 55 | + "specifier": map[string]interface{}{ |
| 56 | + "id": 10, |
| 57 | + "email": "user@test.com", |
| 58 | + "name": "Test User", |
| 59 | + }, |
| 60 | + "resources": []map[string]interface{}{ |
| 61 | + { |
| 62 | + "resource_id": 123, |
| 63 | + "resource_type": "account", |
| 64 | + "access_level": 100, |
| 65 | + }, |
| 66 | + }, |
| 67 | + }, |
| 68 | + }) |
| 69 | + }) |
| 70 | + defer cleanup() |
| 71 | + |
| 72 | + cmd := account_access.NewCmdAccountAccess(f) |
| 73 | + cmd.SetArgs([]string{"list"}) |
| 74 | + cmd.SetOut(buf) |
| 75 | + |
| 76 | + if err := cmd.Execute(); err != nil { |
| 77 | + t.Fatalf("unexpected error: %v", err) |
| 78 | + } |
| 79 | + |
| 80 | + output := buf.String() |
| 81 | + if !strings.Contains(output, "user@test.com") { |
| 82 | + t.Errorf("expected output to contain 'user@test.com', got:\n%s", output) |
| 83 | + } |
| 84 | + if !strings.Contains(output, "Test User") { |
| 85 | + t.Errorf("expected output to contain 'Test User', got:\n%s", output) |
| 86 | + } |
| 87 | + if !strings.Contains(output, "account") { |
| 88 | + t.Errorf("expected output to contain 'account', got:\n%s", output) |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +func TestAccountAccessListJSON(t *testing.T) { |
| 93 | + f, buf, cleanup := setupTest(func(w http.ResponseWriter, r *http.Request) { |
| 94 | + w.Header().Set("Content-Type", "application/json") |
| 95 | + json.NewEncoder(w).Encode([]map[string]interface{}{ |
| 96 | + { |
| 97 | + "id": 1, |
| 98 | + "specifier_type": "User", |
| 99 | + "specifier": map[string]interface{}{ |
| 100 | + "id": 10, |
| 101 | + "email": "user@test.com", |
| 102 | + "name": "Test User", |
| 103 | + }, |
| 104 | + "resources": []map[string]interface{}{ |
| 105 | + { |
| 106 | + "resource_id": 123, |
| 107 | + "resource_type": "account", |
| 108 | + "access_level": 100, |
| 109 | + }, |
| 110 | + }, |
| 111 | + }, |
| 112 | + }) |
| 113 | + }) |
| 114 | + defer cleanup() |
| 115 | + |
| 116 | + viper.Set("output", "json") |
| 117 | + |
| 118 | + cmd := account_access.NewCmdAccountAccess(f) |
| 119 | + cmd.SetArgs([]string{"list"}) |
| 120 | + cmd.SetOut(buf) |
| 121 | + |
| 122 | + if err := cmd.Execute(); err != nil { |
| 123 | + t.Fatalf("unexpected error: %v", err) |
| 124 | + } |
| 125 | + |
| 126 | + output := buf.String() |
| 127 | + var result []map[string]interface{} |
| 128 | + if err := json.Unmarshal([]byte(output), &result); err != nil { |
| 129 | + t.Fatalf("output is not valid JSON: %v\noutput:\n%s", err, output) |
| 130 | + } |
| 131 | + if len(result) != 1 { |
| 132 | + t.Fatalf("expected 1 access, got %d", len(result)) |
| 133 | + } |
| 134 | + // Verify nested structure is preserved in JSON mode |
| 135 | + specifier, ok := result[0]["specifier"].(map[string]interface{}) |
| 136 | + if !ok { |
| 137 | + t.Fatal("expected 'specifier' to be a nested object") |
| 138 | + } |
| 139 | + if specifier["email"] != "user@test.com" { |
| 140 | + t.Errorf("expected specifier email 'user@test.com', got %v", specifier["email"]) |
| 141 | + } |
| 142 | +} |
| 143 | + |
| 144 | +func TestAccountAccessRemove(t *testing.T) { |
| 145 | + f, buf, cleanup := setupTest(func(w http.ResponseWriter, r *http.Request) { |
| 146 | + if r.Method != http.MethodDelete { |
| 147 | + t.Errorf("expected DELETE, got %s", r.Method) |
| 148 | + } |
| 149 | + if !strings.Contains(r.URL.Path, "/api/accounts/123/account_accesses/1") { |
| 150 | + t.Errorf("unexpected path: %s", r.URL.Path) |
| 151 | + } |
| 152 | + w.WriteHeader(http.StatusOK) |
| 153 | + }) |
| 154 | + defer cleanup() |
| 155 | + |
| 156 | + cmd := account_access.NewCmdAccountAccess(f) |
| 157 | + cmd.SetArgs([]string{"remove", "--id", "1"}) |
| 158 | + cmd.SetOut(buf) |
| 159 | + |
| 160 | + if err := cmd.Execute(); err != nil { |
| 161 | + t.Fatalf("unexpected error: %v", err) |
| 162 | + } |
| 163 | + |
| 164 | + output := buf.String() |
| 165 | + if !strings.Contains(output, "removed successfully") { |
| 166 | + t.Errorf("expected 'removed successfully' in output, got:\n%s", output) |
| 167 | + } |
| 168 | +} |
| 169 | + |
| 170 | +func TestAccountAccessRemoveMissingID(t *testing.T) { |
| 171 | + f, _, cleanup := setupTest(func(w http.ResponseWriter, r *http.Request) {}) |
| 172 | + defer cleanup() |
| 173 | + |
| 174 | + buf := &bytes.Buffer{} |
| 175 | + f.IOStreams.Out = buf |
| 176 | + |
| 177 | + cmd := account_access.NewCmdAccountAccess(f) |
| 178 | + cmd.SetArgs([]string{"remove"}) |
| 179 | + cmd.SetOut(buf) |
| 180 | + |
| 181 | + err := cmd.Execute() |
| 182 | + if err == nil { |
| 183 | + t.Fatal("expected error when --id is missing") |
| 184 | + } |
| 185 | + if !strings.Contains(err.Error(), "--id is required") { |
| 186 | + t.Errorf("expected '--id is required' error, got: %v", err) |
| 187 | + } |
| 188 | +} |
0 commit comments