Skip to content

Commit

Permalink
Add SlackWraper mocks, update slack_client_test.go
Browse files Browse the repository at this point in the history
  • Loading branch information
nicholas-wu-hs committed Nov 17, 2017
1 parent e1d059e commit 1b67e1d
Show file tree
Hide file tree
Showing 4 changed files with 361 additions and 66 deletions.
58 changes: 29 additions & 29 deletions server/events/webhooks/mocks/mock_slack_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,18 @@ func (mock *MockSlackClient) AuthTest() error {
return ret0
}

func (mock *MockSlackClient) TokenIsSet() bool {
params := []pegomock.Param{}
result := pegomock.GetGenericMockFrom(mock).Invoke("TokenIsSet", params, []reflect.Type{reflect.TypeOf((*bool)(nil)).Elem()})
var ret0 bool
if len(result) != 0 {
if result[0] != nil {
ret0 = result[0].(bool)
}
}
return ret0
}

func (mock *MockSlackClient) ChannelExists(channelName string) (bool, error) {
params := []pegomock.Param{channelName}
result := pegomock.GetGenericMockFrom(mock).Invoke("ChannelExists", params, []reflect.Type{reflect.TypeOf((*bool)(nil)).Elem(), reflect.TypeOf((*error)(nil)).Elem()})
Expand Down Expand Up @@ -57,18 +69,6 @@ func (mock *MockSlackClient) PostMessage(channel string, applyResult webhooks.Ap
return ret0
}

func (mock *MockSlackClient) TokenIsSet() bool {
params := []pegomock.Param{}
result := pegomock.GetGenericMockFrom(mock).Invoke("TokenIsSet", params, []reflect.Type{reflect.TypeOf((*bool)(nil)).Elem()})
var ret0 bool
if len(result) != 0 {
if result[0] != nil {
ret0 = result[0].(bool)
}
}
return ret0
}

func (mock *MockSlackClient) VerifyWasCalledOnce() *VerifierSlackClient {
return &VerifierSlackClient{mock, pegomock.Times(1), nil}
}
Expand Down Expand Up @@ -104,6 +104,23 @@ func (c *SlackClient_AuthTest_OngoingVerification) GetCapturedArguments() {
func (c *SlackClient_AuthTest_OngoingVerification) GetAllCapturedArguments() {
}

func (verifier *VerifierSlackClient) TokenIsSet() *SlackClient_TokenIsSet_OngoingVerification {
params := []pegomock.Param{}
methodInvocations := pegomock.GetGenericMockFrom(verifier.mock).Verify(verifier.inOrderContext, verifier.invocationCountMatcher, "TokenIsSet", params)
return &SlackClient_TokenIsSet_OngoingVerification{mock: verifier.mock, methodInvocations: methodInvocations}
}

type SlackClient_TokenIsSet_OngoingVerification struct {
mock *MockSlackClient
methodInvocations []pegomock.MethodInvocation
}

func (c *SlackClient_TokenIsSet_OngoingVerification) GetCapturedArguments() {
}

func (c *SlackClient_TokenIsSet_OngoingVerification) GetAllCapturedArguments() {
}

func (verifier *VerifierSlackClient) ChannelExists(channelName string) *SlackClient_ChannelExists_OngoingVerification {
params := []pegomock.Param{channelName}
methodInvocations := pegomock.GetGenericMockFrom(verifier.mock).Verify(verifier.inOrderContext, verifier.invocationCountMatcher, "ChannelExists", params)
Expand Down Expand Up @@ -161,20 +178,3 @@ func (c *SlackClient_PostMessage_OngoingVerification) GetAllCapturedArguments()
}
return
}

func (verifier *VerifierSlackClient) TokenIsSet() *SlackClient_TokenIsSet_OngoingVerification {
params := []pegomock.Param{}
methodInvocations := pegomock.GetGenericMockFrom(verifier.mock).Verify(verifier.inOrderContext, verifier.invocationCountMatcher, "TokenIsSet", params)
return &SlackClient_TokenIsSet_OngoingVerification{mock: verifier.mock, methodInvocations: methodInvocations}
}

type SlackClient_TokenIsSet_OngoingVerification struct {
mock *MockSlackClient
methodInvocations []pegomock.MethodInvocation
}

func (c *SlackClient_TokenIsSet_OngoingVerification) GetCapturedArguments() {
}

func (c *SlackClient_TokenIsSet_OngoingVerification) GetAllCapturedArguments() {
}
167 changes: 167 additions & 0 deletions server/events/webhooks/mocks/mock_slack_wrapper.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
// Automatically generated by pegomock. DO NOT EDIT!
// Source: github.com/hootsuite/atlantis/server/events/webhooks (interfaces: SlackWrapper)

package mocks

import (
slack "github.com/nlopes/slack"
pegomock "github.com/petergtz/pegomock"
"reflect"
)

type MockSlackWrapper struct {
fail func(message string, callerSkip ...int)
}

func NewMockSlackWrapper() *MockSlackWrapper {
return &MockSlackWrapper{fail: pegomock.GlobalFailHandler}
}

func (mock *MockSlackWrapper) AuthTest() (*slack.AuthTestResponse, error) {
params := []pegomock.Param{}
result := pegomock.GetGenericMockFrom(mock).Invoke("AuthTest", params, []reflect.Type{reflect.TypeOf((**slack.AuthTestResponse)(nil)).Elem(), reflect.TypeOf((*error)(nil)).Elem()})
var ret0 *slack.AuthTestResponse
var ret1 error
if len(result) != 0 {
if result[0] != nil {
ret0 = result[0].(*slack.AuthTestResponse)
}
if result[1] != nil {
ret1 = result[1].(error)
}
}
return ret0, ret1
}

func (mock *MockSlackWrapper) GetChannels(excludeArchived bool) ([]slack.Channel, error) {
params := []pegomock.Param{excludeArchived}
result := pegomock.GetGenericMockFrom(mock).Invoke("GetChannels", params, []reflect.Type{reflect.TypeOf((*[]slack.Channel)(nil)).Elem(), reflect.TypeOf((*error)(nil)).Elem()})
var ret0 []slack.Channel
var ret1 error
if len(result) != 0 {
if result[0] != nil {
ret0 = result[0].([]slack.Channel)
}
if result[1] != nil {
ret1 = result[1].(error)
}
}
return ret0, ret1
}

func (mock *MockSlackWrapper) PostMessage(channel string, text string, parameters slack.PostMessageParameters) (string, string, error) {
params := []pegomock.Param{channel, text, parameters}
result := pegomock.GetGenericMockFrom(mock).Invoke("PostMessage", params, []reflect.Type{reflect.TypeOf((*string)(nil)).Elem(), reflect.TypeOf((*string)(nil)).Elem(), reflect.TypeOf((*error)(nil)).Elem()})
var ret0 string
var ret1 string
var ret2 error
if len(result) != 0 {
if result[0] != nil {
ret0 = result[0].(string)
}
if result[1] != nil {
ret1 = result[1].(string)
}
if result[2] != nil {
ret2 = result[2].(error)
}
}
return ret0, ret1, ret2
}

func (mock *MockSlackWrapper) VerifyWasCalledOnce() *VerifierSlackWrapper {
return &VerifierSlackWrapper{mock, pegomock.Times(1), nil}
}

func (mock *MockSlackWrapper) VerifyWasCalled(invocationCountMatcher pegomock.Matcher) *VerifierSlackWrapper {
return &VerifierSlackWrapper{mock, invocationCountMatcher, nil}
}

func (mock *MockSlackWrapper) VerifyWasCalledInOrder(invocationCountMatcher pegomock.Matcher, inOrderContext *pegomock.InOrderContext) *VerifierSlackWrapper {
return &VerifierSlackWrapper{mock, invocationCountMatcher, inOrderContext}
}

type VerifierSlackWrapper struct {
mock *MockSlackWrapper
invocationCountMatcher pegomock.Matcher
inOrderContext *pegomock.InOrderContext
}

func (verifier *VerifierSlackWrapper) AuthTest() *SlackWrapper_AuthTest_OngoingVerification {
params := []pegomock.Param{}
methodInvocations := pegomock.GetGenericMockFrom(verifier.mock).Verify(verifier.inOrderContext, verifier.invocationCountMatcher, "AuthTest", params)
return &SlackWrapper_AuthTest_OngoingVerification{mock: verifier.mock, methodInvocations: methodInvocations}
}

type SlackWrapper_AuthTest_OngoingVerification struct {
mock *MockSlackWrapper
methodInvocations []pegomock.MethodInvocation
}

func (c *SlackWrapper_AuthTest_OngoingVerification) GetCapturedArguments() {
}

func (c *SlackWrapper_AuthTest_OngoingVerification) GetAllCapturedArguments() {
}

func (verifier *VerifierSlackWrapper) GetChannels(excludeArchived bool) *SlackWrapper_GetChannels_OngoingVerification {
params := []pegomock.Param{excludeArchived}
methodInvocations := pegomock.GetGenericMockFrom(verifier.mock).Verify(verifier.inOrderContext, verifier.invocationCountMatcher, "GetChannels", params)
return &SlackWrapper_GetChannels_OngoingVerification{mock: verifier.mock, methodInvocations: methodInvocations}
}

type SlackWrapper_GetChannels_OngoingVerification struct {
mock *MockSlackWrapper
methodInvocations []pegomock.MethodInvocation
}

func (c *SlackWrapper_GetChannels_OngoingVerification) GetCapturedArguments() bool {
excludeArchived := c.GetAllCapturedArguments()
return excludeArchived[len(excludeArchived)-1]
}

func (c *SlackWrapper_GetChannels_OngoingVerification) GetAllCapturedArguments() (_param0 []bool) {
params := pegomock.GetGenericMockFrom(c.mock).GetInvocationParams(c.methodInvocations)
if len(params) > 0 {
_param0 = make([]bool, len(params[0]))
for u, param := range params[0] {
_param0[u] = param.(bool)
}
}
return
}

func (verifier *VerifierSlackWrapper) PostMessage(channel string, text string, parameters slack.PostMessageParameters) *SlackWrapper_PostMessage_OngoingVerification {
params := []pegomock.Param{channel, text, parameters}
methodInvocations := pegomock.GetGenericMockFrom(verifier.mock).Verify(verifier.inOrderContext, verifier.invocationCountMatcher, "PostMessage", params)
return &SlackWrapper_PostMessage_OngoingVerification{mock: verifier.mock, methodInvocations: methodInvocations}
}

type SlackWrapper_PostMessage_OngoingVerification struct {
mock *MockSlackWrapper
methodInvocations []pegomock.MethodInvocation
}

func (c *SlackWrapper_PostMessage_OngoingVerification) GetCapturedArguments() (string, string, slack.PostMessageParameters) {
channel, text, parameters := c.GetAllCapturedArguments()
return channel[len(channel)-1], text[len(text)-1], parameters[len(parameters)-1]
}

func (c *SlackWrapper_PostMessage_OngoingVerification) GetAllCapturedArguments() (_param0 []string, _param1 []string, _param2 []slack.PostMessageParameters) {
params := pegomock.GetGenericMockFrom(c.mock).GetInvocationParams(c.methodInvocations)
if len(params) > 0 {
_param0 = make([]string, len(params[0]))
for u, param := range params[0] {
_param0[u] = param.(string)
}
_param1 = make([]string, len(params[1]))
for u, param := range params[1] {
_param1[u] = param.(string)
}
_param2 = make([]slack.PostMessageParameters, len(params[2]))
for u, param := range params[2] {
_param2[u] = param.(slack.PostMessageParameters)
}
}
return
}
19 changes: 13 additions & 6 deletions server/events/webhooks/slack_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,30 @@ const (
)

//go:generate pegomock generate --use-experimental-model-gen --package mocks -o mocks/mock_slack_client.go SlackClient
//go:generate pegomock generate --use-experimental-model-gen --package mocks -o mocks/mock_slack_wrapper.go SlackWrapper

type SlackClient interface {
AuthTest() error
TokenIsSet() bool
ChannelExists(channelName string) (bool, error)
PostMessage(channel string, applyResult ApplyResult) error
TokenIsSet() bool
}

type SlackWrapper interface {
AuthTest() (response *slack.AuthTestResponse, error error)
GetChannels(excludeArchived bool) ([]slack.Channel, error)
PostMessage(channel, text string, parameters slack.PostMessageParameters) (string, string, error)
}

type DefaultSlackClient struct {
Slack *slack.Client
token string
Slack SlackWrapper
Token string
}

func NewSlackClient(token string) SlackClient {
return &DefaultSlackClient{
Slack: slack.New(token),
token: token,
Token: token,
}
}

Expand All @@ -38,15 +45,15 @@ func (d *DefaultSlackClient) AuthTest() error {
}

func (d *DefaultSlackClient) TokenIsSet() bool {
return d.token != ""
return d.Token != ""
}

func (d *DefaultSlackClient) ChannelExists(channelName string) (bool, error) {
channels, err := d.Slack.GetChannels(true)
if err != nil {
return false, err
}

fmt.Printf("%+v", channels)
for _, channel := range channels {
if channel.Name == channelName {
return true, nil
Expand Down
Loading

0 comments on commit 1b67e1d

Please sign in to comment.