mirror of
https://git.freecumextremist.com/grumbulon/pomme.git
synced 2024-07-01 08:06:05 +00:00
175 lines
3.5 KiB
Go
175 lines
3.5 KiB
Go
package api
|
|
|
|
import (
|
|
"encoding/json"
|
|
"io"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"net/url"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"git.freecumextremist.com/grumbulon/pomme/internal"
|
|
"git.freecumextremist.com/grumbulon/pomme/internal/db"
|
|
"github.com/go-chi/chi/v5"
|
|
"github.com/stretchr/testify/assert"
|
|
"golang.org/x/crypto/bcrypt"
|
|
)
|
|
|
|
type response struct {
|
|
Username string `json:"username"`
|
|
Message string `json:"message"`
|
|
Status int `json:"status"`
|
|
}
|
|
|
|
type accountTest struct {
|
|
username string
|
|
password string
|
|
url string
|
|
}
|
|
|
|
func TestAPI(t *testing.T) {
|
|
config, err := internal.ReadConfig()
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
pomme := chi.NewRouter()
|
|
|
|
pomme.Mount("/api", API())
|
|
|
|
s := &http.Server{
|
|
ReadTimeout: 3 * time.Second,
|
|
WriteTimeout: 15 * time.Second,
|
|
Addr: ":" + config.Port,
|
|
Handler: pomme,
|
|
}
|
|
|
|
ts := httptest.NewUnstartedServer(pomme)
|
|
ts.Config = s
|
|
ts.Start()
|
|
|
|
defer ts.Close()
|
|
|
|
tester := Init(ts.URL)
|
|
|
|
c, err := internal.ReadConfig()
|
|
if err != nil {
|
|
assert.NotNil(t, err)
|
|
}
|
|
|
|
db := db.InitDb(c.TestDB)
|
|
|
|
hashedPassword, err := bcrypt.GenerateFromPassword([]byte(tester.password), bcrypt.DefaultCost)
|
|
if err != nil {
|
|
return
|
|
}
|
|
|
|
db.Create(&internal.User{Username: tester.username, HashedPassword: string(hashedPassword)})
|
|
|
|
tester.TestMakeAccount(t)
|
|
tester.TestLogin(t)
|
|
tester.TestLogout(t)
|
|
}
|
|
|
|
func Init(url string) accountTest {
|
|
return accountTest{
|
|
username: autoUname(),
|
|
password: "merde",
|
|
url: url,
|
|
}
|
|
}
|
|
|
|
func (a *accountTest) TestMakeAccount(t *testing.T) {
|
|
var target response
|
|
|
|
client := http.Client{}
|
|
|
|
form := url.Values{}
|
|
|
|
form.Add("username", a.username)
|
|
|
|
form.Add("password", a.password)
|
|
|
|
if req, err := http.NewRequest(http.MethodPost, a.url+`api/create`, strings.NewReader(form.Encode())); err == nil {
|
|
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
|
|
|
|
req.Header.Add("User-Agent", "pomme-api-test-slave")
|
|
|
|
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)
|
|
}
|
|
|
|
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", a.password)
|
|
|
|
if req, err := http.NewRequest(http.MethodPost, a.url+`/api/login`, strings.NewReader(form.Encode())); err == nil {
|
|
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
|
|
req.Header.Add("User-Agent", "pomme-api-test-slave")
|
|
|
|
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)
|
|
}
|
|
|
|
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, a.url+`/api/logout`, strings.NewReader(form.Encode())); err == nil {
|
|
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
|
|
req.Header.Add("User-Agent", "pomme-api-test-slave")
|
|
|
|
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)
|
|
}
|
|
|
|
assert.Equal(t, http.StatusOK, target.Status)
|
|
}
|
|
}
|