diff options
| author | jwijenbergh <jeroenwijenbergh@protonmail.com> | 2022-11-28 13:28:27 +0100 |
|---|---|---|
| committer | jwijenbergh <jeroenwijenbergh@protonmail.com> | 2022-11-28 13:50:02 +0100 |
| commit | 279c0de75629de5868c3ac1b3272a2850e6b62f7 (patch) | |
| tree | b01b764baca799fe952f01a25f1cf5e05ced8333 /client | |
| parent | 7bab6c76599fdfd34ea9bb064d871ed2be01d4c8 (diff) | |
OAuth: Refactor Token getting and do not save them in the config
This commit refactors getting the tokens into receiver methods. This
means that functions do not have to call the cryptic "EnsureTokens"
method. The receiver getter then already verifier whether or not the
tokens could be obtained (and refreshes too). The downside is that
some things are now private, so testing for invalid tokens needs to be
done somewhere else.
This needs another patch such that clients can save the tokens
themselves using a keyring.
Diffstat (limited to 'client')
| -rw-r--r-- | client/client_test.go | 73 |
1 files changed, 8 insertions, 65 deletions
diff --git a/client/client_test.go b/client/client_test.go index 128319c..9145ef3 100644 --- a/client/client_test.go +++ b/client/client_test.go @@ -249,8 +249,10 @@ func TestTokenExpired(t *testing.T) { serverOAuth := currentServer.OAuth() - accessToken := serverOAuth.Token.Access - refreshToken := serverOAuth.Token.Refresh + accessToken, accessTokenErr := serverOAuth.AccessToken() + if accessTokenErr != nil { + t.Fatalf("Failed to get token: %v", accessTokenErr) + } // Wait for TTL so that the tokens expire time.Sleep(time.Duration(expiredInt) * time.Second) @@ -262,73 +264,14 @@ func TestTokenExpired(t *testing.T) { } // Check if tokens have changed - accessTokenAfter := serverOAuth.Token.Access - refreshTokenAfter := serverOAuth.Token.Refresh + accessTokenAfter, accessTokenAfterErr := serverOAuth.AccessToken() + if accessTokenAfterErr != nil { + t.Fatalf("Failed to get token: %v", accessTokenAfterErr) + } if accessToken == accessTokenAfter { t.Errorf("Access token is the same after refresh") } - - if refreshToken == refreshTokenAfter { - t.Errorf("Refresh token is the same after refresh") - } -} - -func TestTokenInvalid(t *testing.T) { - serverURI := getServerURI(t) - state := &Client{} - - registerErr := state.Register( - "org.letsconnect-vpn.app.linux", - "configsinvalid", - "en", - func(old FSMStateID, new FSMStateID, data interface{}) bool { - stateCallback(t, old, new, data, state) - return true - }, - false, - ) - if registerErr != nil { - t.Fatalf("Register error: %v", registerErr) - } - - _, addErr := state.AddCustomServer(serverURI) - if addErr != nil { - t.Fatalf("Add error: %v", addErr) - } - - _, _, configErr := state.GetConfigCustomServer(serverURI, false) - - if configErr != nil { - t.Fatalf("Connect error before invalid: %v", configErr) - } - - dummyValue := "37" - - currentServer, serverErr := state.Servers.GetCurrentServer() - if serverErr != nil { - t.Fatalf("No server found") - } - - serverOAuth := currentServer.OAuth() - - // Override tokens with invalid values - serverOAuth.Token.Access = dummyValue - serverOAuth.Token.Refresh = dummyValue - - _, _, configErr = state.GetConfigCustomServer(serverURI, false) - - if configErr != nil { - t.Fatalf("Connect error after invalid: %v", configErr) - } - - if serverOAuth.Token.Access == dummyValue { - t.Errorf("Access token is equal to dummy value: %s", dummyValue) - } - - if serverOAuth.Token.Refresh == dummyValue { - t.Errorf("Refresh token is equal to dummy value: %s", dummyValue) - } } // Test if an invalid profile will be corrected. |
