|
| 1 | +package incidentio |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "net/http" |
| 7 | + "reflect" |
| 8 | + "testing" |
| 9 | +) |
| 10 | + |
| 11 | +func TestUsersService_List(t *testing.T) { |
| 12 | + client, mux, _, teardown := setup() |
| 13 | + defer teardown() |
| 14 | + |
| 15 | + mux.HandleFunc("/v2/users", func(w http.ResponseWriter, r *http.Request) { |
| 16 | + testMethod(t, r, "GET") |
| 17 | + testHeader(t, r, "Authorization", "Bearer test-key") |
| 18 | + |
| 19 | + response := `{ |
| 20 | + "users": [ |
| 21 | + { |
| 22 | + "id": "01FCNDV6P870EA6S7TK1DSYDG0", |
| 23 | + "name": "John Doe", |
| 24 | + "email": "john@example.com", |
| 25 | + "role": "owner", |
| 26 | + "slack": { |
| 27 | + "user_id": "U01FCNDV6P8", |
| 28 | + "team_id": "T01FCNDV6P8" |
| 29 | + } |
| 30 | + }, |
| 31 | + { |
| 32 | + "id": "01FCNDV6P870EA6S7TK1DSYDG1", |
| 33 | + "name": "Jane Smith", |
| 34 | + "email": "jane@example.com", |
| 35 | + "role": "admin" |
| 36 | + } |
| 37 | + ] |
| 38 | + }` |
| 39 | + |
| 40 | + _, _ = fmt.Fprint(w, response) |
| 41 | + }) |
| 42 | + |
| 43 | + ctx := context.Background() |
| 44 | + users, _, err := client.Users.List(ctx, nil) |
| 45 | + if err != nil { |
| 46 | + t.Errorf("Users.List returned error: %v", err) |
| 47 | + } |
| 48 | + |
| 49 | + expected := []*User{ |
| 50 | + { |
| 51 | + ID: "01FCNDV6P870EA6S7TK1DSYDG0", |
| 52 | + Name: "John Doe", |
| 53 | + Email: "john@example.com", |
| 54 | + Role: "owner", |
| 55 | + Slack: &struct { |
| 56 | + UserID string `json:"user_id"` |
| 57 | + TeamID string `json:"team_id"` |
| 58 | + }{ |
| 59 | + UserID: "U01FCNDV6P8", |
| 60 | + TeamID: "T01FCNDV6P8", |
| 61 | + }, |
| 62 | + }, |
| 63 | + { |
| 64 | + ID: "01FCNDV6P870EA6S7TK1DSYDG1", |
| 65 | + Name: "Jane Smith", |
| 66 | + Email: "jane@example.com", |
| 67 | + Role: "admin", |
| 68 | + }, |
| 69 | + } |
| 70 | + |
| 71 | + if !reflect.DeepEqual(users, expected) { |
| 72 | + t.Errorf("Users.List returned %+v, want %+v", users, expected) |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +func TestUsersService_List_EmptyResponse(t *testing.T) { |
| 77 | + client, mux, _, teardown := setup() |
| 78 | + defer teardown() |
| 79 | + |
| 80 | + mux.HandleFunc("/v2/users", func(w http.ResponseWriter, r *http.Request) { |
| 81 | + testMethod(t, r, "GET") |
| 82 | + testHeader(t, r, "Authorization", "Bearer test-key") |
| 83 | + |
| 84 | + response := `{ |
| 85 | + "users": [] |
| 86 | + }` |
| 87 | + |
| 88 | + _, _ = fmt.Fprint(w, response) |
| 89 | + }) |
| 90 | + |
| 91 | + ctx := context.Background() |
| 92 | + users, _, err := client.Users.List(ctx, nil) |
| 93 | + if err != nil { |
| 94 | + t.Errorf("Users.List returned error: %v", err) |
| 95 | + } |
| 96 | + |
| 97 | + if len(users) != 0 { |
| 98 | + t.Errorf("Users.List returned %d users, want 0", len(users)) |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +func TestUsersService_List_HTTPError(t *testing.T) { |
| 103 | + client, mux, _, teardown := setup() |
| 104 | + defer teardown() |
| 105 | + |
| 106 | + mux.HandleFunc("/v2/users", func(w http.ResponseWriter, r *http.Request) { |
| 107 | + testMethod(t, r, "GET") |
| 108 | + w.WriteHeader(http.StatusInternalServerError) |
| 109 | + response := `{ |
| 110 | + "type": "internal_error", |
| 111 | + "status": 500, |
| 112 | + "detail": "Internal server error" |
| 113 | + }` |
| 114 | + _, _ = fmt.Fprint(w, response) |
| 115 | + }) |
| 116 | + |
| 117 | + ctx := context.Background() |
| 118 | + users, resp, err := client.Users.List(ctx, nil) |
| 119 | + |
| 120 | + if err == nil { |
| 121 | + t.Error("Expected error, got nil") |
| 122 | + } |
| 123 | + |
| 124 | + if users != nil { |
| 125 | + t.Errorf("Users.List returned users %+v, want nil", users) |
| 126 | + } |
| 127 | + |
| 128 | + if resp == nil { |
| 129 | + t.Error("Expected response, got nil") |
| 130 | + } else if resp.StatusCode != http.StatusInternalServerError { |
| 131 | + t.Errorf("Expected status %d, got %d", http.StatusInternalServerError, resp.StatusCode) |
| 132 | + } |
| 133 | +} |
| 134 | + |
| 135 | +func TestUsersService_List_InvalidJSON(t *testing.T) { |
| 136 | + client, mux, _, teardown := setup() |
| 137 | + defer teardown() |
| 138 | + |
| 139 | + mux.HandleFunc("/v2/users", func(w http.ResponseWriter, r *http.Request) { |
| 140 | + testMethod(t, r, "GET") |
| 141 | + response := `{"invalid": json}` |
| 142 | + _, _ = fmt.Fprint(w, response) |
| 143 | + }) |
| 144 | + |
| 145 | + ctx := context.Background() |
| 146 | + users, _, err := client.Users.List(ctx, nil) |
| 147 | + |
| 148 | + if err == nil { |
| 149 | + t.Error("Expected error, got nil") |
| 150 | + } |
| 151 | + |
| 152 | + if users != nil { |
| 153 | + t.Errorf("Users.List returned users %+v, want nil", users) |
| 154 | + } |
| 155 | +} |
| 156 | + |
| 157 | +func TestUsersService_List_WithoutSlack(t *testing.T) { |
| 158 | + client, mux, _, teardown := setup() |
| 159 | + defer teardown() |
| 160 | + |
| 161 | + mux.HandleFunc("/v2/users", func(w http.ResponseWriter, r *http.Request) { |
| 162 | + testMethod(t, r, "GET") |
| 163 | + testHeader(t, r, "Authorization", "Bearer test-key") |
| 164 | + |
| 165 | + response := `{ |
| 166 | + "users": [ |
| 167 | + { |
| 168 | + "id": "01FCNDV6P870EA6S7TK1DSYDG0", |
| 169 | + "name": "John Doe", |
| 170 | + "email": "john@example.com", |
| 171 | + "role": "owner" |
| 172 | + } |
| 173 | + ] |
| 174 | + }` |
| 175 | + |
| 176 | + _, _ = fmt.Fprint(w, response) |
| 177 | + }) |
| 178 | + |
| 179 | + ctx := context.Background() |
| 180 | + users, _, err := client.Users.List(ctx, nil) |
| 181 | + if err != nil { |
| 182 | + t.Errorf("Users.List returned error: %v", err) |
| 183 | + } |
| 184 | + |
| 185 | + if len(users) != 1 { |
| 186 | + t.Errorf("Users.List returned %d users, want 1", len(users)) |
| 187 | + return |
| 188 | + } |
| 189 | + |
| 190 | + user := users[0] |
| 191 | + if user.Slack != nil { |
| 192 | + t.Errorf("Users.List returned user with Slack data %+v, want nil", user.Slack) |
| 193 | + } |
| 194 | + |
| 195 | + expected := &User{ |
| 196 | + ID: "01FCNDV6P870EA6S7TK1DSYDG0", |
| 197 | + Name: "John Doe", |
| 198 | + Email: "john@example.com", |
| 199 | + Role: "owner", |
| 200 | + } |
| 201 | + |
| 202 | + if !reflect.DeepEqual(user, expected) { |
| 203 | + t.Errorf("Users.List returned %+v, want %+v", user, expected) |
| 204 | + } |
| 205 | +} |
0 commit comments