server_twilio_test.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. package server
  2. import (
  3. "github.com/stretchr/testify/require"
  4. "io"
  5. "net/http"
  6. "net/http/httptest"
  7. "sync/atomic"
  8. "testing"
  9. )
  10. func TestServer_Twilio_SMS(t *testing.T) {
  11. var called atomic.Bool
  12. twilioServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  13. body, err := io.ReadAll(r.Body)
  14. require.Nil(t, err)
  15. require.Equal(t, "/2010-04-01/Accounts/AC1234567890/Messages.json", r.URL.Path)
  16. require.Equal(t, "Basic QUMxMjM0NTY3ODkwOkFBRUFBMTIzNDU2Nzg5MA==", r.Header.Get("Authorization"))
  17. require.Equal(t, "Body=test%0A%0A--%0AThis+message+was+sent+by+9.9.9.9+via+ntfy.sh%2Fmytopic&From=%2B1234567890&To=%2B11122233344", string(body))
  18. called.Store(true)
  19. }))
  20. defer twilioServer.Close()
  21. c := newTestConfig(t)
  22. c.BaseURL = "https://ntfy.sh"
  23. c.TwilioBaseURL = twilioServer.URL
  24. c.TwilioAccount = "AC1234567890"
  25. c.TwilioAuthToken = "AAEAA1234567890"
  26. c.TwilioFromNumber = "+1234567890"
  27. s := newTestServer(t, c)
  28. response := request(t, s, "POST", "/mytopic", "test", map[string]string{
  29. "SMS": "+11122233344",
  30. })
  31. require.Equal(t, "test", toMessage(t, response.Body.String()).Message)
  32. waitFor(t, func() bool {
  33. return called.Load()
  34. })
  35. }
  36. func TestServer_Twilio_Call(t *testing.T) {
  37. var called atomic.Bool
  38. twilioServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  39. body, err := io.ReadAll(r.Body)
  40. require.Nil(t, err)
  41. require.Equal(t, "/2010-04-01/Accounts/AC1234567890/Calls.json", r.URL.Path)
  42. require.Equal(t, "Basic QUMxMjM0NTY3ODkwOkFBRUFBMTIzNDU2Nzg5MA==", r.Header.Get("Authorization"))
  43. require.Equal(t, "From=%2B1234567890&To=%2B11122233344&Twiml=%0A%3CResponse%3E%0A%09%3CPause+length%3D%221%22%2F%3E%0A%09%3CSay%3EYou+have+a+message+from+notify+on+topic+mytopic.+Message%3A%3C%2FSay%3E%0A%09%3CPause+length%3D%221%22%2F%3E%0A%09%3CSay%3Ethis+message+has%26%23xA%3Ba+new+line+and+%26lt%3Bbrackets%26gt%3B%21%26%23xA%3Band+%26%2334%3Bquotes+and+other+%26%2339%3Bquotes%3C%2FSay%3E%0A%09%3CPause+length%3D%221%22%2F%3E%0A%09%3CSay%3EEnd+message.%3C%2FSay%3E%0A%09%3CPause+length%3D%221%22%2F%3E%0A%09%3CSay%3EThis+message+was+sent+by+9.9.9.9+via+127.0.0.1%3A12345%2Fmytopic%3C%2FSay%3E%0A%09%3CPause+length%3D%221%22%2F%3E%0A%3C%2FResponse%3E", string(body))
  44. called.Store(true)
  45. }))
  46. defer twilioServer.Close()
  47. c := newTestConfig(t)
  48. c.TwilioBaseURL = twilioServer.URL
  49. c.TwilioAccount = "AC1234567890"
  50. c.TwilioAuthToken = "AAEAA1234567890"
  51. c.TwilioFromNumber = "+1234567890"
  52. s := newTestServer(t, c)
  53. body := `this message has
  54. a new line and <brackets>!
  55. and "quotes and other 'quotes`
  56. response := request(t, s, "POST", "/mytopic", body, map[string]string{
  57. "x-call": "+11122233344",
  58. })
  59. require.Equal(t, "this message has\na new line and <brackets>!\nand \"quotes and other 'quotes", toMessage(t, response.Body.String()).Message)
  60. waitFor(t, func() bool {
  61. return called.Load()
  62. })
  63. }
  64. func TestServer_Twilio_Call_InvalidNumber(t *testing.T) {
  65. c := newTestConfig(t)
  66. c.TwilioBaseURL = "https://127.0.0.1"
  67. c.TwilioAccount = "AC1234567890"
  68. c.TwilioAuthToken = "AAEAA1234567890"
  69. c.TwilioFromNumber = "+1234567890"
  70. s := newTestServer(t, c)
  71. response := request(t, s, "POST", "/mytopic", "test", map[string]string{
  72. "x-call": "+invalid",
  73. })
  74. require.Equal(t, 40031, toHTTPError(t, response.Body.String()).Code)
  75. }
  76. func TestServer_Twilio_SMS_InvalidNumber(t *testing.T) {
  77. c := newTestConfig(t)
  78. c.TwilioBaseURL = "https://127.0.0.1"
  79. c.TwilioAccount = "AC1234567890"
  80. c.TwilioAuthToken = "AAEAA1234567890"
  81. c.TwilioFromNumber = "+1234567890"
  82. s := newTestServer(t, c)
  83. response := request(t, s, "POST", "/mytopic", "test", map[string]string{
  84. "x-sms": "+invalid",
  85. })
  86. require.Equal(t, 40031, toHTTPError(t, response.Body.String()).Code)
  87. }
  88. func TestServer_Twilio_SMS_Unconfigured(t *testing.T) {
  89. s := newTestServer(t, newTestConfig(t))
  90. response := request(t, s, "POST", "/mytopic", "test", map[string]string{
  91. "x-sms": "+1234",
  92. })
  93. require.Equal(t, 40030, toHTTPError(t, response.Body.String()).Code)
  94. }
  95. func TestServer_Twilio_Call_Unconfigured(t *testing.T) {
  96. s := newTestServer(t, newTestConfig(t))
  97. response := request(t, s, "POST", "/mytopic", "test", map[string]string{
  98. "x-call": "+1234",
  99. })
  100. require.Equal(t, 40030, toHTTPError(t, response.Body.String()).Code)
  101. }