Skip to content

Commit

Permalink
Merge branch 'master' into MM-844
Browse files Browse the repository at this point in the history
  • Loading branch information
raghavaggarwal2308 authored Dec 9, 2024
2 parents 96d0b6f + 907c59f commit 2ce6630
Show file tree
Hide file tree
Showing 38 changed files with 1,875 additions and 68 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ apply:
## Install go tools
install-go-tools:
@echo Installing go tools
$(GO) install github.com/golangci/golangci-lint/cmd/golangci-lint@v1.51.1
$(GO) install github.com/golangci/golangci-lint/cmd/golangci-lint@v1.55.0
$(GO) install gotest.tools/[email protected]

## Runs golangci-lint and eslint.
Expand Down
2 changes: 1 addition & 1 deletion calendar/api/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ func (api *api) createEvent(w http.ResponseWriter, r *http.Request) {
}
}

client := api.Remote.MakeClient(context.Background(), user.OAuth2Token)
client := api.Remote.MakeUserClient(context.Background(), user.OAuth2Token, mattermostUserID, api.Poster, api.Store)

mailbox, errMailbox := client.GetMailboxSettings(user.Remote.ID)
if errMailbox != nil {
Expand Down
496 changes: 496 additions & 0 deletions calendar/api/events_test.go

Large diffs are not rendered by default.

14 changes: 7 additions & 7 deletions calendar/api/post_action_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ func TestPostActionAccept(t *testing.T) {
name: "Accept event successfully",
setup: func(req *http.Request) {
mockStore.EXPECT().LoadUser(MockUserID).Return(&store.User{Remote: &remote.User{ID: MockRemoteUserID}}, nil).Times(2)
mockRemote.EXPECT().MakeClient(gomock.Any(), gomock.Any()).Return(mockClient)
mockRemote.EXPECT().MakeUserClient(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(mockClient)
mockPluginAPI.EXPECT().GetMattermostUser(MockUserID).Times(2)
mockClient.EXPECT().AcceptEvent(MockRemoteUserID, MockEventID).Return(nil)

Expand Down Expand Up @@ -198,7 +198,7 @@ func TestPostDeclineAccept(t *testing.T) {
name: "Decline event successfully",
setup: func(req *http.Request) {
mockStore.EXPECT().LoadUser(MockUserID).Return(&store.User{Remote: &remote.User{ID: MockRemoteUserID}}, nil).Times(2)
mockRemote.EXPECT().MakeClient(gomock.Any(), gomock.Any()).Return(mockClient)
mockRemote.EXPECT().MakeUserClient(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(mockClient)
mockPluginAPI.EXPECT().GetMattermostUser(MockUserID).Times(2)
mockClient.EXPECT().DeclineEvent(MockRemoteUserID, MockEventID).Return(nil)

Expand Down Expand Up @@ -255,7 +255,7 @@ func TestPostActionTentative(t *testing.T) {
name: "Tentatively accept event successfully",
setup: func(req *http.Request) {
mockStore.EXPECT().LoadUser(MockUserID).Return(&store.User{Remote: &remote.User{ID: MockRemoteUserID}}, nil).Times(2)
mockRemote.EXPECT().MakeClient(gomock.Any(), gomock.Any()).Return(mockClient)
mockRemote.EXPECT().MakeUserClient(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(mockClient)
mockPluginAPI.EXPECT().GetMattermostUser(MockUserID).Times(2)
mockClient.EXPECT().TentativelyAcceptEvent(MockRemoteUserID, MockEventID).Return(nil)

Expand Down Expand Up @@ -312,7 +312,7 @@ func TestPostActionRespond(t *testing.T) {
name: "Error responding to event",
setup: func(req *http.Request) {
mockStore.EXPECT().LoadUser(MockUserID).Return(&store.User{Remote: &remote.User{ID: MockRemoteUserID}}, nil).Times(2)
mockRemote.EXPECT().MakeClient(gomock.Any(), gomock.Any()).Return(mockClient).Times(1)
mockRemote.EXPECT().MakeUserClient(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(mockClient)
mockPluginAPI.EXPECT().GetMattermostUser(MockUserID).Return(&model.User{Id: MockUserID}, nil).Times(2)
mockClient.EXPECT().AcceptEvent(MockRemoteUserID, MockEventID).Return(nil)

Expand All @@ -338,7 +338,7 @@ func TestPostActionRespond(t *testing.T) {
name: "Error updating post",
setup: func(req *http.Request) {
mockStore.EXPECT().LoadUser(MockUserID).Return(&store.User{Remote: &remote.User{ID: MockRemoteUserID}}, nil).Times(2)
mockRemote.EXPECT().MakeClient(gomock.Any(), gomock.Any()).Return(mockClient).Times(1)
mockRemote.EXPECT().MakeUserClient(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(mockClient)
mockPluginAPI.EXPECT().GetMattermostUser(MockUserID).Return(&model.User{Id: MockUserID}, nil).Times(2)
mockPluginAPI.EXPECT().GetPost("").Return(nil, &model.AppError{Message: "error getting post"})
mockClient.EXPECT().AcceptEvent(MockRemoteUserID, MockEventID).Return(nil)
Expand All @@ -365,7 +365,7 @@ func TestPostActionRespond(t *testing.T) {
name: "No attachment found",
setup: func(req *http.Request) {
mockStore.EXPECT().LoadUser(MockUserID).Return(&store.User{Remote: &remote.User{ID: MockRemoteUserID}}, nil).Times(2)
mockRemote.EXPECT().MakeClient(gomock.Any(), gomock.Any()).Return(mockClient).Times(1)
mockRemote.EXPECT().MakeUserClient(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(mockClient)
mockPluginAPI.EXPECT().GetMattermostUser(MockUserID).Return(&model.User{Id: MockUserID}, nil).Times(2)
mockPluginAPI.EXPECT().GetPost("").Return(&model.Post{}, nil)
mockClient.EXPECT().AcceptEvent(MockRemoteUserID, MockEventID).Return(nil)
Expand All @@ -392,7 +392,7 @@ func TestPostActionRespond(t *testing.T) {
name: "Action responded successfully",
setup: func(req *http.Request) {
mockStore.EXPECT().LoadUser(MockUserID).Return(&store.User{Remote: &remote.User{ID: MockRemoteUserID}}, nil).Times(2)
mockRemote.EXPECT().MakeClient(gomock.Any(), gomock.Any()).Return(mockClient).Times(1)
mockRemote.EXPECT().MakeUserClient(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(mockClient)
mockPluginAPI.EXPECT().GetMattermostUser(MockUserID).Return(&model.User{Id: MockUserID}, nil).Times(2)
mockClient.EXPECT().AcceptEvent(MockRemoteUserID, MockEventID).Return(nil)
attachment := model.SlackAttachment{
Expand Down
72 changes: 72 additions & 0 deletions calendar/api/test_utils.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package api

import (
"fmt"
"testing"
"time"

"github.com/golang/mock/gomock"

Expand All @@ -21,6 +23,18 @@ const (
MockOption = "mockOption"
MockEventID = "mockEventID"
MockRemoteUserID = "mockRemoteUserID"
MockChannelID = "mockChannelID"

ValidRequestBodyJSON = `{
"all_day": false,
"attendees": ["user1", "user2"],
"date": "2024-10-17",
"start_time": "10:00AM",
"end_time": "11:00AM",
"description": "Team sync meeting",
"subject": "Team Sync",
"channel_id": "mockChannelID"
}`
)

type MockNotificationProcessor struct {
Expand Down Expand Up @@ -69,3 +83,61 @@ func GetMockSetup(t *testing.T) (*api, *mock_store.MockStore, *mock_bot.MockPost

return api, mockStore, mockPoster, mockRemote, mockPluginAPI, mockLogger, mockLoggerWith, mockClient
}

// revive:disable-next-line:unexported-return
func GetMockCreateEventPayload(allDay bool, attendees []string, date, startTime, endTime, description, subject, location, channelID string) createEventPayload {
return createEventPayload{
AllDay: allDay,
Attendees: attendees,
Date: date,
StartTime: startTime,
EndTime: endTime,
Description: description,
Subject: subject,
Location: location,
ChannelID: channelID,
}
}

func GetCurrentTimeRequestBodyJSON(channelID string) string {
currentTime := time.Now()
date := currentTime.Format("2006-01-02")
startTime := currentTime.Add(time.Hour).Format("15:04")
endTime := currentTime.Add(2 * time.Hour).Format("15:04")

return fmt.Sprintf(`{
"all_day": false,
"attendees": [],
"date": "%s",
"start_time": "%s",
"end_time": "%s",
"description": "Discuss the quarterly results.",
"subject": "Meeting with team",
"location": "Conference Room",
"channel_id": "%s"
}`, date, startTime, endTime, channelID)
}

func GetMockRemoteEvent() *remote.Event {
currentTime := time.Now()
return &remote.Event{
Start: &remote.DateTime{
DateTime: currentTime.Add(time.Hour).Format("2006-01-02T15:04:05Z"),
TimeZone: "UTC",
},
End: &remote.DateTime{
DateTime: currentTime.Add(2 * time.Hour).Format("2006-01-02T15:04:05Z"),
TimeZone: "UTC",
},
Subject: "Meeting with team",
Location: &remote.Location{DisplayName: "Conference Room"},
Conference: &remote.Conference{
URL: "https://example.com/conference",
Application: "Zoom",
},
Attendees: []*remote.Attendee{
{EmailAddress: &remote.EmailAddress{Name: "John Doe", Address: "[email protected]"}},
{EmailAddress: &remote.EmailAddress{Name: "Jane Smith", Address: "[email protected]"}},
},
}
}
17 changes: 17 additions & 0 deletions calendar/command/daily_summary.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package command

import (
"fmt"
"strings"
"time"

"github.com/mattermost/mattermost-plugin-mscalendar/calendar/config"
Expand Down Expand Up @@ -30,12 +31,20 @@ func (c *Command) dailySummary(parameters ...string) (string, bool, error) {
case "view", "today":
postStr, err := c.Engine.GetDaySummaryForUser(time.Now(), c.user())
if err != nil {
if strings.Contains(err.Error(), store.ErrorRefreshTokenNotSet) || strings.Contains(err.Error(), store.ErrorUserInactive) {
return store.ErrorUserInactive, false, nil
}

return err.Error(), false, err
}
return postStr, false, nil
case "tomorrow":
postStr, err := c.Engine.GetDaySummaryForUser(time.Now().Add(time.Hour*24), c.user())
if err != nil {
if strings.Contains(err.Error(), store.ErrorRefreshTokenNotSet) || strings.Contains(err.Error(), store.ErrorUserInactive) {
return store.ErrorUserInactive, false, nil
}

return err.Error(), false, err
}
return postStr, false, nil
Expand All @@ -47,13 +56,21 @@ func (c *Command) dailySummary(parameters ...string) (string, bool, error) {

dsum, err := c.Engine.SetDailySummaryPostTime(c.user(), val)
if err != nil {
if strings.Contains(err.Error(), store.ErrorRefreshTokenNotSet) || strings.Contains(err.Error(), store.ErrorUserInactive) {
return store.ErrorUserInactive, false, nil
}

return err.Error() + "\n" + getDailySummarySetTimeErrorMessage(), false, nil
}

return dailySummaryResponse(dsum), false, nil
case "settings":
dsum, err := c.Engine.GetDailySummarySettingsForUser(c.user())
if err != nil {
if strings.Contains(err.Error(), store.ErrorRefreshTokenNotSet) || strings.Contains(err.Error(), store.ErrorUserInactive) {
return store.ErrorUserInactive, false, nil
}

return err.Error() + "\nYou may need to configure your daily summary using the commands below.\n" + getDailySummaryHelp(), false, nil
}

Expand Down
7 changes: 7 additions & 0 deletions calendar/command/get_calendars.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
package command

import (
"strings"

"github.com/mattermost/mattermost-plugin-mscalendar/calendar/store"
"github.com/mattermost/mattermost-plugin-mscalendar/calendar/utils"
)

func (c *Command) showCalendars(_ ...string) (string, bool, error) {
resp, err := c.Engine.GetCalendars(c.user())
if err != nil {
if strings.Contains(err.Error(), store.ErrorRefreshTokenNotSet) || strings.Contains(err.Error(), store.ErrorUserInactive) {
return store.ErrorUserInactive, false, nil
}

return "", false, err
}
return utils.JSONBlock(resp), false, nil
Expand Down
6 changes: 6 additions & 0 deletions calendar/command/view_calendar.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,20 @@
package command

import (
"strings"
"time"

"github.com/mattermost/mattermost-plugin-mscalendar/calendar/engine/views"
"github.com/mattermost/mattermost-plugin-mscalendar/calendar/store"
)

func (c *Command) viewCalendar(_ ...string) (string, bool, error) {
tz, err := c.Engine.GetTimezone(c.user())
if err != nil {
if strings.Contains(err.Error(), store.ErrorRefreshTokenNotSet) || strings.Contains(err.Error(), store.ErrorUserInactive) {
return store.ErrorUserInactive, false, nil
}

return "Error: No timezone found", false, err
}

Expand Down
8 changes: 4 additions & 4 deletions calendar/engine/availability_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -888,7 +888,7 @@ func TestRetrieveUsersToSyncIndividually(t *testing.T) {

events := []*remote.Event{newTestEvent("1", "", "test")}
papi.EXPECT().GetMattermostUser(testUser.MattermostUserID)
r.EXPECT().MakeClient(gomock.Any(), testUser.OAuth2Token).Return(client)
r.EXPECT().MakeUserClient(context.Background(), testUser.OAuth2Token, gomock.Any(), gomock.Any(), gomock.Any()).Return(client)
c.EXPECT().GetEventsBetweenDates(testUser.Remote.ID, gomock.Any(), gomock.Any()).Return(events, nil)

m := New(e, "").(*mscalendar)
Expand Down Expand Up @@ -934,7 +934,7 @@ func TestRetrieveUsersToSyncIndividually(t *testing.T) {

events := []*remote.Event{newTestEvent("1", "", "test")}
papi.EXPECT().GetMattermostUser(testUser.MattermostUserID)
r.EXPECT().MakeClient(gomock.Any(), testUser.OAuth2Token).Return(client)
r.EXPECT().MakeUserClient(context.Background(), testUser.OAuth2Token, gomock.Any(), gomock.Any(), gomock.Any()).Return(client)
c.EXPECT().GetEventsBetweenDates(testUser.Remote.ID, gomock.Any(), gomock.Any()).Return(events, nil)

m := New(e, "").(*mscalendar)
Expand Down Expand Up @@ -983,8 +983,8 @@ func TestRetrieveUsersToSyncIndividually(t *testing.T) {
eventsUser2 := []*remote.Event{newTestEvent("2", "", "test2")}
papi.EXPECT().GetMattermostUser(testUser.MattermostUserID)
papi.EXPECT().GetMattermostUser(testUser2.MattermostUserID)
r.EXPECT().MakeClient(gomock.Any(), testUser.OAuth2Token).Return(client)
r.EXPECT().MakeClient(gomock.Any(), testUser2.OAuth2Token).Return(client)
r.EXPECT().MakeUserClient(context.Background(), testUser.OAuth2Token, gomock.Any(), gomock.Any(), gomock.Any()).Return(client)
r.EXPECT().MakeUserClient(context.Background(), testUser2.OAuth2Token, gomock.Any(), gomock.Any(), gomock.Any()).Return(client)
c.EXPECT().GetEventsBetweenDates(testUser.Remote.ID, gomock.Any(), gomock.Any()).Return(eventsUser1, nil)
c.EXPECT().GetEventsBetweenDates(testUser2.Remote.ID, gomock.Any(), gomock.Any()).Return(eventsUser2, nil)

Expand Down
Loading

0 comments on commit 2ce6630

Please sign in to comment.