File size: 3,047 Bytes
83a59ca
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package entities

import (
	"testing"

	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"
)

func stringPointer(value string) *string {
	return &value
}

func TestPhoneNotificationTransport(t *testing.T) {
	tests := []struct {
		name      string
		token     *string
		transport NotificationTransport
		hasError  bool
	}{
		{name: "firebase token with colon", token: stringPointer("fcm-token:value"), transport: NotificationTransportFCM},
		{name: "opaque token with slash", token: stringPointer("projects/alpha/messages/123"), transport: NotificationTransportFCM},
		{name: "public https url", token: stringPointer("https://adapter.example.com/notify"), transport: NotificationTransportHTTP},
		{name: "missing token", token: nil, hasError: true},
		{name: "empty token", token: stringPointer("  "), hasError: true},
		{name: "http url", token: stringPointer("http://adapter.example.com/notify"), hasError: true},
		{name: "ftp url", token: stringPointer("ftp://adapter.example.com/notify"), hasError: true},
		{name: "scheme-like https token", token: stringPointer("https:adapter.example.com"), hasError: true},
		{name: "scheme-like http token", token: stringPointer("http:foo"), hasError: true},
		{name: "scheme-like ftp token", token: stringPointer("ftp:foo"), hasError: true},
		{name: "missing host", token: stringPointer("https:///notify"), hasError: true},
		{name: "embedded user information", token: stringPointer("https://user:password@adapter.example.com/notify"), transport: NotificationTransportHTTP},
		{name: "malformed url", token: stringPointer("https://[::1"), hasError: true},
	}

	for _, test := range tests {
		t.Run(test.name, func(t *testing.T) {
			phone := &Phone{FcmToken: test.token}

			transport, err := phone.NotificationTransport()

			if test.hasError {
				require.Error(t, err)
				return
			}
			require.NoError(t, err)
			assert.Equal(t, test.transport, transport)
		})
	}
}

func TestPhoneNotificationURL(t *testing.T) {
	phone := &Phone{FcmToken: stringPointer("https://user:password@adapter.example.com/notify?tenant=42")}

	endpoint, err := phone.NotificationURL()

	require.NoError(t, err)
	assert.Equal(t, "https", endpoint.Scheme)
	assert.Equal(t, "user", endpoint.User.Username())
	password, hasPassword := endpoint.User.Password()
	assert.True(t, hasPassword)
	assert.Equal(t, "password", password)
	assert.Equal(t, "adapter.example.com", endpoint.Hostname())
	assert.Equal(t, "/notify", endpoint.Path)
	assert.Equal(t, "tenant=42", endpoint.RawQuery)
}

func TestPhoneNotificationURLRejectsFCMToken(t *testing.T) {
	phone := &Phone{FcmToken: stringPointer("fcm-token:value")}

	_, err := phone.NotificationURL()

	require.Error(t, err)
}

func TestPhoneNotificationTransportDoesNotExposeMalformedToken(t *testing.T) {
	token := "https://[::1/secret?token=customer-secret"
	phone := &Phone{FcmToken: &token}

	_, err := phone.NotificationTransport()

	require.Error(t, err)
	assert.NotContains(t, err.Error(), token)
	assert.NotContains(t, err.Error(), "customer-secret")
}