Skip to content

Commit

Permalink
Improve robustness of accounts API test
Browse files Browse the repository at this point in the history
  • Loading branch information
anbsky committed Jul 17, 2019
1 parent 07e384b commit 1e4d938
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 8 deletions.
21 changes: 15 additions & 6 deletions api/accounts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
const dummyUserID = 751365
const dummyServerURL = "http://127.0.0.1:59999"
const proxySuffix = "/api/proxy"
const testSetupWait = 200 * time.Millisecond

func TestMain(m *testing.M) {
// call flag.Parse() here if TestMain uses flags
Expand All @@ -47,6 +48,11 @@ func TestMain(m *testing.M) {
}

func testFuncSetup() {
time.Sleep(testSetupWait)
lbrynet.RemoveAccount(dummyUserID)
}

func testFuncTeardown() {
lbrynet.RemoveAccount(dummyUserID)
}

Expand All @@ -69,6 +75,7 @@ func launchDummyAPIServerDelayed(response []byte, delayMsec time.Duration) *http

func TestWithValidAuthToken(t *testing.T) {
testFuncSetup()
defer testFuncTeardown()

var (
q *jsonrpc.RPCRequest
Expand Down Expand Up @@ -122,15 +129,16 @@ func TestWithValidAuthToken(t *testing.T) {
}

func TestWithValidAuthTokenConcurrent(t *testing.T) {
defer lbrynet.RemoveAccount(123)
testFuncSetup()
defer testFuncTeardown()

var wg sync.WaitGroup

ts := launchDummyAPIServerDelayed([]byte(`{
"success": true,
"error": null,
"data": {
"id": 123,
"id": 751365,
"language": "en",
"given_name": null,
"family_name": null,
Expand Down Expand Up @@ -165,12 +173,10 @@ func TestWithValidAuthTokenConcurrent(t *testing.T) {
r.Header.Add("X-Lbry-Auth-Token", "d94ab9865f8416d107935d2ca644509c")
rr := httptest.NewRecorder()

monitor.Logger.Debugf("request %v about to start", w)

http.HandlerFunc(Proxy).ServeHTTP(rr, r)
assert.Equal(t, http.StatusOK, rr.Code)
require.Equal(t, http.StatusOK, rr.Code)
json.Unmarshal(rr.Body.Bytes(), &response)
assert.Nil(t, response.Error)
require.Nil(t, response.Error)
monitor.Logger.Debugf("request %v done", w)
wg.Done()
}(w, &wg)
Expand All @@ -180,6 +186,7 @@ func TestWithValidAuthTokenConcurrent(t *testing.T) {

func TestWithWrongAuthToken(t *testing.T) {
testFuncSetup()
defer testFuncTeardown()

var (
q *jsonrpc.RPCRequest
Expand Down Expand Up @@ -211,9 +218,11 @@ func TestWithWrongAuthToken(t *testing.T) {

func TestWithoutToken(t *testing.T) {
testFuncSetup()
defer testFuncTeardown()

// Create a dummy account so we have a wallet beside the default one
lbrynet.CreateAccount(999)
defer lbrynet.RemoveAccount(999)

var (
q *jsonrpc.RPCRequest
Expand Down
2 changes: 1 addition & 1 deletion api/handlers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
"github.com/ybbus/jsonrpc"
)

func TestProxy_Options(t *testing.T) {
func TestProxyOptions(t *testing.T) {
request, _ := http.NewRequest("OPTIONS", "/api/proxy", nil)
rr := httptest.NewRecorder()
http.HandlerFunc(Proxy).ServeHTTP(rr, request)
Expand Down
2 changes: 1 addition & 1 deletion internal/lbrynet/lbrynet.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func RemoveAccount(UID int) (*ljsonrpc.AccountRemoveResponse, error) {
if err != nil {
return nil, err
}
logger.Log().Infof("removing account %v", UID)
logger.LogF(monitor.F{"uid": UID, "account_id": acc.ID}).Warn("removing account from lbrynet")
r, err := Client.AccountRemove(acc.ID)
if err != nil {
return nil, err
Expand Down

0 comments on commit 1e4d938

Please sign in to comment.