diff --git a/integration-test/integration_test.go b/integration-test/integration_test.go index 3b61f76..06a6bff 100644 --- a/integration-test/integration_test.go +++ b/integration-test/integration_test.go @@ -18,17 +18,11 @@ import ( petname "github.com/dustinkirkland/golang-petname" ) -const ( - // Attempts connection - host = "app:8080" - 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) @@ -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)) @@ -60,6 +55,8 @@ func healthCheck(attempts int) error { } func TestWebFooterVersion(t *testing.T) { + _, _, basePath := grabTestHost() + Test(t, Description("Footer Version"), Get(basePath+"/"), @@ -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, @@ -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"), @@ -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, @@ -218,6 +218,8 @@ 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"), @@ -275,6 +277,7 @@ func TestHTTPKompanionShelf(t *testing.T) { client, loginSteps := webAuthSteps() Test(t, Description("Login for Device"), loginSteps) + _, _, basePath := grabTestHost() // put book var redirectedPath string @@ -344,7 +347,6 @@ func TestHTTPKompanionShelf(t *testing.T) { // stats func TestWebStats(t *testing.T) { - _, password := grabTestUser() // arrange client, loginSteps := webAuthSteps() Test(t, Description("Login for Device"), loginSteps) @@ -352,7 +354,9 @@ func TestWebStats(t *testing.T) { deviceSteps := setupDeviceSteps(client, deviceName) Test(t, Description("Device Register"), deviceSteps) + _, password := grabTestUser() 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 { @@ -420,6 +424,7 @@ func TestHTTPKompanionOPDS(t *testing.T) { client, loginSteps := webAuthSteps() Test(t, Description("Login for Device"), loginSteps) + _, _, basePath := grabTestHost() // put book var redirectedPath string @@ -461,9 +466,33 @@ 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_TEST_%s", strings.ToUpper(key)) + return os.Getenv(envKey) +} + +func grabTestHost() (host string, healthPath string, basePath string) { + host = readPrefixedEnv("HOST") + + if host == "" { + host = "app:8080" + } + + return host, "http://" + host + "/healthcheck", "http://" + host +} + +func grabTestUser() (user string, password string) { + user = readPrefixedEnv("USER") + password = readPrefixedEnv("PASSWORD") + + if user == "" { + user = "user" + } + if password == "" { + password = "password" + } + + return user, password } func generateDeviceName() string { @@ -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) @@ -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"),