Debian Codegraph @

testing/integration/teams/utils_test.go

raw
1 package teams_test
2
3 import (
4 "bytes"
5 "io"
6 "net/http"
7 "net/url"
8 "strings"
9 "testing"
10
11 "github.com/stretchr/testify/mock"
12
13 "github.com/nicholas-fedor/shoutrrr/pkg/services/chat/teams"
14 )
15
16 // MockHTTPClient is a testify mock that implements the teams.HTTPClient interface.
17 type MockHTTPClient struct {
18 mock.Mock
19 }
20
21 // mockLogger is a simple logger implementation for testing.
22 type mockLogger struct{}
23
24 const workflowURL = "https://prod-00.westus.logic.azure.com:443/workflows/abc123/triggers/manual/paths/invoke?api-version=2016-06-00&sp=/triggers/manual/run&sv=1.0&sig=XXXXXXXX"
25
26 func (m *mockLogger) Print(_ ...any) {}
27 func (m *mockLogger) Printf(_ string, _ ...any) {}
28 func (m *mockLogger) Println(_ ...any) {}
29
30 // createTestService creates a Teams service instance configured for testing.
31 func createTestService(
32 t *testing.T,
33 webhookURL string,
34 httpClients ...teams.HTTPClient,
35 ) *teams.Service {
36 t.Helper()
37
38 service := &teams.Service{}
39
40 parsedURL, err := url.Parse(webhookURL)
41 if err != nil {
42 t.Fatalf("failed to parse webhook URL: %v", err)
43 }
44
45 err = service.Initialize(parsedURL, &mockLogger{})
46 if err != nil {
47 t.Fatalf("failed to initialize service: %v", err)
48 }
49
50 if len(httpClients) > 0 && httpClients[0] != nil {
51 service.SetHTTPClient(httpClients[0])
52 }
53
54 return service
55 }
56
57 func (m *MockHTTPClient) Do(req *http.Request) (*http.Response, error) {
58 if req.Body != nil {
59 body, err := io.ReadAll(req.Body)
60 if err == nil {
61 req.Body = io.NopCloser(bytes.NewReader(body))
62 }
63 }
64
65 args := m.Called(req)
66
67 if args.Get(0) == nil {
68 return nil, args.Error(1)
69 }
70
71 return args.Get(0).(*http.Response), args.Error(1)
72 }
73
74 // createMockResponse creates a mock HTTP response with the given status code, body, and optional headers.
75 //
76 //nolint:unparam // body and headers are intentionally exposed for callers to use
77 func createMockResponse(statusCode int, body string, headers ...map[string]string) *http.Response {
78 resp := &http.Response{
79 StatusCode: statusCode,
80 Body: io.NopCloser(strings.NewReader(body)),
81 Header: make(http.Header),
82 }
83
84 for _, hdrMap := range headers {
85 for k, v := range hdrMap {
86 resp.Header.Set(k, v)
87 }
88 }
89
90 return resp
91 }
92
93 // assertRequestContains asserts that the HTTP request body contains the expected content.
94 func assertRequestContains(t *testing.T, mockClient *MockHTTPClient, expectedContent string) {
95 t.Helper()
96
97 found := false
98
99 for i := range mockClient.Calls {
100 call := &mockClient.Calls[i]
101 if call.Method == "Do" {
102 req := call.Arguments[0].(*http.Request)
103
104 body, err := io.ReadAll(req.Body)
105 if err != nil {
106 continue
107 }
108
109 req.Body = io.NopCloser(bytes.NewReader(body))
110
111 if strings.Contains(string(body), expectedContent) {
112 found = true
113
114 break
115 }
116 }
117 }
118
119 if !found {
120 t.Errorf("Expected request body to contain %q, but no matching call found", expectedContent)
121 }
122 }