-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
af53118
commit 4c05fec
Showing
1 changed file
with
81 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,10 +2,11 @@ package cmd | |
|
||
import ( | ||
"errors" | ||
"github.com/form3tech-oss/go-pagerduty-oncall-report/api" | ||
"testing" | ||
"time" | ||
|
||
"github.com/form3tech-oss/go-pagerduty-oncall-report/api" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
@@ -148,6 +149,85 @@ func Test_pagerDutyClient_getUserTimezone(t *testing.T) { | |
} | ||
} | ||
|
||
func Test_pagerDutyClient_getUserEmailAddress(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
cachedUsers []*api.User | ||
defaultUserTimezone string | ||
mockSetup func(mock *clientMock) | ||
want string | ||
wantErr bool | ||
}{ | ||
{ | ||
name: "Successfully find the user email address", | ||
cachedUsers: []*api.User{ | ||
{ | ||
ID: "USER_ID", | ||
Email: "[email protected]", | ||
}, | ||
}, | ||
want: "[email protected]", | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "User with empty email address will receive empty string", | ||
cachedUsers: []*api.User{ | ||
{ | ||
ID: "USER_ID", | ||
}, | ||
}, | ||
want: "", | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "User not cached will receive empty email address", | ||
cachedUsers: []*api.User{ | ||
{ | ||
ID: "NOT_THE_USER_ID", | ||
}, | ||
}, | ||
want: "", | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "If user not cached it will load users in cache and successfully return email address", | ||
mockSetup: func(mock *clientMock) { | ||
mock.On("ListUsers").Once().Return([]*api.User{ | ||
{ID: "USER_ID", Email: "[email protected]"}, | ||
}, nil) | ||
}, | ||
want: "[email protected]", | ||
wantErr: false, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
mockedClient := &clientMock{} | ||
if tt.mockSetup != nil { | ||
tt.mockSetup(mockedClient) | ||
} | ||
|
||
pd := pagerDutyClient{ | ||
client: mockedClient, | ||
cachedUsers: tt.cachedUsers, | ||
defaultUserTimezone: tt.defaultUserTimezone, | ||
} | ||
|
||
got, err := pd.getUserEmail("USER_ID") | ||
mockedClient.AssertExpectations(t) | ||
|
||
if tt.wantErr == true { | ||
require.Error(t, err) | ||
return | ||
} | ||
|
||
require.NoError(t, err) | ||
|
||
assert.Equal(t, tt.want, got) | ||
}) | ||
} | ||
} | ||
|
||
func Test_pagerDutyClient_convertToUserLocalTimezone(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
|