chore(test): update integration test setup to automatically generate ports
All checks were successful
Build Docker Image / Build-Docker-Image (push) Successful in 43s
Build and Push Docker Image / Build-And-Push-Docker-Image (push) Successful in 52s

This commit was merged in pull request #332.
This commit is contained in:
2024-12-16 17:30:19 +01:00
parent 88892ab6ca
commit 6d3902e572

View File

@@ -1,37 +1,47 @@
package main
import (
"me-fit/log"
"me-fit/service"
"me-fit/types"
"context"
"database/sql"
"fmt"
"net/http"
"net/url"
"strings"
"sync/atomic"
"testing"
"time"
"me-fit/log"
"me-fit/service"
"me-fit/types"
"github.com/google/uuid"
"github.com/stretchr/testify/assert"
"golang.org/x/net/html"
)
func TestHandleSignIn(t *testing.T) {
t.Parallel()
httpClient := http.Client{
var (
httpClient = http.Client{
// Disable redirect following
CheckRedirect: func(req *http.Request, via []*http.Request) error {
return http.ErrUseLastResponse
},
}
port atomic.Int32
)
func TestSecurity(t *testing.T) {
t.Parallel()
}
func TestAuth(t *testing.T) {
t.Parallel()
t.Run("should signin and return session cookie", func(t *testing.T) {
t.Parallel()
db, ctx := setupIntegrationTest(t, "8080")
db, basePath, ctx := setupIntegrationTest(t)
pass := service.GetHashPassword("password", []byte("salt"))
_, err := db.Exec(`
@@ -41,7 +51,7 @@ func TestHandleSignIn(t *testing.T) {
t.Fatalf("Error inserting user: %v", err)
}
req, err := http.NewRequestWithContext(ctx, "GET", "http://localhost:8080/auth/signin", nil)
req, err := http.NewRequestWithContext(ctx, "GET", basePath+"/auth/signin", nil)
assert.Nil(t, err)
resp, err := httpClient.Do(req)
@@ -61,7 +71,7 @@ func TestHandleSignIn(t *testing.T) {
"csrf-token": {csrfToken},
}
req, err = http.NewRequestWithContext(ctx, "POST", "http://localhost:8080/api/auth/signin", strings.NewReader(formData.Encode()))
req, err = http.NewRequestWithContext(ctx, "POST", basePath+"/api/auth/signin", strings.NewReader(formData.Encode()))
assert.Nil(t, err)
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
req.Header.Set("Cookie", anonymousSession.Name+"="+anonymousSession.Value)
@@ -90,38 +100,43 @@ func findCookie(resp *http.Response, name string) *http.Cookie {
return nil
}
func setupIntegrationTest(t *testing.T, port string) (*sql.DB, context.Context) {
func setupIntegrationTest(t *testing.T) (db *sql.DB, basePath string, ctx context.Context) {
ctx, done := context.WithCancel(context.Background())
t.Cleanup(done)
database, err := sql.Open("sqlite3", ":memory:")
db, err := sql.Open("sqlite3", ":memory:")
if err != nil {
t.Fatalf("Could not open Database data.db: %v", err)
}
t.Cleanup(func() {
database.Close()
db.Close()
})
go run(ctx, database, getEnv(port))
testPort := port.Add(1)
testPort += 1024
err = waitForReady(ctx, 5*time.Second, "http://localhost:8080")
go run(ctx, db, getEnv(testPort))
basePath = "http://localhost:" + fmt.Sprint(testPort)
err = waitForReady(ctx, 5*time.Second, basePath)
if err != nil {
t.Fatalf("Failed to start server: %v", err)
}
return database, ctx
return db, basePath, ctx
}
func getEnv(port string) func(string) string {
func getEnv(port int32) func(string) string {
return func(key string) string {
if key == "PORT" {
return port
return fmt.Sprint(port)
} else if key == "SMTP_ENABLED" {
return "false"
} else if key == "PROMETHEUS_ENABLED" {
return "false"
} else if key == "BASE_URL" {
return "http://localhost:" + port
return "http://localhost:" + fmt.Sprint(port)
} else if key == "ENVIRONMENT" {
return "test"
} else {