From a78b6cc9a6ff6953da9e84d0850b5d4022ebbe0e Mon Sep 17 00:00:00 2001 From: Edward Viaene Date: Tue, 27 Aug 2024 13:02:48 -0500 Subject: [PATCH] fix issue with creating a new user --- Makefile | 2 +- latest | 2 +- pkg/rest/types.go | 6 +++++ pkg/rest/users.go | 2 +- pkg/rest/users_test.go | 36 ++++++++++++++++++++++++++++++ pkg/users/localusers.go | 2 +- pkg/users/types.go | 1 + provisioning/scripts/install_s3.sh | 2 +- 8 files changed, 48 insertions(+), 5 deletions(-) diff --git a/Makefile b/Makefile index 93e7ee8..b47e1ca 100644 --- a/Makefile +++ b/Makefile @@ -47,7 +47,7 @@ install-digitalocean: cd provisioning && packer build packer-digitalocean-amd64.pkr.hcl install-s3: - cd provisioning && AWS_PROFILE=in4it-vpn-server scripts/install_s3.sh + cd provisioning && AWS_PROFILE=in4it-vpn-server scripts/install_s3.sh --release install-docs: provisioning/scripts/deploy_documentation.sh diff --git a/latest b/latest index 56130fb..0f1acbd 100644 --- a/latest +++ b/latest @@ -1 +1 @@ -v1.1.1 +v1.1.2 diff --git a/pkg/rest/types.go b/pkg/rest/types.go index a236cb0..ff7fdc0 100644 --- a/pkg/rest/types.go +++ b/pkg/rest/types.go @@ -199,3 +199,9 @@ type UserStatsDataPoint struct { X string `json:"x"` Y float64 `json:"y"` } + +type NewUserRequest struct { + Login string `json:"login"` + Role string `json:"role"` + Password string `json:"password,omitempty"` +} diff --git a/pkg/rest/users.go b/pkg/rest/users.go index e79a69e..2dcdcc5 100644 --- a/pkg/rest/users.go +++ b/pkg/rest/users.go @@ -82,7 +82,7 @@ func (c *Context) usersHandler(w http.ResponseWriter, r *http.Request) { } c.write(w, out) case http.MethodPost: - var user users.User + var user NewUserRequest decoder := json.NewDecoder(r.Body) err := decoder.Decode(&user) if err != nil { diff --git a/pkg/rest/users_test.go b/pkg/rest/users_test.go index faccc4e..c95e920 100644 --- a/pkg/rest/users_test.go +++ b/pkg/rest/users_test.go @@ -153,3 +153,39 @@ func TestCreateUserConnectionDeleteUserFlow(t *testing.T) { t.Fatalf("could read user config file, expected not to") } } + +func TestCreateUser(t *testing.T) { + // first create a new user + storage := &testingmocks.MockMemoryStorage{} + + c, err := newContext(storage, SERVER_TYPE_VPN) + if err != nil { + t.Fatalf("Cannot create context") + } + + err = c.UserStore.Empty() + if err != nil { + t.Fatalf("Cannot create context") + } + + // create a user + payload := []byte(`{"id": "", "login": "testuser", "password": "tttt213", "role": "user", "oidcID": "", "samlID": "", "lastLogin": "", "provisioned": false, "role":"user","samlID":"","suspended":false}`) + req := httptest.NewRequest("POST", "http://example.com/users", bytes.NewBuffer(payload)) + w := httptest.NewRecorder() + c.usersHandler(w, req) + + resp := w.Result() + + if resp.StatusCode != 200 { + t.Fatalf("status code is not 200: %d", resp.StatusCode) + } + + defer resp.Body.Close() + + var user users.User + err = json.NewDecoder(resp.Body).Decode(&user) + if err != nil { + t.Fatalf("Cannot decode response from create user: %s", err) + } + +} diff --git a/pkg/users/localusers.go b/pkg/users/localusers.go index ba8ef93..55be548 100644 --- a/pkg/users/localusers.go +++ b/pkg/users/localusers.go @@ -9,7 +9,7 @@ import ( func (u *UserStore) AddUser(user User) (User, error) { if user.Login == "" { - return user, fmt.Errorf("Login cannot be empty") + return user, fmt.Errorf("login cannot be empty") } existingUsers := u.ListUsers() for _, existingUser := range existingUsers { diff --git a/pkg/users/types.go b/pkg/users/types.go index 2ebe9d8..0c2bc65 100644 --- a/pkg/users/types.go +++ b/pkg/users/types.go @@ -27,6 +27,7 @@ type User struct { ExternalID string `json:"externalID,omitempty"` LastLogin time.Time `json:"lastLogin"` } + type Factor struct { Name string `json:"name"` Type string `json:"type"` diff --git a/provisioning/scripts/install_s3.sh b/provisioning/scripts/install_s3.sh index 90f75aa..2115190 100755 --- a/provisioning/scripts/install_s3.sh +++ b/provisioning/scripts/install_s3.sh @@ -8,5 +8,5 @@ aws s3 cp ../configmanager-linux-amd64 s3://in4it-vpn-server/assets/binaries/${L aws s3 cp ../configmanager-linux-amd64.sha256 s3://in4it-vpn-server/assets/binaries/${LATEST}/configmanager-linux-amd64.sha256 if [ "$1" == "--release" ] ; then echo "=> $LATEST released." - #aws s3 cp ../latest s3://in4it-vpn-server/assets/binaries/latest + aws s3 cp ../latest s3://in4it-vpn-server/assets/binaries/latest fi