Skip to content

Commit

Permalink
[MI-3115] Added logging in the APIs for autocomplete MS Teams teams a…
Browse files Browse the repository at this point in the history
…nd channels (#142)

* [MI-3115] Added logging in the APIs for autocomplete MS Teams teams and channels

* [MI-3115] Fixed failing unit tests
  • Loading branch information
manojmalik20 authored May 24, 2023
1 parent 50ed736 commit b84d5d1
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 3 deletions.
7 changes: 7 additions & 0 deletions server/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,18 +160,21 @@ func (a *API) autocompleteTeams(w http.ResponseWriter, r *http.Request) {

client, err := a.p.GetClientForUser(userID)
if err != nil {
a.p.API.LogError("Unable to get the client for user", "Error", err.Error())
data, _ := json.Marshal(out)
_, _ = w.Write(data)
return
}

teams, err := client.ListTeams()
if err != nil {
a.p.API.LogError("Unable to get the MS Teams teams", "Error", err.Error())
data, _ := json.Marshal(out)
_, _ = w.Write(data)
return
}

a.p.API.LogDebug("Successfully fetched the list of teams", "Count", len(teams))
for _, t := range teams {
s := model.AutocompleteListItem{
Item: t.ID,
Expand All @@ -181,6 +184,7 @@ func (a *API) autocompleteTeams(w http.ResponseWriter, r *http.Request) {

out = append(out, s)
}

data, _ := json.Marshal(out)
_, _ = w.Write(data)
}
Expand All @@ -197,6 +201,7 @@ func (a *API) autocompleteChannels(w http.ResponseWriter, r *http.Request) {

client, err := a.p.GetClientForUser(userID)
if err != nil {
a.p.API.LogError("Unable to get the client for user", "Error", err.Error())
data, _ := json.Marshal(out)
_, _ = w.Write(data)
return
Expand All @@ -205,11 +210,13 @@ func (a *API) autocompleteChannels(w http.ResponseWriter, r *http.Request) {
teamID := args[2]
channels, err := client.ListChannels(teamID)
if err != nil {
a.p.API.LogError("Unable to get the channels for MS Teams team", "TeamID", teamID, "Error", err.Error())
data, _ := json.Marshal(out)
_, _ = w.Write(data)
return
}

a.p.API.LogDebug("Successfully fetched the list of channels for MS Teams team", "TeamID", teamID, "Count", len(channels))
for _, c := range channels {
s := model.AutocompleteListItem{
Item: c.ID,
Expand Down
29 changes: 26 additions & 3 deletions server/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -468,12 +468,16 @@ func TestProcessLifecycle(t *testing.T) {
func TestAutocompleteTeams(t *testing.T) {
for _, test := range []struct {
Name string
SetupAPI func(*plugintest.API)
SetupStore func(*storemocks.Store)
SetupClient func(*clientmocks.Client, *clientmocks.Client)
ExpectedResult []model.AutocompleteListItem
}{
{
Name: "AutocompleteTeams: Unable to get client for the user",
SetupAPI: func(api *plugintest.API) {
api.On("LogError", "Unable to get the client for user", "Error", "not connected user").Once()
},
SetupStore: func(store *storemocks.Store) {
store.On("GetTokenForMattermostUser", testutils.GetID()).Return(nil, nil).Times(1)
},
Expand All @@ -482,6 +486,9 @@ func TestAutocompleteTeams(t *testing.T) {
},
{
Name: "AutocompleteTeams: Unable to get the teams list",
SetupAPI: func(api *plugintest.API) {
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)
},
Expand All @@ -492,6 +499,9 @@ func TestAutocompleteTeams(t *testing.T) {
},
{
Name: "AutocompleteTeams: Valid",
SetupAPI: func(api *plugintest.API) {
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)
},
Expand Down Expand Up @@ -526,6 +536,7 @@ func TestAutocompleteTeams(t *testing.T) {
t.Run(test.Name, func(t *testing.T) {
assert := assert.New(t)
plugin := newTestPlugin(t)
test.SetupAPI(plugin.API.(*plugintest.API))
test.SetupStore(plugin.store.(*storemocks.Store))
test.SetupClient(plugin.msteamsAppClient.(*clientmocks.Client), plugin.clientBuilderWithToken("", "", "", "", nil, nil).(*clientmocks.Client))
w := httptest.NewRecorder()
Expand All @@ -548,18 +559,23 @@ func TestAutocompleteChannels(t *testing.T) {
for _, test := range []struct {
Name string
QueryParams string
SetupAPI func(*plugintest.API)
SetupStore func(*storemocks.Store)
SetupClient func(*clientmocks.Client, *clientmocks.Client)
ExpectedResult []model.AutocompleteListItem
}{
{
Name: "AutocompleteChannels: Query params not present",
SetupAPI: func(a *plugintest.API) {},
SetupStore: func(store *storemocks.Store) {},
SetupClient: func(client *clientmocks.Client, uclient *clientmocks.Client) {},
ExpectedResult: []model.AutocompleteListItem{},
},
{
Name: "AutocompleteChannels: Unable to get client for the user",
Name: "AutocompleteChannels: Unable to get client for the user",
SetupAPI: func(api *plugintest.API) {
api.On("LogError", "Unable to get the client for user", "Error", "not connected user").Once()
},
QueryParams: "mockData-1 mockData-2 mockData-3",
SetupStore: func(store *storemocks.Store) {
store.On("GetTokenForMattermostUser", testutils.GetID()).Return(nil, nil).Times(1)
Expand All @@ -568,7 +584,10 @@ func TestAutocompleteChannels(t *testing.T) {
ExpectedResult: []model.AutocompleteListItem{},
},
{
Name: "AutocompleteChannels: Unable to get the channels list",
Name: "AutocompleteChannels: Unable to get the channels list",
SetupAPI: func(api *plugintest.API) {
api.On("LogError", "Unable to get the channels for MS Teams team", "TeamID", "mockData-3", "Error", "unable to get the channels list").Once()
},
QueryParams: "mockData-1 mockData-2 mockData-3",
SetupStore: func(store *storemocks.Store) {
store.On("GetTokenForMattermostUser", testutils.GetID()).Return(&oauth2.Token{}, nil).Times(1)
Expand All @@ -579,7 +598,10 @@ func TestAutocompleteChannels(t *testing.T) {
ExpectedResult: []model.AutocompleteListItem{},
},
{
Name: "AutocompleteChannels: Valid",
Name: "AutocompleteChannels: Valid",
SetupAPI: func(api *plugintest.API) {
api.On("LogDebug", "Successfully fetched the list of channels for MS Teams team", "TeamID", "mockData-3", "Count", 2).Once()
},
QueryParams: "mockData-1 mockData-2 mockData-3",
SetupStore: func(store *storemocks.Store) {
store.On("GetTokenForMattermostUser", testutils.GetID()).Return(&oauth2.Token{}, nil).Times(1)
Expand Down Expand Up @@ -615,6 +637,7 @@ func TestAutocompleteChannels(t *testing.T) {
t.Run(test.Name, func(t *testing.T) {
assert := assert.New(t)
plugin := newTestPlugin(t)
test.SetupAPI(plugin.API.(*plugintest.API))
test.SetupStore(plugin.store.(*storemocks.Store))
test.SetupClient(plugin.msteamsAppClient.(*clientmocks.Client), plugin.clientBuilderWithToken("", "", "", "", nil, nil).(*clientmocks.Client))
w := httptest.NewRecorder()
Expand Down

0 comments on commit b84d5d1

Please sign in to comment.