From 9412d8ec28df16f5a244aa2e5bcad3789e98fcf4 Mon Sep 17 00:00:00 2001 From: grumbulon Date: Sat, 28 Jan 2023 23:39:26 -0500 Subject: [PATCH] added user creation, login, and logout test. Linted. Go mod. Adding http.StatusOK const in a few places I noticed it was not in. --- go.mod | 3 + go.sum | 2 - internal/api/api_test.go | 142 +++++++++++++++++++++++++++++++++++++++ internal/api/auth.go | 4 +- 4 files changed, 147 insertions(+), 4 deletions(-) create mode 100644 internal/api/api_test.go diff --git a/go.mod b/go.mod index 5124810..a6b3308 100644 --- a/go.mod +++ b/go.mod @@ -16,6 +16,7 @@ require ( require ( github.com/KyleBanks/depth v1.2.1 // indirect github.com/cespare/xxhash/v2 v2.2.0 // indirect + github.com/davecgh/go-spew v1.1.1 // indirect github.com/go-openapi/jsonpointer v0.19.5 // indirect github.com/go-openapi/jsonreference v0.20.0 // indirect github.com/go-openapi/spec v0.20.6 // indirect @@ -23,6 +24,7 @@ require ( github.com/josharian/intern v1.0.0 // indirect github.com/mailru/easyjson v0.7.6 // indirect github.com/mattn/go-colorable v0.1.12 // indirect + github.com/pmezard/go-difflib v1.0.0 // indirect github.com/rs/zerolog v1.27.0 // indirect github.com/swaggo/files v0.0.0-20220610200504-28940afbdbfe // indirect gopkg.in/yaml.v2 v2.4.0 // indirect @@ -45,6 +47,7 @@ require ( github.com/lestrrat-go/option v1.0.1 // indirect github.com/mattn/go-isatty v0.0.17 // indirect github.com/remyoudompheng/bigfft v0.0.0-20200410134404-eec4a21b6bb0 // indirect + github.com/stretchr/testify v1.8.1 github.com/swaggo/http-swagger v1.3.3 golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4 // indirect golang.org/x/net v0.5.0 // indirect diff --git a/go.sum b/go.sum index 4a0864d..b02d0dc 100644 --- a/go.sum +++ b/go.sum @@ -112,8 +112,6 @@ github.com/swaggo/files v0.0.0-20220610200504-28940afbdbfe h1:K8pHPVoTgxFJt1lXuI github.com/swaggo/files v0.0.0-20220610200504-28940afbdbfe/go.mod h1:lKJPbtWzJ9JhsTN1k1gZgleJWY/cqq0psdoMmaThG3w= github.com/swaggo/http-swagger v1.3.3 h1:Hu5Z0L9ssyBLofaama21iYaF2VbWyA8jdohaaCGpHsc= github.com/swaggo/http-swagger v1.3.3/go.mod h1:sE+4PjD89IxMPm77FnkDz0sdO+p5lbXzrVWT6OTVVGo= -github.com/swaggo/swag v1.8.9 h1:kHtaBe/Ob9AZzAANfcn5c6RyCke9gG9QpH0jky0I/sA= -github.com/swaggo/swag v1.8.9/go.mod h1:ezQVUUhly8dludpVk+/PuwJWvLLanB13ygV5Pr9enSk= github.com/swaggo/swag v1.8.10 h1:eExW4bFa52WOjqRzRD58bgWsWfdFJso50lpbeTcmTfo= github.com/swaggo/swag v1.8.10/go.mod h1:ezQVUUhly8dludpVk+/PuwJWvLLanB13ygV5Pr9enSk= github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= diff --git a/internal/api/api_test.go b/internal/api/api_test.go new file mode 100644 index 0000000..1e61007 --- /dev/null +++ b/internal/api/api_test.go @@ -0,0 +1,142 @@ +package api + +import ( + "encoding/json" + "io" + "log" + "net/http" + "net/url" + "strings" + "testing" + + "git.freecumextremist.com/grumbulon/pomme/internal" + "git.freecumextremist.com/grumbulon/pomme/internal/db" + "github.com/stretchr/testify/assert" +) + +type response struct { + Username string `json:"username"` + Message string `json:"message"` + Status int `json:"status"` +} + +type accountTest struct { + username string + password string +} + +func TestInit(t *testing.T) { + tester := accountTest{ + username: autoUname(), + password: "merde", + } + + tester.TestMakeAccount(t) + tester.TestLogin(t) + tester.TestLogout(t) + tester.CleanUpDb() +} + +func (a *accountTest) TestMakeAccount(t *testing.T) { + var target response + + client := http.Client{} + + form := url.Values{} + + form.Add("username", a.username) + + form.Add("password", "test") + + if req, err := http.NewRequest(http.MethodPost, "http://localhost:3010/api/create", strings.NewReader(form.Encode())); err == nil { + req.Header.Add("Content-Type", "application/x-www-form-urlencoded") + + resp, err := client.Do(req) + if err != nil { + assert.NotNil(t, err) + } + + respBody, _ := io.ReadAll(resp.Body) + + err = json.Unmarshal(respBody, &target) + if err != nil { + assert.NotNil(t, err) + } + + log.Println(target) + assert.Equal(t, http.StatusCreated, target.Status) + } +} + +func (a *accountTest) TestLogin(t *testing.T) { + var target response + + client := http.Client{} + + form := url.Values{} + + form.Add("username", a.username) + + form.Add("password", "test") + + if req, err := http.NewRequest(http.MethodPost, "http://localhost:3010/api/login", strings.NewReader(form.Encode())); err == nil { + req.Header.Add("Content-Type", "application/x-www-form-urlencoded") + + resp, err := client.Do(req) + if err != nil { + assert.NotNil(t, err) + } + + respBody, _ := io.ReadAll(resp.Body) + + err = json.Unmarshal(respBody, &target) + if err != nil { + assert.NotNil(t, err) + } + + log.Println(target) + assert.Equal(t, http.StatusOK, target.Status) + } +} + +func (a *accountTest) TestLogout(t *testing.T) { + var target response + + client := http.Client{} + + form := url.Values{} + + form.Add("username", a.username) + + if req, err := http.NewRequest(http.MethodPost, "http://localhost:3010/api/logout", strings.NewReader(form.Encode())); err == nil { + req.Header.Add("Content-Type", "application/x-www-form-urlencoded") + + resp, err := client.Do(req) + if err != nil { + assert.NotNil(t, err) + } + + respBody, _ := io.ReadAll(resp.Body) + + err = json.Unmarshal(respBody, &target) + if err != nil { + assert.NotNil(t, err) + } + + log.Println(target) + assert.Equal(t, http.StatusOK, target.Status) + } +} + +// currently does not work. +func (a *accountTest) CleanUpDb() { + var user internal.User + + db := db.InitDb() + + db.Where("username = ?", a.username).First(&user) + + if user.Username != "" { + db.Delete(&user, user.ID) + } +} diff --git a/internal/api/auth.go b/internal/api/auth.go index 39aa347..0b863f8 100644 --- a/internal/api/auth.go +++ b/internal/api/auth.go @@ -120,7 +120,7 @@ func Login(w http.ResponseWriter, r *http.Request) { err = json.NewEncoder(w).Encode( internal.Response{ Message: "Successfully logged in", - HTTPResponse: 200, + HTTPResponse: http.StatusOK, }) if err != nil { @@ -148,7 +148,7 @@ func Logout(w http.ResponseWriter, r *http.Request) { err := json.NewEncoder(w).Encode( internal.Response{ Message: "Successfully logged out", - HTTPResponse: 200, + HTTPResponse: http.StatusOK, }) if err != nil { internalServerError(w, "internal server error")