From c8536254e0ae3bfa070d78ef11de52bb622e1f31 Mon Sep 17 00:00:00 2001 From: Vladimir Yakovlev Date: Thu, 20 Oct 2022 14:32:16 +0300 Subject: [PATCH 1/6] Check for valid user token in integration tests --- tests/integration/integration_test.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/tests/integration/integration_test.go b/tests/integration/integration_test.go index 8fc8a854a70dc..a5250aecfa8ca 100644 --- a/tests/integration/integration_test.go +++ b/tests/integration/integration_test.go @@ -266,6 +266,7 @@ func getTokenForLoggedInUser(t testing.TB, session *TestSession) string { tokenCounter++ req := NewRequest(t, "GET", "/user/settings/applications") resp := session.MakeRequest(t, req, http.StatusOK) + assert.EqualValues(t, resp.Result().StatusCode, 200) doc := NewHTMLParser(t, resp.Body) req = NewRequestWithValues(t, "POST", "/user/settings/applications", map[string]string{ "_csrf": doc.GetCSRF(), @@ -274,8 +275,10 @@ func getTokenForLoggedInUser(t testing.TB, session *TestSession) string { session.MakeRequest(t, req, http.StatusSeeOther) req = NewRequest(t, "GET", "/user/settings/applications") resp = session.MakeRequest(t, req, http.StatusOK) + assert.EqualValues(t, resp.Result().StatusCode, 200) htmlDoc := NewHTMLParser(t, resp.Body) token := htmlDoc.doc.Find(".ui.info p").Text() + assert.NotEmpty(t, token) return token } From 8355401b609625761ba52d4351dddecbaa0732d0 Mon Sep 17 00:00:00 2001 From: Vladimir Yakovlev Date: Thu, 20 Oct 2022 15:03:35 +0300 Subject: [PATCH 2/6] Fixup duplicate checks --- tests/integration/integration_test.go | 2 -- 1 file changed, 2 deletions(-) diff --git a/tests/integration/integration_test.go b/tests/integration/integration_test.go index a5250aecfa8ca..57cf82c2272d5 100644 --- a/tests/integration/integration_test.go +++ b/tests/integration/integration_test.go @@ -266,7 +266,6 @@ func getTokenForLoggedInUser(t testing.TB, session *TestSession) string { tokenCounter++ req := NewRequest(t, "GET", "/user/settings/applications") resp := session.MakeRequest(t, req, http.StatusOK) - assert.EqualValues(t, resp.Result().StatusCode, 200) doc := NewHTMLParser(t, resp.Body) req = NewRequestWithValues(t, "POST", "/user/settings/applications", map[string]string{ "_csrf": doc.GetCSRF(), @@ -275,7 +274,6 @@ func getTokenForLoggedInUser(t testing.TB, session *TestSession) string { session.MakeRequest(t, req, http.StatusSeeOther) req = NewRequest(t, "GET", "/user/settings/applications") resp = session.MakeRequest(t, req, http.StatusOK) - assert.EqualValues(t, resp.Result().StatusCode, 200) htmlDoc := NewHTMLParser(t, resp.Body) token := htmlDoc.doc.Find(".ui.info p").Text() assert.NotEmpty(t, token) From 7158b89e768b1ecd1904b0ac5b483547a932644b Mon Sep 17 00:00:00 2001 From: Vladimir Yakovlev Date: Thu, 20 Oct 2022 15:41:08 +0300 Subject: [PATCH 3/6] FIXUP Random user token --- tests/integration/integration_test.go | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/tests/integration/integration_test.go b/tests/integration/integration_test.go index 57cf82c2272d5..543a5795d9ab0 100644 --- a/tests/integration/integration_test.go +++ b/tests/integration/integration_test.go @@ -11,6 +11,7 @@ import ( "hash" "hash/fnv" "io" + "math/rand" "net/http" "net/http/cookiejar" "net/http/httptest" @@ -258,12 +259,9 @@ func loginUserWithPassword(t testing.TB, userName, password string) *TestSession return session } -// token has to be unique this counter take care of -var tokenCounter int64 - func getTokenForLoggedInUser(t testing.TB, session *TestSession) string { t.Helper() - tokenCounter++ + tokenCounter := rand.Int() req := NewRequest(t, "GET", "/user/settings/applications") resp := session.MakeRequest(t, req, http.StatusOK) doc := NewHTMLParser(t, resp.Body) From 213aab71cec58e768e1c3f59388f097ac294881e Mon Sep 17 00:00:00 2001 From: Vladimir Yakovlev Date: Thu, 20 Oct 2022 16:20:05 +0300 Subject: [PATCH 4/6] Revert "FIXUP Random user token" This reverts commit 7158b89e768b1ecd1904b0ac5b483547a932644b. --- tests/integration/integration_test.go | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/tests/integration/integration_test.go b/tests/integration/integration_test.go index 543a5795d9ab0..57cf82c2272d5 100644 --- a/tests/integration/integration_test.go +++ b/tests/integration/integration_test.go @@ -11,7 +11,6 @@ import ( "hash" "hash/fnv" "io" - "math/rand" "net/http" "net/http/cookiejar" "net/http/httptest" @@ -259,9 +258,12 @@ func loginUserWithPassword(t testing.TB, userName, password string) *TestSession return session } +// token has to be unique this counter take care of +var tokenCounter int64 + func getTokenForLoggedInUser(t testing.TB, session *TestSession) string { t.Helper() - tokenCounter := rand.Int() + tokenCounter++ req := NewRequest(t, "GET", "/user/settings/applications") resp := session.MakeRequest(t, req, http.StatusOK) doc := NewHTMLParser(t, resp.Body) From f006e6301da48eebd63b30d8cba2966815871132 Mon Sep 17 00:00:00 2001 From: Vladimir Yakovlev Date: Thu, 20 Oct 2022 16:24:40 +0300 Subject: [PATCH 5/6] Atomic creation of user tokens in integration tests --- tests/integration/integration_test.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/tests/integration/integration_test.go b/tests/integration/integration_test.go index 57cf82c2272d5..dc87bc98d771f 100644 --- a/tests/integration/integration_test.go +++ b/tests/integration/integration_test.go @@ -18,6 +18,7 @@ import ( "os" "path/filepath" "strings" + "sync" "testing" "time" @@ -260,9 +261,12 @@ func loginUserWithPassword(t testing.TB, userName, password string) *TestSession // token has to be unique this counter take care of var tokenCounter int64 +var tokenCounterMutex sync.Mutex func getTokenForLoggedInUser(t testing.TB, session *TestSession) string { t.Helper() + tokenCounterMutex.Lock() + defer tokenCounterMutex.Unlock() tokenCounter++ req := NewRequest(t, "GET", "/user/settings/applications") resp := session.MakeRequest(t, req, http.StatusOK) From 54b3f323c0138666d317daa8a6dbbde305c95362 Mon Sep 17 00:00:00 2001 From: wxiaoguang Date: Thu, 20 Oct 2022 21:45:28 +0800 Subject: [PATCH 6/6] Update integration_test.go --- tests/integration/integration_test.go | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/tests/integration/integration_test.go b/tests/integration/integration_test.go index dc87bc98d771f..416cc126bda74 100644 --- a/tests/integration/integration_test.go +++ b/tests/integration/integration_test.go @@ -18,7 +18,7 @@ import ( "os" "path/filepath" "strings" - "sync" + "sync/atomic" "testing" "time" @@ -261,19 +261,15 @@ func loginUserWithPassword(t testing.TB, userName, password string) *TestSession // token has to be unique this counter take care of var tokenCounter int64 -var tokenCounterMutex sync.Mutex func getTokenForLoggedInUser(t testing.TB, session *TestSession) string { t.Helper() - tokenCounterMutex.Lock() - defer tokenCounterMutex.Unlock() - tokenCounter++ req := NewRequest(t, "GET", "/user/settings/applications") resp := session.MakeRequest(t, req, http.StatusOK) doc := NewHTMLParser(t, resp.Body) req = NewRequestWithValues(t, "POST", "/user/settings/applications", map[string]string{ "_csrf": doc.GetCSRF(), - "name": fmt.Sprintf("api-testing-token-%d", tokenCounter), + "name": fmt.Sprintf("api-testing-token-%d", atomic.AddInt64(&tokenCounter, 1)), }) session.MakeRequest(t, req, http.StatusSeeOther) req = NewRequest(t, "GET", "/user/settings/applications")