Skip to content
Open
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 43 additions & 12 deletions integration-test/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,11 @@ import (
petname "github.com/dustinkirkland/golang-petname"
)

const (
// Attempts connection
host = "app:8080"
Comment thread
bubylou marked this conversation as resolved.
healthPath = "http://" + host + "/healthcheck"
attempts = 20

// HTTP REST
basePath = "http://" + host
)
const attempts = 20

func TestMain(m *testing.M) {
host, _, _ := grabTestHost()

err := healthCheck(attempts)
if err != nil {
log.Fatalf("Integration tests: host %s is not available: %s", host, err)
Expand All @@ -42,6 +36,7 @@ func TestMain(m *testing.M) {

func healthCheck(attempts int) error {
var err error
_, healthPath, _ := grabTestHost()

for attempts > 0 {
err = Do(Get(healthPath), Expect().Status().Equal(http.StatusOK))
Expand All @@ -60,6 +55,8 @@ func healthCheck(attempts int) error {
}

func TestWebFooterVersion(t *testing.T) {
_, _, basePath := grabTestHost()

Test(t,
Description("Footer Version"),
Get(basePath+"/"),
Expand All @@ -72,6 +69,7 @@ func TestWebFooterVersion(t *testing.T) {
// New tests based on controller/web
// login/page
func TestWebAuthUser(t *testing.T) {
_, _, basePath := grabTestHost()
username, password := grabTestUser()

Test(t,
Expand Down Expand Up @@ -116,6 +114,7 @@ func TestWebAuthUser(t *testing.T) {
// devices
func TestWebDevice(t *testing.T) {
client, loginSteps := webAuthSteps()
_, _, basePath := grabTestHost()

Test(t,
Description("Login for Device"),
Expand Down Expand Up @@ -178,6 +177,7 @@ func TestHTTPKoreaderSyncProgress(t *testing.T) {
deviceName := generateDeviceName()
deviceSteps := setupDeviceSteps(client, deviceName)
Test(t, Description("Device Register"), deviceSteps)
_, _, basePath := grabTestHost()

// check auth
Test(t,
Expand Down Expand Up @@ -218,6 +218,7 @@ func TestHTTPKoreaderSyncProgress(t *testing.T) {
// HTTP GET /users/auth
func TestHTTPAuth(t *testing.T) {
username, password := grabTestUser()
_, _, basePath := grabTestHost()
Test(t,
Description("Auth With Incorrect Password"),
Get(basePath+"/users/auth"),
Expand Down Expand Up @@ -275,6 +276,7 @@ func TestHTTPKompanionShelf(t *testing.T) {

client, loginSteps := webAuthSteps()
Test(t, Description("Login for Device"), loginSteps)
_, _, basePath := grabTestHost()

// put book
var redirectedPath string
Expand Down Expand Up @@ -353,6 +355,7 @@ func TestWebStats(t *testing.T) {
Test(t, Description("Device Register"), deviceSteps)

basicAuth := "Basic " + base64.StdEncoding.EncodeToString([]byte(deviceName+":"+password))
_, _, basePath := grabTestHost()

statsContent, err := os.ReadFile("../test/test_data/koreader/koreader_statistics_example.sqlite3")
if err != nil {
Expand Down Expand Up @@ -420,6 +423,7 @@ func TestHTTPKompanionOPDS(t *testing.T) {

client, loginSteps := webAuthSteps()
Test(t, Description("Login for Device"), loginSteps)
_, _, basePath := grabTestHost()

// put book
var redirectedPath string
Expand Down Expand Up @@ -461,9 +465,34 @@ func TestHTTPKompanionOPDS(t *testing.T) {
// seach opds
}

func grabTestUser() (string, string) {
// TODO: read from env
return "user", "password"
func readPrefixedEnv(key string) string {
envKey := fmt.Sprintf("KOMPANION_%s", strings.ToUpper(key))
return os.Getenv(envKey)
}

func grabTestHost() (host string, healthPath string, basePath string) {
host = readPrefixedEnv("TEST_HOST")

if host == "" {
host = "app:8080"
}

healthPath = "http://" + host + "/healthcheck"
basePath = "http://" + host

return host, healthPath, basePath
}

func grabTestUser() (user string, password string) {
user = readPrefixedEnv("TEST_USER")
password = readPrefixedEnv("TEST_PASSWORD")

if user == "" || password == "" {

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it needs to be something like:

if user == "" {
  user = "user"
}
if password == "" {
  password = "password"
}

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

user = "user"
password = "password"
}

return user, password
}

func generateDeviceName() string {
Expand All @@ -477,6 +506,7 @@ func hashSyncPassword(password string) string {

// webAuthSteps returns a client and a step to authenticate
func webAuthSteps() (*http.Client, hit.IStep) {
_, _, basePath := grabTestHost()
username, password := grabTestUser()

jar, err := cookiejar.New(nil)
Expand Down Expand Up @@ -505,6 +535,7 @@ func webAuthSteps() (*http.Client, hit.IStep) {
}

func setupDeviceSteps(client *http.Client, deviceName string) hit.IStep {
_, _, basePath := grabTestHost()
return CombineSteps(
HTTPClient(client),
Description("Device Register"),
Expand Down