Debian Codegraph @

testing/integration/homeassistant/api_compliance_test.go

raw
1 package homeassistant_test
2
3 import (
4 "net/http"
5 "testing"
6 "testing/synctest"
7
8 "github.com/stretchr/testify/require"
9
10 "github.com/nicholas-fedor/shoutrrr/internal/meta"
11 )
12
13 func TestAPIURLFormatCompliance(t *testing.T) {
14 t.Parallel()
15 synctest.Test(t, func(t *testing.T) {
16 service, mockClient := createTestServiceWithMock(t, validHomeAssistantURL)
17
18 err := service.Send("Test message", nil)
19 require.NoError(t, err)
20
21 req := findMatchingRequest(mockClient, func(req *http.Request) bool {
22 return req.Method == http.MethodPost && req.URL.String() == persistentAPIURL
23 })
24 require.NotNil(t, req)
25
26 mockClient.AssertExpectations(t)
27 })
28 }
29
30 func TestContentTypeAuthorizationAndUserAgent(t *testing.T) {
31 t.Parallel()
32 synctest.Test(t, func(t *testing.T) {
33 service, mockClient := createTestServiceWithMock(t, validHomeAssistantURL)
34
35 err := service.Send("Test message", nil)
36 require.NoError(t, err)
37
38 req := findMatchingRequest(mockClient, func(req *http.Request) bool {
39 return req.Method == http.MethodPost &&
40 req.Header.Get("Content-Type") == "application/json" &&
41 req.Header.Get("Authorization") == "Bearer s3cret" &&
42 req.Header.Get("User-Agent") == meta.UserAgent()
43 })
44 require.NotNil(t, req)
45
46 mockClient.AssertExpectations(t)
47 })
48 }
49
50 func TestNotifyServicePath(t *testing.T) {
51 t.Parallel()
52 synctest.Test(t, func(t *testing.T) {
53 service, mockClient := createTestServiceWithMock(
54 t,
55 validHomeAssistantURL+"?service=mobile_app_phone",
56 )
57
58 err := service.Send("ping", nil)
59 require.NoError(t, err)
60
61 req := findMatchingRequest(mockClient, func(req *http.Request) bool {
62 return req.URL.String() == "https://ha.example.com:443/api/services/notify/mobile_app_phone"
63 })
64 require.NotNil(t, req)
65
66 mockClient.AssertExpectations(t)
67 })
68 }
69
70 func TestDisableTLSImpliedPort(t *testing.T) {
71 t.Parallel()
72 synctest.Test(t, func(t *testing.T) {
73 service, mockClient := createTestServiceWithMock(
74 t,
75 "homeassistant://[email protected]/?disabletls=yes",
76 )
77
78 err := service.Send("lan", nil)
79 require.NoError(t, err)
80
81 req := findMatchingRequest(mockClient, func(req *http.Request) bool {
82 return req.URL.String() ==
83 "http://homeassistant.local:8123/api/services/persistent_notification/create"
84 })
85 require.NotNil(t, req)
86
87 mockClient.AssertExpectations(t)
88 })
89 }
90
91 func TestReverseProxyPrefix(t *testing.T) {
92 t.Parallel()
93 synctest.Test(t, func(t *testing.T) {
94 service, mockClient := createTestServiceWithMock(
95 t,
96 "homeassistant://[email protected]/hass/",
97 )
98
99 err := service.Send("proxied", nil)
100 require.NoError(t, err)
101
102 req := findMatchingRequest(mockClient, func(req *http.Request) bool {
103 return req.URL.String() ==
104 "https://ha.example.com:443/hass/api/services/persistent_notification/create"
105 })
106 require.NotNil(t, req)
107
108 mockClient.AssertExpectations(t)
109 })
110 }