(t *testing.T)
| 163 | } |
| 164 | |
| 165 | func TestBatchCheckRESTHandler(t *testing.T) { |
| 166 | nspaces := []*namespace.Namespace{{ |
| 167 | Name: "batch-check-handler", |
| 168 | }} |
| 169 | |
| 170 | reg := driver.NewSqliteTestRegistry(t, false, driver.WithNamespaces(nspaces)) |
| 171 | h := check.NewHandler(reg) |
| 172 | r := httprouterx.NewTestRouterPublic(t) |
| 173 | h.RegisterReadRoutes(r) |
| 174 | ts := httptest.NewServer(r) |
| 175 | defer ts.Close() |
| 176 | |
| 177 | t.Run("case=returns bad request on non-int max depth", func(t *testing.T) { |
| 178 | resp, err := ts.Client().Post(ts.URL+check.BatchRoute+"?max-depth=foo", |
| 179 | "application/json", nil) |
| 180 | require.NoError(t, err) |
| 181 | |
| 182 | assert.Equal(t, http.StatusBadRequest, resp.StatusCode) |
| 183 | body, err := io.ReadAll(resp.Body) |
| 184 | require.NoError(t, err) |
| 185 | assert.Contains(t, string(body), "invalid syntax") |
| 186 | }) |
| 187 | |
| 188 | t.Run("case=returns bad request on invalid request body", func(t *testing.T) { |
| 189 | resp, err := ts.Client().Post(buildBatchURL(ts.URL, "5"), |
| 190 | "application/json", strings.NewReader("not-json")) |
| 191 | require.NoError(t, err) |
| 192 | |
| 193 | assert.Equal(t, http.StatusBadRequest, resp.StatusCode) |
| 194 | body, err := io.ReadAll(resp.Body) |
| 195 | require.NoError(t, err) |
| 196 | assert.Contains(t, string(body), "could not unmarshal json") |
| 197 | }) |
| 198 | |
| 199 | t.Run("case=returns bad request with too many tuples", func(t *testing.T) { |
| 200 | tuples := make([]client.Relationship, 11) |
| 201 | for i := 0; i < len(tuples); i++ { |
| 202 | tuples[i] = client.Relationship{ |
| 203 | Namespace: "n", |
| 204 | Object: "o", |
| 205 | Relation: "r", |
| 206 | SubjectId: new("s"), |
| 207 | } |
| 208 | } |
| 209 | reqBody := client.BatchCheckPermissionBody{Tuples: tuples} |
| 210 | bodyBytes, err := json.Marshal(reqBody) |
| 211 | require.NoError(t, err) |
| 212 | |
| 213 | resp, err := ts.Client().Post(buildBatchURL(ts.URL, "5"), |
| 214 | "application/json", bytes.NewReader(bodyBytes)) |
| 215 | require.NoError(t, err) |
| 216 | |
| 217 | assert.Equal(t, http.StatusBadRequest, resp.StatusCode) |
| 218 | body, err := io.ReadAll(resp.Body) |
| 219 | require.NoError(t, err) |
| 220 | assert.Contains(t, string(body), "batch exceeds max size of 10") |
| 221 | }) |
| 222 |
nothing calls this directly
no test coverage detected