| 1 | package teams_test |
| 2 | |
| 3 | import ( |
| 4 | "net/http" |
| 5 | "net/url" |
| 6 | "testing" |
| 7 | "testing/synctest" |
| 8 | |
| 9 | "github.com/stretchr/testify/mock" |
| 10 | "github.com/stretchr/testify/require" |
| 11 | |
| 12 | "github.com/nicholas-fedor/shoutrrr/pkg/types" |
| 13 | ) |
| 14 | |
| 15 | var integrationBase = "teams://?host=" + url.QueryEscape(workflowURL) |
| 16 | |
| 17 | func TestServiceInitialization(t *testing.T) { |
| 18 | t.Parallel() |
| 19 | synctest.Test(t, func(t *testing.T) { |
| 20 | mockClient := &MockHTTPClient{} |
| 21 | service := createTestService( |
| 22 | t, |
| 23 | integrationBase, |
| 24 | mockClient, |
| 25 | ) |
| 26 | |
| 27 | require.NotNil(t, service) |
| 28 | require.Equal(t, "teams", service.GetID()) |
| 29 | |
| 30 | mockClient.AssertExpectations(t) |
| 31 | }) |
| 32 | } |
| 33 | |
| 34 | func TestSendWithTitle(t *testing.T) { |
| 35 | t.Parallel() |
| 36 | synctest.Test(t, func(t *testing.T) { |
| 37 | mockClient := &MockHTTPClient{} |
| 38 | service := createTestService( |
| 39 | t, |
| 40 | integrationBase, |
| 41 | mockClient, |
| 42 | ) |
| 43 | |
| 44 | mockClient.On("Do", mock.Anything). |
| 45 | Return(createMockResponse(http.StatusOK, ""), nil). |
| 46 | Once() |
| 47 | |
| 48 | params := &types.Params{"title": "Custom Title"} |
| 49 | err := service.Send("Message with custom title", params) |
| 50 | |
| 51 | require.NoError(t, err) |
| 52 | assertRequestContains(t, mockClient, `"weight":"Bolder"`) |
| 53 | assertRequestContains(t, mockClient, `"text":"Custom Title"`) |
| 54 | |
| 55 | mockClient.AssertExpectations(t) |
| 56 | }) |
| 57 | } |
| 58 | |
| 59 | func TestSendWithColor(t *testing.T) { |
| 60 | t.Parallel() |
| 61 | synctest.Test(t, func(t *testing.T) { |
| 62 | mockClient := &MockHTTPClient{} |
| 63 | service := createTestService( |
| 64 | t, |
| 65 | integrationBase, |
| 66 | mockClient, |
| 67 | ) |
| 68 | |
| 69 | mockClient.On("Do", mock.Anything). |
| 70 | Return(createMockResponse(http.StatusOK, ""), nil). |
| 71 | Once() |
| 72 | |
| 73 | params := &types.Params{"color": "attention", "title": "Important"} |
| 74 | err := service.Send("Message body", params) |
| 75 | |
| 76 | require.NoError(t, err) |
| 77 | assertRequestContains(t, mockClient, `"text":"Important"`) |
| 78 | assertRequestContains(t, mockClient, `"color":"attention"`) |
| 79 | |
| 80 | mockClient.AssertExpectations(t) |
| 81 | }) |
| 82 | } |
| 83 | |
| 84 | func TestSendWithEmptyParameters(t *testing.T) { |
| 85 | t.Parallel() |
| 86 | synctest.Test(t, func(t *testing.T) { |
| 87 | mockClient := &MockHTTPClient{} |
| 88 | service := createTestService( |
| 89 | t, |
| 90 | integrationBase, |
| 91 | mockClient, |
| 92 | ) |
| 93 | |
| 94 | mockClient.On("Do", mock.Anything). |
| 95 | Return(createMockResponse(http.StatusOK, ""), nil). |
| 96 | Once() |
| 97 | |
| 98 | emptyParams := &types.Params{} |
| 99 | err := service.Send("Message with empty params", emptyParams) |
| 100 | |
| 101 | require.NoError(t, err) |
| 102 | assertRequestContains(t, mockClient, `"text":"Message with empty params"`) |
| 103 | |
| 104 | mockClient.AssertExpectations(t) |
| 105 | }) |
| 106 | } |
| 107 | |
| 108 | func TestSendWithNilParameters(t *testing.T) { |
| 109 | t.Parallel() |
| 110 | synctest.Test(t, func(t *testing.T) { |
| 111 | mockClient := &MockHTTPClient{} |
| 112 | service := createTestService( |
| 113 | t, |
| 114 | integrationBase, |
| 115 | mockClient, |
| 116 | ) |
| 117 | |
| 118 | mockClient.On("Do", mock.Anything). |
| 119 | Return(createMockResponse(http.StatusOK, ""), nil). |
| 120 | Once() |
| 121 | |
| 122 | err := service.Send("Message with nil params", nil) |
| 123 | |
| 124 | require.NoError(t, err) |
| 125 | assertRequestContains(t, mockClient, `"text":"Message with nil params"`) |
| 126 | |
| 127 | mockClient.AssertExpectations(t) |
| 128 | }) |
| 129 | } |
| 130 | |
| 131 | func TestSendWithURLParameters(t *testing.T) { |
| 132 | t.Parallel() |
| 133 | synctest.Test(t, func(t *testing.T) { |
| 134 | mockClient := &MockHTTPClient{} |
| 135 | mockClient.On("Do", mock.Anything). |
| 136 | Return(createMockResponse(http.StatusOK, ""), nil). |
| 137 | Once() |
| 138 | |
| 139 | serviceURL := integrationBase + "&title=URLTitle&color=00ff00" |
| 140 | service := createTestService(t, serviceURL, mockClient) |
| 141 | |
| 142 | err := service.Send("Message with URL params", nil) |
| 143 | |
| 144 | require.NoError(t, err) |
| 145 | assertRequestContains(t, mockClient, `"text":"URLTitle"`) |
| 146 | assertRequestContains(t, mockClient, `"weight":"Bolder"`) |
| 147 | |
| 148 | mockClient.AssertExpectations(t) |
| 149 | }) |
| 150 | } |