add test stuff

This commit is contained in:
grumbulon 2023-02-28 22:50:20 -05:00 committed by Gitea
parent 19402166da
commit e0d72ef13b
4 changed files with 27 additions and 19 deletions

View file

@ -51,7 +51,7 @@ func API() http.Handler {
render.JSON(w, r, resp)
}),
))
api.Use(setDBMiddleware)
//api.Use(setDBMiddleware)
api.With(setDBMiddleware).Post("/create", NewUser)
api.With(setDBMiddleware).Post("/login", Login)
api.Post("/logout", Logout)

View file

@ -21,6 +21,7 @@ import (
"github.com/go-chi/jwtauth/v5"
"github.com/stretchr/testify/assert"
"golang.org/x/crypto/bcrypt"
"gorm.io/gorm"
)
type response struct {
@ -73,12 +74,7 @@ func TestAPI(t *testing.T) {
tester := Init(ts.URL)
c, err := internal.ReadConfig()
if err != nil {
assert.NotNil(t, err)
}
db, err, ok := db.InitDb(c.TestDB)
db, err, ok := db.InitDb(config.DB)
if err != nil && !ok {
assert.NotNil(t, err)
}
@ -94,11 +90,12 @@ func TestAPI(t *testing.T) {
tester.TestLogin(t)
tester.TestLogout(t)
tester.TestUpload(t)
tester.CleanUp(db)
}
func Init(url string) accountTest {
user := autoUname()
user = user + "-testUser"
token, err := makeTestToken(user)
if err != nil {
return accountTest{}
@ -128,8 +125,6 @@ func (a *accountTest) TestMakeAccount(t *testing.T) {
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)
@ -159,7 +154,6 @@ func (a *accountTest) TestLogin(t *testing.T) {
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 {
@ -188,7 +182,6 @@ func (a *accountTest) TestLogout(t *testing.T) {
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 {
@ -297,7 +290,6 @@ func (a *accountTest) TestUpload(t *testing.T) {
}
req.Header.Add("Authorization", `Bearer:`+a.token)
req.Header.Add("User-Agent", "pomme-api-test-slave")
resp, err := client.Do(req)
if err != nil {
@ -316,3 +308,10 @@ func (a *accountTest) TestUpload(t *testing.T) {
})
}
}
func (a *accountTest) CleanUp(db *gorm.DB) {
var user internal.User
var req internal.ZoneRequest
db.Where("username = ?", a.username).Delete(&user)
db.Where("user = ?", a.username).Delete(&req)
}

View file

@ -29,12 +29,7 @@ func setDBMiddleware(next http.Handler) http.Handler {
logger.writeLogEntry()
}
switch r.Header.Get("User-Agent") {
case "pomme-api-test-slave":
pommeDB, err, ok = db.InitDb(c.TestDB)
default:
pommeDB, err, ok = db.InitDb(c.DB)
}
pommeDB, err, ok = db.InitDb(c.DB)
if err != nil && !ok {
logger := newResponder(Response[any]{

View file

@ -37,6 +37,7 @@ import (
//
// @Router /api/upload [post]
func ReceiveFile(w http.ResponseWriter, r *http.Request) {
var result internal.User
_, claims, _ := jwtauth.FromContext(r.Context())
r.Body = http.MaxBytesReader(w, r.Body, 1*1024*1024) // approx 1 mb max upload
@ -108,6 +109,19 @@ func ReceiveFile(w http.ResponseWriter, r *http.Request) {
return
}
// check if request is coming from user not in the DB but has a valid JWT
db.Where("username = ?", claims["username"].(string)).First(&result)
if result.Username == "" {
logger := newResponder(Response[any]{
Message: "user does not exist",
Status: http.StatusInternalServerError,
})
logger.apiError(w, r)
return
}
db.Create(
&ZoneRequest{
User: claims["username"].(string),