chore(test): update integration test setup to automatically generate ports
This commit was merged in pull request #332.
This commit is contained in:
55
main_test.go
55
main_test.go
@@ -1,37 +1,47 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"me-fit/log"
|
|
||||||
"me-fit/service"
|
|
||||||
"me-fit/types"
|
|
||||||
|
|
||||||
"context"
|
"context"
|
||||||
"database/sql"
|
"database/sql"
|
||||||
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
"strings"
|
"strings"
|
||||||
|
"sync/atomic"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"me-fit/log"
|
||||||
|
"me-fit/service"
|
||||||
|
"me-fit/types"
|
||||||
|
|
||||||
"github.com/google/uuid"
|
"github.com/google/uuid"
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
"golang.org/x/net/html"
|
"golang.org/x/net/html"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestHandleSignIn(t *testing.T) {
|
var (
|
||||||
t.Parallel()
|
httpClient = http.Client{
|
||||||
|
|
||||||
httpClient := http.Client{
|
|
||||||
// Disable redirect following
|
// Disable redirect following
|
||||||
CheckRedirect: func(req *http.Request, via []*http.Request) error {
|
CheckRedirect: func(req *http.Request, via []*http.Request) error {
|
||||||
return http.ErrUseLastResponse
|
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.Run("should signin and return session cookie", func(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
|
|
||||||
db, ctx := setupIntegrationTest(t, "8080")
|
db, basePath, ctx := setupIntegrationTest(t)
|
||||||
|
|
||||||
pass := service.GetHashPassword("password", []byte("salt"))
|
pass := service.GetHashPassword("password", []byte("salt"))
|
||||||
_, err := db.Exec(`
|
_, err := db.Exec(`
|
||||||
@@ -41,7 +51,7 @@ func TestHandleSignIn(t *testing.T) {
|
|||||||
t.Fatalf("Error inserting user: %v", err)
|
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)
|
assert.Nil(t, err)
|
||||||
|
|
||||||
resp, err := httpClient.Do(req)
|
resp, err := httpClient.Do(req)
|
||||||
@@ -61,7 +71,7 @@ func TestHandleSignIn(t *testing.T) {
|
|||||||
"csrf-token": {csrfToken},
|
"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)
|
assert.Nil(t, err)
|
||||||
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
||||||
req.Header.Set("Cookie", anonymousSession.Name+"="+anonymousSession.Value)
|
req.Header.Set("Cookie", anonymousSession.Name+"="+anonymousSession.Value)
|
||||||
@@ -90,38 +100,43 @@ func findCookie(resp *http.Response, name string) *http.Cookie {
|
|||||||
return nil
|
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())
|
ctx, done := context.WithCancel(context.Background())
|
||||||
t.Cleanup(done)
|
t.Cleanup(done)
|
||||||
|
|
||||||
database, err := sql.Open("sqlite3", ":memory:")
|
db, err := sql.Open("sqlite3", ":memory:")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("Could not open Database data.db: %v", err)
|
t.Fatalf("Could not open Database data.db: %v", err)
|
||||||
}
|
}
|
||||||
t.Cleanup(func() {
|
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 {
|
if err != nil {
|
||||||
t.Fatalf("Failed to start server: %v", err)
|
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 {
|
return func(key string) string {
|
||||||
if key == "PORT" {
|
if key == "PORT" {
|
||||||
return port
|
return fmt.Sprint(port)
|
||||||
} else if key == "SMTP_ENABLED" {
|
} else if key == "SMTP_ENABLED" {
|
||||||
return "false"
|
return "false"
|
||||||
} else if key == "PROMETHEUS_ENABLED" {
|
} else if key == "PROMETHEUS_ENABLED" {
|
||||||
return "false"
|
return "false"
|
||||||
} else if key == "BASE_URL" {
|
} else if key == "BASE_URL" {
|
||||||
return "http://localhost:" + port
|
return "http://localhost:" + fmt.Sprint(port)
|
||||||
} else if key == "ENVIRONMENT" {
|
} else if key == "ENVIRONMENT" {
|
||||||
return "test"
|
return "test"
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
Reference in New Issue
Block a user