From ed8595fa5b95fc9222c5f1f43dae414805a21856 Mon Sep 17 00:00:00 2001 From: James Houlahan Date: Thu, 9 Apr 2020 13:49:07 +0200 Subject: [PATCH] test: some work on integration tests (fake) --- pkg/pmapi/auth.go | 13 ++++++++----- pkg/pmapi/clientmanager.go | 4 ++-- test/Makefile | 2 +- test/fakeapi/auth.go | 6 ++++++ test/fakeapi/controller.go | 2 ++ test/fakeapi/fakeapi.go | 13 ++++++------- 6 files changed, 25 insertions(+), 15 deletions(-) diff --git a/pkg/pmapi/auth.go b/pkg/pmapi/auth.go index 9eba98d5..3126241a 100644 --- a/pkg/pmapi/auth.go +++ b/pkg/pmapi/auth.go @@ -208,15 +208,12 @@ func (c *client) sendAuth(auth *Auth) { c.log.Debug("Client is sending auth to ClientManager") if auth != nil { - // UID is only provided in the initial /auth, not during /auth/refresh - if auth.UID() != "" { - c.uid = auth.UID() - } + c.uid = auth.UID() c.accessToken = auth.accessToken } go func() { - c.cm.getClientAuthChannel() <- ClientAuth{ + c.cm.GetClientAuthChannel() <- ClientAuth{ UserID: c.userID, Auth: auth, } @@ -459,6 +456,12 @@ func (c *client) AuthRefresh(uidAndRefreshToken string) (auth *Auth, err error) auth = res.getAuth() + // Responses from /auth/refresh are not guaranteed to return the UID if it has not changed. + // But we want to always return it. + if auth.uid == "" { + auth.uid = c.uid + } + c.sendAuth(auth) return auth, err diff --git a/pkg/pmapi/clientmanager.go b/pkg/pmapi/clientmanager.go index 6dd7577b..fa8bd0ed 100644 --- a/pkg/pmapi/clientmanager.go +++ b/pkg/pmapi/clientmanager.go @@ -249,8 +249,8 @@ func (cm *ClientManager) GetAuthUpdateChannel() chan ClientAuth { return cm.bridgeAuths } -// getClientAuthChannel returns a channel on which clients should send auths. -func (cm *ClientManager) getClientAuthChannel() chan ClientAuth { +// GetClientAuthChannel returns a channel on which clients should send auths. +func (cm *ClientManager) GetClientAuthChannel() chan ClientAuth { return cm.clientAuths } diff --git a/test/Makefile b/test/Makefile index f58a9abc..743e0d61 100644 --- a/test/Makefile +++ b/test/Makefile @@ -29,7 +29,7 @@ test-stage: TEST_ENV=live go test -tags=$(TAGS) -- $(FEATURES) test-debug: - TEST_ENV=fake dlv test -- $(FEATURES) + TEST_ENV=fake TEST_ACCOUNTS=accounts/fake.json dlv test -- $(FEATURES) test-live-debug: TEST_ENV=live dlv test -- $(FEATURES) diff --git a/test/fakeapi/auth.go b/test/fakeapi/auth.go index 901fa72e..e9b22d11 100644 --- a/test/fakeapi/auth.go +++ b/test/fakeapi/auth.go @@ -65,9 +65,12 @@ func (api *FakePMAPI) Auth(username, password string, authInfo *pmapi.AuthInfo) auth := &pmapi.Auth{ TwoFA: user.get2FAInfo(), RefreshToken: session.refreshToken, + ExpiresIn: 86400, } + auth.DANGEROUSLYSetUID(session.uid) api.sendAuth(auth) + return auth, nil } @@ -135,7 +138,10 @@ func (api *FakePMAPI) AuthRefresh(token string) (*pmapi.Auth, error) { auth := &pmapi.Auth{ RefreshToken: session.refreshToken, + ExpiresIn: 86400, } + auth.DANGEROUSLYSetUID(session.uid) + api.sendAuth(auth) return auth, nil diff --git a/test/fakeapi/controller.go b/test/fakeapi/controller.go index 99138c1c..f4517f15 100644 --- a/test/fakeapi/controller.go +++ b/test/fakeapi/controller.go @@ -32,6 +32,7 @@ type Controller struct { labelIDGenerator idGenerator messageIDGenerator idGenerator tokenGenerator idGenerator + clientManager *pmapi.ClientManager // State controlled by test. noInternetConnection bool @@ -52,6 +53,7 @@ func NewController(cm *pmapi.ClientManager) *Controller { labelIDGenerator: 100, // We cannot use system label IDs. messageIDGenerator: 0, tokenGenerator: 1000, // No specific reason; 1000 simply feels right. + clientManager: cm, noInternetConnection: false, usersByUsername: map[string]*fakeUser{}, diff --git a/test/fakeapi/fakeapi.go b/test/fakeapi/fakeapi.go index c5caacfd..aa8d54bd 100644 --- a/test/fakeapi/fakeapi.go +++ b/test/fakeapi/fakeapi.go @@ -103,14 +103,13 @@ func (api *FakePMAPI) checkInternetAndRecordCall(method method, path string, req return nil } -// TODO: This should be sent back to the ClientManager properly! func (api *FakePMAPI) sendAuth(auth *pmapi.Auth) { - if auth != nil { - auth.DANGEROUSLYSetUID(api.uid) - } - if api.auths != nil { - api.auths <- auth - } + go func() { + api.controller.clientManager.GetClientAuthChannel() <- pmapi.ClientAuth{ + UserID: api.user.ID, + Auth: auth, + } + }() } func (api *FakePMAPI) setUser(username string) error {