Skip to content

Commit

Permalink
Review tests to ensure mocked calls are executed
Browse files Browse the repository at this point in the history
  • Loading branch information
streamer45 committed Aug 6, 2024
1 parent dd5f8ba commit 241b7df
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 15 deletions.
2 changes: 2 additions & 0 deletions server/configuration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,8 @@ func TestConfigurationIsValid(t *testing.T) {
func TestGetClientConfig(t *testing.T) {
mockAPI := &pluginMocks.MockAPI{}

defer mockAPI.AssertExpectations(t)

p := &Plugin{
MattermostPlugin: plugin.MattermostPlugin{
API: mockAPI,
Expand Down
11 changes: 3 additions & 8 deletions server/job_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,10 @@ func TestJobServiceStopJob(t *testing.T) {
})

t.Run("sending events", func(t *testing.T) {
defer mockAPI.AssertExpectations(t)
defer mockMetrics.AssertExpectations(t)

mockMetrics.On("IncWebSocketEvent", "out", wsEventJobStop).Once()
mockMetrics.On("IncWebSocketEvent", "out", wsEventCallEnd).Once()

mockAPI.On("PublishWebSocketEvent", wsEventJobStop, map[string]any{
"job_id": "jobID",
Expand All @@ -79,13 +81,6 @@ func TestJobServiceStopJob(t *testing.T) {
ReliableClusterSend: true,
}).Once()

mockAPI.On("PublishWebSocketEvent", wsEventCallEnd, map[string]any{
"channelID": "callChannelID",
}, &model.WebsocketBroadcast{
ConnectionId: "botConnID",
ReliableClusterSend: true,
}).Once()

err := p.jobService.StopJob("callChannelID", "jobID", "botUserID", "botConnID")
require.NoError(t, err)
})
Expand Down
15 changes: 15 additions & 0 deletions server/rtcd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ func TestGetHostForNewCall(t *testing.T) {
mockClientB := &rtcdMocks.MockRTCDClient{}
mockClientC := &rtcdMocks.MockRTCDClient{}

defer mockClientA.AssertExpectations(t)
defer mockClientB.AssertExpectations(t)
defer mockClientC.AssertExpectations(t)
defer mockAPI.AssertExpectations(t)

mockClientA.On("Connected").Return(true).Once()
mockClientB.On("Connected").Return(true).Once()
mockClientC.On("Connected").Return(true).Once()
Expand Down Expand Up @@ -100,6 +105,11 @@ func TestGetHostForNewCall(t *testing.T) {
mockClientB := &rtcdMocks.MockRTCDClient{}
mockClientC := &rtcdMocks.MockRTCDClient{}

defer mockClientA.AssertExpectations(t)
defer mockClientB.AssertExpectations(t)
defer mockClientC.AssertExpectations(t)
defer mockAPI.AssertExpectations(t)

mockClientA.On("Connected").Return(false).Once()
mockClientB.On("Connected").Return(false).Once()
mockClientC.On("Connected").Return(false).Once()
Expand Down Expand Up @@ -155,6 +165,11 @@ func TestGetHostForNewCall(t *testing.T) {
mockClientB := &rtcdMocks.MockRTCDClient{}
mockClientC := &rtcdMocks.MockRTCDClient{}

defer mockClientA.AssertExpectations(t)
defer mockClientB.AssertExpectations(t)
defer mockClientC.AssertExpectations(t)
defer mockAPI.AssertExpectations(t)

m := &rtcdClientManager{
ctx: &Plugin{
MattermostPlugin: plugin.MattermostPlugin{
Expand Down
35 changes: 28 additions & 7 deletions server/websocket_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ func TestHandleBotWSReconnect(t *testing.T) {
mockAPI := &pluginMocks.MockAPI{}
mockMetrics := &serverMocks.MockMetrics{}

defer mockAPI.AssertExpectations(t)
defer mockMetrics.AssertExpectations(t)

p := Plugin{
MattermostPlugin: plugin.MattermostPlugin{
API: mockAPI,
Expand All @@ -52,8 +55,6 @@ func TestHandleBotWSReconnect(t *testing.T) {
mockMetrics.On("ObserveClusterMutexGrabTime", "mutex_call", mock.AnythingOfType("float64"))
mockMetrics.On("ObserveClusterMutexLockedTime", "mutex_call", mock.AnythingOfType("float64"))
mockMetrics.On("ObserveAppHandlersTime", mock.AnythingOfType("string"), mock.AnythingOfType("float64"))
mockMetrics.On("IncStoreOp", "KVGet")
mockMetrics.On("IncStoreOp", "KVSet")

channelID := model.NewId()

Expand Down Expand Up @@ -269,6 +270,8 @@ func TestWSReader(t *testing.T) {
})

t.Run("valid session", func(_ *testing.T) {
defer mockAPI.AssertExpectations(t)

mockAPI.On("GetSession", "authSessionID").Return(&model.Session{
Id: "authSessionID",
ExpiresAt: time.Now().UnixMilli() + 60000,
Expand All @@ -289,6 +292,8 @@ func TestWSReader(t *testing.T) {
})

t.Run("valid session, no expiration", func(_ *testing.T) {
defer mockAPI.AssertExpectations(t)

mockAPI.On("GetSession", "authSessionID").Return(&model.Session{
Id: "authSessionID",
}, nil).Once()
Expand All @@ -308,6 +313,8 @@ func TestWSReader(t *testing.T) {
})

t.Run("expired session", func(_ *testing.T) {
defer mockAPI.AssertExpectations(t)

expiresAt := time.Now().UnixMilli()
us := newUserSession("userID", "channelID", "connID", "callID", false)

Expand Down Expand Up @@ -339,6 +346,8 @@ func TestWSReader(t *testing.T) {
})

t.Run("revoked session", func(_ *testing.T) {
defer mockAPI.AssertExpectations(t)

us := newUserSession("userID", "channelID", "connID", "callID", false)

mockAPI.On("GetSession", "authSessionID").Return(nil,
Expand Down Expand Up @@ -372,6 +381,9 @@ func TestHandleCallStateRequest(t *testing.T) {
mockAPI := &pluginMocks.MockAPI{}
mockMetrics := &serverMocks.MockMetrics{}

defer mockAPI.AssertExpectations(t)
defer mockMetrics.AssertExpectations(t)

p := Plugin{
MattermostPlugin: plugin.MattermostPlugin{
API: mockAPI,
Expand All @@ -391,8 +403,6 @@ func TestHandleCallStateRequest(t *testing.T) {
mockMetrics.On("ObserveClusterMutexGrabTime", "mutex_call", mock.AnythingOfType("float64"))
mockMetrics.On("ObserveClusterMutexLockedTime", "mutex_call", mock.AnythingOfType("float64"))
mockMetrics.On("ObserveAppHandlersTime", mock.AnythingOfType("string"), mock.AnythingOfType("float64"))
mockMetrics.On("IncStoreOp", "KVGet")
mockMetrics.On("IncStoreOp", "KVSet")

channelID := model.NewId()
userID := model.NewId()
Expand Down Expand Up @@ -424,7 +434,6 @@ func TestHandleCallStateRequest(t *testing.T) {
})
require.NoError(t, err)

mockAPI.On("KVDelete", "mutex_call_"+channelID).Return(nil).Once()
mockAPI.On("HasPermissionToChannel", userID, channelID, model.PermissionReadChannel).Return(true).Once()
mockMetrics.On("IncWebSocketEvent", "out", "call_state").Once()
mockAPI.On("PublishWebSocketEvent", "call_state", mock.Anything, mock.Anything).Once()
Expand Down Expand Up @@ -504,6 +513,9 @@ func TestPublishWebSocketEvent(t *testing.T) {
})

t.Run("broadcast", func(_ *testing.T) {
defer mockAPI.AssertExpectations(t)
defer mockMetrics.AssertExpectations(t)

data := map[string]any{}
bc := &WebSocketBroadcast{
ChannelID: callChannelID,
Expand All @@ -528,6 +540,9 @@ func TestPublishWebSocketEvent(t *testing.T) {
})

t.Run("specified users, including bot", func(_ *testing.T) {
defer mockAPI.AssertExpectations(t)
defer mockMetrics.AssertExpectations(t)

data := map[string]any{}
bc := &WebSocketBroadcast{
ChannelID: callChannelID,
Expand Down Expand Up @@ -561,13 +576,16 @@ func TestPublishWebSocketEvent(t *testing.T) {
},
}).Once()

mockMetrics.On("IncWebSocketEvent", "out", wsEventUserReacted).Times(3)
mockMetrics.On("IncWebSocketEvent", "out", wsEventUserReacted).Times(2)

p.publishWebSocketEvent(wsEventUserReacted, data, bc)
})
})

t.Run("connection specific", func(_ *testing.T) {
defer mockAPI.AssertExpectations(t)
defer mockMetrics.AssertExpectations(t)

data := map[string]any{
"session_id": "userSessionID",
}
Expand All @@ -586,6 +604,9 @@ func TestPublishWebSocketEvent(t *testing.T) {
})

t.Run("specified users", func(_ *testing.T) {
defer mockAPI.AssertExpectations(t)
defer mockMetrics.AssertExpectations(t)

data := map[string]any{}
bc := &WebSocketBroadcast{
ChannelID: callChannelID,
Expand All @@ -595,7 +616,7 @@ func TestPublishWebSocketEvent(t *testing.T) {
"userD",
},
}
mockMetrics.On("IncWebSocketEvent", "out", wsEventUserMuted).Twice()
mockMetrics.On("IncWebSocketEvent", "out", wsEventUserMuted).Once()

mockAPI.On("PublishWebSocketEvent", wsEventUserMuted, data, &model.WebsocketBroadcast{
ChannelId: callChannelID,
Expand Down

0 comments on commit 241b7df

Please sign in to comment.