Jelajahi Sumber

mostly done docs

Alexander Belanger 5 tahun lalu
induk
melakukan
8b95341e56
3 mengubah file dengan 43 tambahan dan 5 penghapusan
  1. 40 2
      docs/API.md
  2. 2 2
      server/api/user_handler.go
  3. 1 1
      server/api/user_handler_test.go

+ 40 - 2
docs/API.md

@@ -1,3 +1,9 @@
+### Overview
+
+This is the API specification that the Go server is implementing. 
+
+**Error handling:**
+
 Errors are passed via both a non-`2xx` status code and an HTTPError response body:
 
 ```js
@@ -9,6 +15,10 @@ HTTPError{
 }
 ```
 
+Internal server errors are shared across all endpoints and are listed in the [Global Errors](#global-errors) section. 
+
+**Authentication:** The current authentication method is cookie-based sessions--most endpoints require a cookie-based session. 
+
 ### Global Errors
 
 #### `ErrorDataWrite`
@@ -92,6 +102,8 @@ User{
 }
 ```
 
+**Successful Status Code**: `200`
+
 **Errors:**
 
 - Invalid `id` URL parameter
@@ -129,6 +141,8 @@ User{
 }
 ```
 
+**Successful Status Code**: `200`
+
 **Errors:** 
 
 - Invalid `id` URL parameter
@@ -166,6 +180,8 @@ User{
 }
 ```
 
+**Successful Status Code**: `200`
+
 **Errors:** 
 
 - Invalid `id` URL parameter
@@ -199,6 +215,8 @@ User{
 
 **Successful Response Body**: N/A
 
+**Successful Status Code**: `201`
+
 **Errors:**
 
 - Invalid email (example: `{"email": "notanemail"}`)
@@ -233,7 +251,7 @@ User{
 
 #### `PUT /api/users/{id}`
 
-**Description:** Updates an existing user
+**Description:** Updates an existing user.
 
 **URL parameters:** 
 
@@ -245,6 +263,8 @@ User{
 
 **Successful Response Body**: N/A
 
+**Successful Status Code**: `204`
+
 **Errors:** 
 
 - Invalid `id` URL parameter
@@ -259,7 +279,7 @@ User{
 
 #### `DELETE /api/users/{id}`
 
-**Description:** Deletes an existing user
+**Description:** Deletes an existing user, requires the password to be sent before deletion. 
 
 **URL parameters:** 
 
@@ -269,10 +289,28 @@ User{
 
 **Request body:**
 
+```js
+{
+    "password": String,
+}
+```
+
 **Successful Response Body**: N/A
 
+**Successful Status Code**: `204`
+
 **Errors:** 
 
+- Invalid `password`
+  - Status Code: `400`
+  - Request Body:
+    ```json
+    {
+        "code":601,
+        "errors":["invalid password"]
+    }
+    ```
+
 - Invalid `id` URL parameter
   - Status Code: `400`
   - Request Body:

+ 2 - 2
server/api/user_handler.go

@@ -86,7 +86,7 @@ func (app *App) HandleUpdateUser(w http.ResponseWriter, r *http.Request) {
 
 	if err == nil {
 		app.logger.Info().Msgf("User updated: %d", user.ID)
-		w.WriteHeader(http.StatusAccepted)
+		w.WriteHeader(http.StatusNoContent)
 	}
 }
 
@@ -109,7 +109,7 @@ func (app *App) HandleDeleteUser(w http.ResponseWriter, r *http.Request) {
 
 	if err == nil {
 		app.logger.Info().Msgf("User deleted: %d", user.ID)
-		w.WriteHeader(http.StatusAccepted)
+		w.WriteHeader(http.StatusNoContent)
 	}
 }
 

+ 1 - 1
server/api/user_handler_test.go

@@ -267,7 +267,7 @@ var updateUserTests = []userTest{
 		method:    "PUT",
 		endpoint:  "/api/users/1",
 		body:      `{"rawKubeConfig":"apiVersion: v1\nkind: Config\npreferences: {}\ncurrent-context: default\nclusters:\n- cluster:\n    server: https://localhost\n  name: cluster-test\ncontexts:\n- context:\n    cluster: cluster-test\n    user: test-admin\n  name: context-test\nusers:\n- name: test-admin", "allowedClusters":[]}`,
-		expStatus: http.StatusAccepted,
+		expStatus: http.StatusNoContent,
 		expBody:   "",
 		canQuery:  true,
 		validate: func(r *chi.Mux, t *testing.T) {