| 1 | package teams_test |
| 2 | |
| 3 | import ( |
| 4 | "net/http" |
| 5 | "testing" |
| 6 | "testing/synctest" |
| 7 | |
| 8 | "github.com/stretchr/testify/mock" |
| 9 | "github.com/stretchr/testify/require" |
| 10 | ) |
| 11 | |
| 12 | func TestSendPlainTextMessage(t *testing.T) { |
| 13 | t.Parallel() |
| 14 | synctest.Test(t, func(t *testing.T) { |
| 15 | mockClient := &MockHTTPClient{} |
| 16 | service := createTestService( |
| 17 | t, |
| 18 | integrationBase, |
| 19 | mockClient, |
| 20 | ) |
| 21 | |
| 22 | mockClient.On("Do", mock.Anything). |
| 23 | Return(createMockResponse(http.StatusOK, ""), nil). |
| 24 | Once() |
| 25 | |
| 26 | err := service.Send("Test plain text message", nil) |
| 27 | require.NoError(t, err) |
| 28 | |
| 29 | assertRequestContains(t, mockClient, `"type":"message"`) |
| 30 | assertRequestContains(t, mockClient, `"type":"AdaptiveCard"`) |
| 31 | assertRequestContains(t, mockClient, `"text":"Test plain text message"`) |
| 32 | |
| 33 | mockClient.AssertExpectations(t) |
| 34 | }) |
| 35 | } |
| 36 | |
| 37 | func TestSendEmptyMessage(t *testing.T) { |
| 38 | t.Parallel() |
| 39 | synctest.Test(t, func(t *testing.T) { |
| 40 | mockClient := &MockHTTPClient{} |
| 41 | service := createTestService( |
| 42 | t, |
| 43 | integrationBase, |
| 44 | mockClient, |
| 45 | ) |
| 46 | |
| 47 | mockClient.On("Do", mock.Anything). |
| 48 | Return(createMockResponse(http.StatusOK, ""), nil). |
| 49 | Once() |
| 50 | |
| 51 | err := service.Send("", nil) |
| 52 | require.NoError(t, err) |
| 53 | |
| 54 | mockClient.AssertExpectations(t) |
| 55 | }) |
| 56 | } |
| 57 | |
| 58 | func TestSendMultiLineMessage(t *testing.T) { |
| 59 | t.Parallel() |
| 60 | synctest.Test(t, func(t *testing.T) { |
| 61 | mockClient := &MockHTTPClient{} |
| 62 | service := createTestService( |
| 63 | t, |
| 64 | integrationBase, |
| 65 | mockClient, |
| 66 | ) |
| 67 | |
| 68 | mockClient.On("Do", mock.Anything). |
| 69 | Return(createMockResponse(http.StatusOK, ""), nil). |
| 70 | Once() |
| 71 | |
| 72 | err := service.Send("Line 1\nLine 2\nLine 3", nil) |
| 73 | require.NoError(t, err) |
| 74 | |
| 75 | assertRequestContains(t, mockClient, `"text":"Line 1"`) |
| 76 | assertRequestContains(t, mockClient, `"text":"Line 2"`) |
| 77 | assertRequestContains(t, mockClient, `"text":"Line 3"`) |
| 78 | |
| 79 | mockClient.AssertExpectations(t) |
| 80 | }) |
| 81 | } |
| 82 | |
| 83 | func TestSendMessageWithTitleInCard(t *testing.T) { |
| 84 | t.Parallel() |
| 85 | synctest.Test(t, func(t *testing.T) { |
| 86 | mockClient := &MockHTTPClient{} |
| 87 | service := createTestService( |
| 88 | t, |
| 89 | integrationBase+"&title=MyTitle", |
| 90 | mockClient, |
| 91 | ) |
| 92 | |
| 93 | mockClient.On("Do", mock.Anything). |
| 94 | Return(createMockResponse(http.StatusOK, ""), nil). |
| 95 | Once() |
| 96 | |
| 97 | err := service.Send("Body text", nil) |
| 98 | require.NoError(t, err) |
| 99 | |
| 100 | assertRequestContains(t, mockClient, `"text":"MyTitle"`) |
| 101 | assertRequestContains(t, mockClient, `"weight":"Bolder"`) |
| 102 | assertRequestContains(t, mockClient, `"text":"Body text"`) |
| 103 | |
| 104 | mockClient.AssertExpectations(t) |
| 105 | }) |
| 106 | } |
| 107 | |
| 108 | func TestAdaptiveCardStructure(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("Test", nil) |
| 123 | require.NoError(t, err) |
| 124 | |
| 125 | assertRequestContains(t, mockClient, `"$schema":"http://adaptivecards.io/schemas/adaptive-card.json"`) |
| 126 | assertRequestContains(t, mockClient, `"version":"1.2"`) |
| 127 | assertRequestContains(t, mockClient, `"contentType":"application/vnd.microsoft.card.adaptive"`) |
| 128 | |
| 129 | mockClient.AssertExpectations(t) |
| 130 | }) |
| 131 | } |