Sfoglia il codice sorgente

finished tests for api-users

Alexander Belanger 5 anni fa
parent
commit
91e4e253e9
2 ha cambiato i file con 125 aggiunte e 20 eliminazioni
  1. 17 19
      server/api/user_handler.go
  2. 108 1
      server/api/user_handler_test.go

+ 17 - 19
server/api/user_handler.go

@@ -92,30 +92,28 @@ func (app *App) HandleReadUserClusters(w http.ResponseWriter, r *http.Request) {
 func (app *App) HandleReadUserClustersAll(w http.ResponseWriter, r *http.Request) {
 	user, err := app.readUser(w, r)
 
-	// error already handled by helper
-	if err != nil {
-		return
-	}
+	// if there is an error, it's already handled by helper
+	if err == nil {
+		clusters, err := kubernetes.GetAllClusterConfigsFromBytes(user.RawKubeConfig)
 
-	clusters, err := kubernetes.GetAllClusterConfigsFromBytes(user.RawKubeConfig)
+		if err != nil {
+			app.handleErrorFormDecoding(err, ErrUserDecode, w)
+			return
+		}
 
-	if err != nil {
-		app.handleErrorFormDecoding(err, ErrUserDecode, w)
-		return
-	}
+		extClusters := make([]models.ClusterConfigExternal, 0)
 
-	extClusters := make([]models.ClusterConfigExternal, 0)
+		for _, cluster := range clusters {
+			extClusters = append(extClusters, *cluster.Externalize())
+		}
 
-	for _, cluster := range clusters {
-		extClusters = append(extClusters, *cluster.Externalize())
-	}
+		if err := json.NewEncoder(w).Encode(extClusters); err != nil {
+			app.handleErrorFormDecoding(err, ErrUserDecode, w)
+			return
+		}
 
-	if err := json.NewEncoder(w).Encode(extClusters); err != nil {
-		app.handleErrorFormDecoding(err, ErrUserDecode, w)
-		return
+		w.WriteHeader(http.StatusOK)
 	}
-
-	w.WriteHeader(http.StatusOK)
 }
 
 // HandleUpdateUser validates an update user form entry, updates the user
@@ -140,7 +138,7 @@ func (app *App) HandleUpdateUser(w http.ResponseWriter, r *http.Request) {
 	}
 }
 
-// HandleDeleteUser is majestic
+// HandleDeleteUser removes a user after checking that the sent password is correct
 func (app *App) HandleDeleteUser(w http.ResponseWriter, r *http.Request) {
 	id, err := strconv.ParseUint(chi.URLParam(r, "id"), 0, 64)
 

+ 108 - 1
server/api/user_handler_test.go

@@ -131,7 +131,7 @@ var createUserTests = []userTest{
 		},
 	},
 	userTest{
-		msg:      "Create user cannot write to db",
+		msg:      "Create user db connection down",
 		method:   "POST",
 		endpoint: "/api/users",
 		body: `{
@@ -166,6 +166,21 @@ var createUserTests = []userTest{
 			BasicBodyValidator,
 		},
 	},
+	userTest{
+		msg:      "Create user invalid field type",
+		method:   "POST",
+		endpoint: "/api/users",
+		body: `{
+			"email": "belanger@getporter.dev",
+			"password": 0
+		}`,
+		expStatus: http.StatusBadRequest,
+		expBody:   `{"code":600,"errors":["could not process request"]}`,
+		canQuery:  true,
+		validators: []func(rr *httptest.ResponseRecorder, c userTest, r *chi.Mux, t *testing.T){
+			BasicBodyValidator,
+		},
+	},
 }
 
 func TestHandleCreateUser(t *testing.T) {
@@ -303,6 +318,26 @@ var readUserClustersAllTests = []userTest{
 			ClusterBodyValidator,
 		},
 	},
+	userTest{
+		init: func(repo *repository.Repository) {
+			repo.User.CreateUser(&models.User{
+				Email:         "belanger@getporter.dev",
+				Password:      "hello",
+				Clusters:      []models.ClusterConfig{},
+				RawKubeConfig: []byte("apiVersion: \xc5\n"),
+			})
+		},
+		msg:       "Read user with invalid utf-8 \xc5 in kubeconfig",
+		method:    "GET",
+		endpoint:  "/api/users/1/clusters/all",
+		body:      "",
+		expStatus: http.StatusBadRequest,
+		expBody:   `{"code":600,"errors":["could not process request"]}`,
+		canQuery:  true,
+		validators: []func(rr *httptest.ResponseRecorder, c userTest, r *chi.Mux, t *testing.T){
+			BasicBodyValidator,
+		},
+	},
 }
 
 func TestHandleReadUserClustersAll(t *testing.T) {
@@ -372,6 +407,42 @@ var updateUserTests = []userTest{
 			BasicBodyValidator,
 		},
 	},
+	userTest{
+		init: func(repo *repository.Repository) {
+			repo.User.CreateUser(&models.User{
+				Email:    "belanger@getporter.dev",
+				Password: "hello",
+			})
+		},
+		msg:       "Update user bad kubeconfig",
+		method:    "PUT",
+		endpoint:  "/api/users/1",
+		body:      `{"rawKubeConfig":"notvalidyaml", "allowedClusters":[]}`,
+		expStatus: http.StatusBadRequest,
+		expBody:   `{"code":600,"errors":["could not process request"]}`,
+		canQuery:  true,
+		validators: []func(rr *httptest.ResponseRecorder, c userTest, r *chi.Mux, t *testing.T){
+			BasicBodyValidator,
+		},
+	},
+	userTest{
+		init: func(repo *repository.Repository) {
+			repo.User.CreateUser(&models.User{
+				Email:    "belanger@getporter.dev",
+				Password: "hello",
+			})
+		},
+		msg:       "Update user db connection down",
+		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.StatusInternalServerError,
+		expBody:   `{"code":500,"errors":["could not write to database"]}`,
+		canQuery:  false,
+		validators: []func(rr *httptest.ResponseRecorder, c userTest, r *chi.Mux, t *testing.T){
+			BasicBodyValidator,
+		},
+	},
 }
 
 func TestHandleUpdateUser(t *testing.T) {
@@ -429,6 +500,42 @@ var deleteUserTests = []userTest{
 			},
 		},
 	},
+	userTest{
+		init: func(repo *repository.Repository) {
+			repo.User.CreateUser(&models.User{
+				Email:    "belanger@getporter.dev",
+				Password: "hello",
+			})
+		},
+		msg:       "Delete user invalid id",
+		method:    "DELETE",
+		endpoint:  "/api/users/aldkjf",
+		body:      `{"password":"hello"}`,
+		expStatus: http.StatusBadRequest,
+		expBody:   `{"code":600,"errors":["could not process request"]}`,
+		canQuery:  true,
+		validators: []func(rr *httptest.ResponseRecorder, c userTest, r *chi.Mux, t *testing.T){
+			BasicBodyValidator,
+		},
+	},
+	userTest{
+		init: func(repo *repository.Repository) {
+			repo.User.CreateUser(&models.User{
+				Email:    "belanger@getporter.dev",
+				Password: "hello",
+			})
+		},
+		msg:       "Delete user missing password",
+		method:    "DELETE",
+		endpoint:  "/api/users/1",
+		body:      `{}`,
+		expStatus: http.StatusUnprocessableEntity,
+		expBody:   `{"code":601,"errors":["required validation failed"]}`,
+		canQuery:  true,
+		validators: []func(rr *httptest.ResponseRecorder, c userTest, r *chi.Mux, t *testing.T){
+			BasicBodyValidator,
+		},
+	},
 }
 
 func TestHandleDeleteUser(t *testing.T) {