Skip to content

Commit

Permalink
Refresh oauth tokens if needed (#442)
Browse files Browse the repository at this point in the history
* Refresh oauth tokens if needed

* Fixing linter errors

* Proper fake token generation

* Addressing PR review comments

* Fixing timer layer

* Fixing CI

* Fixing the behavior

* Fixing CI

* Fixing tests

* Moving the refreshURL to the client struct

* Fixing refresh token usage
  • Loading branch information
jespino authored Dec 26, 2023
1 parent a060362 commit 57fc845
Show file tree
Hide file tree
Showing 10 changed files with 191 additions and 113 deletions.
22 changes: 12 additions & 10 deletions server/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ import (
"golang.org/x/oauth2"
)

var fakeToken = oauth2.Token{Expiry: time.Now().Add(10 * time.Minute)}

func TestSubscriptionValidation(t *testing.T) {
plugin := newTestPlugin(t)

Expand Down Expand Up @@ -109,7 +111,7 @@ func TestSubscriptionNewMesage(t *testing.T) {
},
},
func() {
plugin.store.(*storemocks.Store).On("GetTokenForMattermostUser", "bot-user-id").Return(&oauth2.Token{}, nil).Times(1)
plugin.store.(*storemocks.Store).On("GetTokenForMattermostUser", "bot-user-id").Return(&fakeToken, nil).Times(1)
plugin.metricsService.(*metricsmocks.Metrics).On("ObserveChangeEventTotal", metrics.ActionCreated).Times(1)
},
http.StatusAccepted,
Expand All @@ -128,7 +130,7 @@ func TestSubscriptionNewMesage(t *testing.T) {
},
},
func() {
plugin.store.(*storemocks.Store).On("GetTokenForMattermostUser", "bot-user-id").Return(&oauth2.Token{}, nil).Times(1)
plugin.store.(*storemocks.Store).On("GetTokenForMattermostUser", "bot-user-id").Return(&fakeToken, nil).Times(1)
plugin.metricsService.(*metricsmocks.Metrics).On("ObserveChangeEventTotal", metrics.ActionCreated).Times(1)
},
http.StatusAccepted,
Expand All @@ -147,7 +149,7 @@ func TestSubscriptionNewMesage(t *testing.T) {
},
},
func() {
plugin.store.(*storemocks.Store).On("GetTokenForMattermostUser", "bot-user-id").Return(&oauth2.Token{}, nil).Times(1)
plugin.store.(*storemocks.Store).On("GetTokenForMattermostUser", "bot-user-id").Return(&fakeToken, nil).Times(1)
plugin.metricsService.(*metricsmocks.Metrics).On("ObserveChangeEventTotal", metrics.ActionCreated).Times(1)
},
http.StatusAccepted,
Expand All @@ -166,7 +168,7 @@ func TestSubscriptionNewMesage(t *testing.T) {
},
},
func() {
plugin.store.(*storemocks.Store).On("GetTokenForMattermostUser", "bot-user-id").Return(&oauth2.Token{}, nil).Times(1)
plugin.store.(*storemocks.Store).On("GetTokenForMattermostUser", "bot-user-id").Return(&fakeToken, nil).Times(1)
plugin.API.(*plugintest.API).On("LogError", "Unable to process created activity", "activity", mock.Anything, "error", "Invalid webhook secret").Return(nil).Times(1)
},
http.StatusBadRequest,
Expand All @@ -187,7 +189,7 @@ func TestSubscriptionNewMesage(t *testing.T) {
},
func() {
plugin.configuration.CertificateKey = "test"
plugin.store.(*storemocks.Store).On("GetTokenForMattermostUser", "bot-user-id").Return(&oauth2.Token{}, nil)
plugin.store.(*storemocks.Store).On("GetTokenForMattermostUser", "bot-user-id").Return(&fakeToken, nil)
plugin.API.(*plugintest.API).On("LogError", "Invalid encrypted content", "error", "invalid certificate key").Return(nil)
},
http.StatusBadRequest,
Expand All @@ -207,7 +209,7 @@ func TestSubscriptionNewMesage(t *testing.T) {
},
func() {
plugin.configuration.CertificateKey = "test"
plugin.store.(*storemocks.Store).On("GetTokenForMattermostUser", "bot-user-id").Return(&oauth2.Token{}, nil)
plugin.store.(*storemocks.Store).On("GetTokenForMattermostUser", "bot-user-id").Return(&fakeToken, nil)
},
http.StatusBadRequest,
"Not encrypted content for encrypted subscription\n",
Expand Down Expand Up @@ -588,7 +590,7 @@ func TestAutocompleteTeams(t *testing.T) {
api.On("LogError", "Unable to get the MS Teams teams", "Error", "unable to get the teams list").Once()
},
SetupStore: func(store *storemocks.Store) {
store.On("GetTokenForMattermostUser", testutils.GetID()).Return(&oauth2.Token{}, nil).Times(1)
store.On("GetTokenForMattermostUser", testutils.GetID()).Return(&fakeToken, nil).Times(1)
},
SetupClient: func(client *clientmocks.Client, uclient *clientmocks.Client) {
uclient.On("ListTeams").Return(nil, errors.New("unable to get the teams list")).Times(1)
Expand All @@ -604,7 +606,7 @@ func TestAutocompleteTeams(t *testing.T) {
api.On("LogDebug", "Successfully fetched the list of teams", "Count", 2).Once()
},
SetupStore: func(store *storemocks.Store) {
store.On("GetTokenForMattermostUser", testutils.GetID()).Return(&oauth2.Token{}, nil).Times(1)
store.On("GetTokenForMattermostUser", testutils.GetID()).Return(&fakeToken, nil).Times(1)
},
SetupClient: func(client *clientmocks.Client, uclient *clientmocks.Client) {
uclient.On("ListTeams").Return([]clientmodels.Team{
Expand Down Expand Up @@ -699,7 +701,7 @@ func TestAutocompleteChannels(t *testing.T) {
},
QueryParams: "mockData-1 mockData-2 mockData-3",
SetupStore: func(store *storemocks.Store) {
store.On("GetTokenForMattermostUser", testutils.GetID()).Return(&oauth2.Token{}, nil).Times(1)
store.On("GetTokenForMattermostUser", testutils.GetID()).Return(&fakeToken, nil).Times(1)
},
SetupClient: func(client *clientmocks.Client, uclient *clientmocks.Client) {
uclient.On("ListChannels", "mockData-3").Return(nil, errors.New("unable to get the channels list")).Times(1)
Expand All @@ -716,7 +718,7 @@ func TestAutocompleteChannels(t *testing.T) {
},
QueryParams: "mockData-1 mockData-2 mockData-3",
SetupStore: func(store *storemocks.Store) {
store.On("GetTokenForMattermostUser", testutils.GetID()).Return(&oauth2.Token{}, nil).Times(1)
store.On("GetTokenForMattermostUser", testutils.GetID()).Return(&fakeToken, nil).Times(1)
},
SetupClient: func(client *clientmocks.Client, uclient *clientmocks.Client) {
uclient.On("ListChannels", "mockData-3").Return([]clientmodels.Channel{
Expand Down
14 changes: 7 additions & 7 deletions server/command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -579,7 +579,7 @@ func TestExecuteLinkCommand(t *testing.T) {
s.On("CheckEnabledTeamByTeamID", testutils.GetTeamsUserID()).Return(true).Times(1)
s.On("GetLinkByChannelID", testutils.GetChannelID()).Return(nil, nil).Times(1)
s.On("GetLinkByMSTeamsChannelID", testutils.GetTeamsUserID(), testutils.GetChannelID()).Return(nil, nil).Times(1)
s.On("GetTokenForMattermostUser", testutils.GetUserID()).Return(&oauth2.Token{}, nil).Times(1)
s.On("GetTokenForMattermostUser", testutils.GetUserID()).Return(&fakeToken, nil).Times(1)
s.On("StoreChannelLink", mock.AnythingOfType("*storemodels.ChannelLink")).Return(nil).Times(1)
s.On("BeginTx").Return(&sql.Tx{}, nil).Times(1)
s.On("SaveChannelSubscription", &sql.Tx{}, mock.AnythingOfType("storemodels.ChannelSubscription")).Return(nil).Times(1)
Expand Down Expand Up @@ -619,7 +619,7 @@ func TestExecuteLinkCommand(t *testing.T) {
s.On("CheckEnabledTeamByTeamID", testutils.GetTeamsUserID()).Return(true).Times(1)
s.On("GetLinkByChannelID", testutils.GetChannelID()).Return(nil, nil).Times(1)
s.On("GetLinkByMSTeamsChannelID", testutils.GetTeamsUserID(), testutils.GetChannelID()).Return(nil, nil).Times(1)
s.On("GetTokenForMattermostUser", testutils.GetUserID()).Return(&oauth2.Token{}, nil).Times(1)
s.On("GetTokenForMattermostUser", testutils.GetUserID()).Return(&fakeToken, nil).Times(1)
s.On("StoreChannelLink", mock.AnythingOfType("*storemodels.ChannelLink")).Return(nil).Times(1)
s.On("BeginTx").Return(nil, errors.New("error in beginning the database transaction")).Times(1)
},
Expand Down Expand Up @@ -657,7 +657,7 @@ func TestExecuteLinkCommand(t *testing.T) {
s.On("CheckEnabledTeamByTeamID", testutils.GetTeamsUserID()).Return(true).Times(1)
s.On("GetLinkByChannelID", testutils.GetChannelID()).Return(nil, nil).Times(1)
s.On("GetLinkByMSTeamsChannelID", testutils.GetTeamsUserID(), testutils.GetChannelID()).Return(nil, nil).Times(1)
s.On("GetTokenForMattermostUser", testutils.GetUserID()).Return(&oauth2.Token{}, nil).Times(1)
s.On("GetTokenForMattermostUser", testutils.GetUserID()).Return(&fakeToken, nil).Times(1)
s.On("StoreChannelLink", mock.AnythingOfType("*storemodels.ChannelLink")).Return(nil).Times(1)
s.On("BeginTx").Return(&sql.Tx{}, nil).Times(1)
s.On("SaveChannelSubscription", &sql.Tx{}, mock.AnythingOfType("storemodels.ChannelSubscription")).Return(nil).Times(1)
Expand Down Expand Up @@ -690,7 +690,7 @@ func TestExecuteLinkCommand(t *testing.T) {
s.On("CheckEnabledTeamByTeamID", testutils.GetTeamsUserID()).Return(true).Times(1)
s.On("GetLinkByChannelID", testutils.GetChannelID()).Return(nil, nil).Times(1)
s.On("GetLinkByMSTeamsChannelID", testutils.GetTeamsUserID(), testutils.GetChannelID()).Return(&storemodels.ChannelLink{}, nil).Times(1)
s.On("GetTokenForMattermostUser", testutils.GetUserID()).Return(&oauth2.Token{}, nil).Times(1)
s.On("GetTokenForMattermostUser", testutils.GetUserID()).Return(&fakeToken, nil).Times(1)
s.On("StoreChannelLink", mock.Anything).Return(nil).Times(1)
},
setupClient: func(c *mockClient.Client, uc *mockClient.Client) {
Expand Down Expand Up @@ -814,7 +814,7 @@ func TestExecuteLinkCommand(t *testing.T) {
s.On("CheckEnabledTeamByTeamID", testutils.GetTeamsUserID()).Return(true).Times(1)
s.On("GetLinkByChannelID", testutils.GetChannelID()).Return(nil, nil).Times(1)
s.On("GetLinkByMSTeamsChannelID", testutils.GetTeamsUserID(), "").Return(nil, nil).Times(1)
s.On("GetTokenForMattermostUser", testutils.GetUserID()).Return(&oauth2.Token{}, nil).Times(1)
s.On("GetTokenForMattermostUser", testutils.GetUserID()).Return(&fakeToken, nil).Times(1)
},
setupClient: func(c *mockClient.Client, uc *mockClient.Client) {
uc.On("GetChannelInTeam", testutils.GetTeamsUserID(), "").Return(nil, errors.New("Error while getting the channel"))
Expand Down Expand Up @@ -850,7 +850,7 @@ func TestExecuteConnectCommand(t *testing.T) {
api.On("SendEphemeralPost", testutils.GetUserID(), testutils.GetEphemeralPost(p.userID, testutils.GetChannelID(), "You are already connected to MS Teams. Please disconnect your account first before connecting again.")).Return(testutils.GetPost(testutils.GetChannelID(), testutils.GetUserID(), time.Now().UnixMicro())).Once()
},
setupStore: func(s *mockStore.Store) {
s.On("GetTokenForMattermostUser", testutils.GetUserID()).Return(&oauth2.Token{}, nil).Once()
s.On("GetTokenForMattermostUser", testutils.GetUserID()).Return(&fakeToken, nil).Once()
},
},
{
Expand Down Expand Up @@ -981,7 +981,7 @@ func TestExecuteConnectBotCommand(t *testing.T) {
api.On("SendEphemeralPost", testutils.GetUserID(), testutils.GetEphemeralPost(p.userID, testutils.GetChannelID(), "The bot account is already connected to MS Teams. Please disconnect the bot account first before connecting again.")).Return(testutils.GetPost(testutils.GetChannelID(), testutils.GetUserID(), time.Now().UnixMicro())).Once()
},
setupStore: func(s *mockStore.Store) {
s.On("GetTokenForMattermostUser", p.userID).Return(&oauth2.Token{}, nil).Once()
s.On("GetTokenForMattermostUser", p.userID).Return(&fakeToken, nil).Once()
},
},
{
Expand Down
Loading

0 comments on commit 57fc845

Please sign in to comment.