added user creation, login, and logout test. Linted. Go mod. Adding http.StatusOK const in a few places I noticed it was not in.

This commit is contained in:
grumbulon 2023-01-28 23:39:26 -05:00
parent 7e72056a6e
commit 9412d8ec28
4 changed files with 147 additions and 4 deletions

3
go.mod
View file

@ -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

2
go.sum
View file

@ -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=

142
internal/api/api_test.go Normal file
View file

@ -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)
}
}

View file

@ -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")