Skip to content

Commit

Permalink
daily summary test
Browse files Browse the repository at this point in the history
  • Loading branch information
fmartingr committed Jul 27, 2023
1 parent 9951416 commit a92d545
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 57 deletions.
8 changes: 4 additions & 4 deletions server/mscalendar/daily_summary.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ func (m *mscalendar) ProcessAllDailySummary(now time.Time) error {
for _, user := range userIndex {
storeUser, storeErr := m.Store.LoadUser(user.MattermostUserID)
if storeErr != nil {
m.Logger.Warnf("Error loading user %s for daily summary. err=%v", user.MattermostUserID, storeErr)
m.Logger.Warnf("Error loading user %s for daily summary. err=%v", storeUser.MattermostUserID, storeErr)
continue
}
byRemoteID[storeUser.Remote.ID] = storeUser
Expand All @@ -127,7 +127,7 @@ func (m *mscalendar) ProcessAllDailySummary(now time.Time) error {

shouldPost, shouldPostErr := shouldPostDailySummary(dsum, now)
if shouldPostErr != nil {
m.Logger.Warnf("Error posting daily summary for user %s. err=%v", user.MattermostUserID, shouldPostErr)
m.Logger.Warnf("Error posting daily summary for user %s. err=%v", storeUser.MattermostUserID, shouldPostErr)
continue
}
if !shouldPost {
Expand All @@ -149,13 +149,13 @@ func (m *mscalendar) ProcessAllDailySummary(now time.Time) error {

tz, err := m.GetTimezone(u)
if err != nil {
m.Logger.Errorf("Error posting daily summary for user %s. err=%v", user.MattermostUserID, shouldPostErr)
m.Logger.Errorf("Error posting daily summary for user %s. err=%v", storeUser.MattermostUserID, shouldPostErr)
continue
}

events, err := m.getTodayCalendarEvents(u, now, tz)
if err != nil {
m.Logger.Errorf("Error posting daily summary for user %s. err=%v", user.MattermostUserID, shouldPostErr)
m.Logger.Errorf("Error posting daily summary for user %s. err=%v", storeUser.MattermostUserID, shouldPostErr)
continue
}

Expand Down
96 changes: 43 additions & 53 deletions server/mscalendar/daily_summary_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,30 @@ Wednesday February 12, 2020
name: "User receives their daily summary (individual data call)",
err: "",
runAssertions: func(deps *Dependencies, client remote.Client) {
user1 := &store.User{
s := deps.Store.(*mock_store.MockStore)
mockRemote := deps.Remote.(*mock_remote.MockRemote)
mockClient := client.(*mock_remote.MockClient)
papi := deps.PluginAPI.(*mock_plugin_api.MockPluginAPI)

loc, err := time.LoadLocation("MST")
require.Nil(t, err)
hour, minute := 10, 0 // Time is "10:00AM"
moment := makeTime(hour, minute, loc)

s.EXPECT().LoadUserIndex().Return(store.UserIndex{{
MattermostUserID: "user1_mm_id",
RemoteID: "user1_remote_id",
}, {
MattermostUserID: "user2_mm_id",
RemoteID: "user2_remote_id",
}, {
MattermostUserID: "user3_mm_id",
RemoteID: "user3_remote_id",
}}, nil)

mockRemote.EXPECT().MakeSuperuserClient(context.Background()).Return(nil, remote.ErrSuperUserClientNotSupported).Times(1)

s.EXPECT().LoadUser("user1_mm_id").Return(&store.User{
MattermostUserID: "user1_mm_id",
Remote: &remote.User{ID: "user1_remote_id"},
Settings: store.Settings{
Expand All @@ -179,8 +202,9 @@ Wednesday February 12, 2020
LastPostTime: "",
},
},
}
user2 := &store.User{
}, nil).Times(3)

s.EXPECT().LoadUser("user2_mm_id").Return(&store.User{
MattermostUserID: "user2_mm_id",
Remote: &remote.User{ID: "user2_remote_id"},
Settings: store.Settings{
Expand All @@ -191,80 +215,46 @@ Wednesday February 12, 2020
LastPostTime: "",
},
},
}
user3 := &store.User{
}, nil).Times(2)

s.EXPECT().LoadUser("user3_mm_id").Return(&store.User{
MattermostUserID: "user3_mm_id",
Remote: &remote.User{ID: "user3_remote_id"},
Settings: store.Settings{
DailySummary: &store.DailySummaryUserSettings{
Enable: false,
Enable: true,
PostTime: "10:00AM", // should not receive summary
Timezone: "Pacific Standard Time",
LastPostTime: "",
},
},
}

mockRemote := deps.Remote.(*mock_remote.MockRemote)
papi := deps.PluginAPI.(*mock_plugin_api.MockPluginAPI)
r := deps.Remote.(*mock_remote.MockRemote)

s := deps.Store.(*mock_store.MockStore)
s.EXPECT().LoadUserIndex().Return(store.UserIndex{{
MattermostUserID: user1.MattermostUserID,
RemoteID: user1.Remote.ID,
}, {
MattermostUserID: user2.MattermostUserID,
RemoteID: user2.Remote.ID,
}, {
MattermostUserID: user3.MattermostUserID,
RemoteID: user3.Remote.ID,
}}, nil)

mockRemote.EXPECT().MakeSuperuserClient(context.Background()).Return(nil, remote.ErrSuperUserClientNotSupported).Times(1)
mockClient := client.(*mock_remote.MockClient)

s.EXPECT().LoadUser(user1.MattermostUserID).Return(user1, nil).Times(2)
s.EXPECT().LoadUser(user2.MattermostUserID).Return(user2, nil).Times(2)
s.EXPECT().LoadUser(user3.MattermostUserID).Return(user3, nil).Times(2)

papi.EXPECT().GetMattermostUser(user1.MattermostUserID).Times(2)
papi.EXPECT().GetMattermostUser(user2.MattermostUserID).Times(2)
papi.EXPECT().GetMattermostUser(user3.MattermostUserID).Times(2)
}, nil)

r.EXPECT().MakeClient(context.TODO(), user1.OAuth2Token).Return(mockClient)
r.EXPECT().MakeClient(context.TODO(), user2.OAuth2Token).Return(mockClient)
r.EXPECT().MakeClient(context.TODO(), user3.OAuth2Token).Return(mockClient)
papi.EXPECT().GetMattermostUser("user1_mm_id").Times(2)
papi.EXPECT().GetMattermostUser("user2_mm_id").Times(1)

mockClient.EXPECT().GetMailboxSettings(user1.Remote.ID).Return(&remote.MailboxSettings{
TimeZone: user1.Settings.DailySummary.Timezone,
}, nil)
mockClient.EXPECT().GetMailboxSettings(user2.Remote.ID).Return(&remote.MailboxSettings{
TimeZone: user2.Settings.DailySummary.Timezone,
mockClient.EXPECT().GetMailboxSettings("user1_remote_id").Return(&remote.MailboxSettings{
TimeZone: "Eastern Standard Time",
}, nil)
mockClient.EXPECT().GetMailboxSettings(user3.Remote.ID).Return(&remote.MailboxSettings{
TimeZone: user3.Settings.DailySummary.Timezone,
mockClient.EXPECT().GetMailboxSettings("user2_remote_id").Return(&remote.MailboxSettings{
TimeZone: "Pacific Standard Time",
}, nil)

loc, err := time.LoadLocation("MST")
require.Nil(t, err)
hour, minute := 10, 0 // Time is "10:00AM"
moment := makeTime(hour, minute, loc)
mockRemote.EXPECT().MakeClient(context.TODO(), gomock.Any()).Return(mockClient)

mockClient.EXPECT().GetDefaultCalendarView(user1.Remote.ID, gomock.Any(), gomock.Any()).Return([]*remote.Event{}, nil)
mockClient.EXPECT().GetDefaultCalendarView(user2.Remote.ID, gomock.Any(), gomock.Any()).Return([]*remote.Event{
mockClient.EXPECT().GetDefaultCalendarView("user1_remote_id", gomock.Any(), gomock.Any()).Return([]*remote.Event{}, nil)
mockClient.EXPECT().GetDefaultCalendarView("user2_remote_id", gomock.Any(), gomock.Any()).Return([]*remote.Event{
{
Subject: "The subject",
Start: remote.NewDateTime(moment, "Mountain Standard Time"),
End: remote.NewDateTime(moment.Add(2*time.Hour), "Mountain Standard Time"),
},
}, nil)
// mockClient.EXPECT().GetDefaultCalendarView(user3.Remote.ID, gomock.Any(), gomock.Any()).Return([]*remote.Event{}, nil)

mockPoster := deps.Poster.(*mock_bot.MockPoster)
gomock.InOrder(
mockPoster.EXPECT().DM(user1.MattermostUserID, "You have no upcoming events.").Return("postID1", nil).Times(1),
mockPoster.EXPECT().DM(user2.MattermostUserID, `Times are shown in Pacific Standard Time
mockPoster.EXPECT().DM("user1_mm_id", "You have no upcoming events.").Return("postID1", nil).Times(1),
mockPoster.EXPECT().DM("user2_mm_id", `Times are shown in Pacific Standard Time
Wednesday February 12, 2020
| Time | Subject |
Expand Down

0 comments on commit a92d545

Please sign in to comment.