Skip to content

Commit 7537513

Browse files
committed
test(alerting): Add Threema Gateway alert provide tests
1 parent edf2fb4 commit 7537513

File tree

5 files changed

+550
-15
lines changed

5 files changed

+550
-15
lines changed

alerting/provider/threemagateway/recipient.go

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,15 @@ func (r *Recipient) UnmarshalText(text []byte) error {
6868
}
6969

7070
func (r Recipient) MarshalText() ([]byte, error) {
71-
return []byte(r.Value), nil
71+
if r.Type == RecipientTypeInvalid {
72+
return []byte("invalid" + ":" + r.Value), nil
73+
}
74+
for key, val := range validRecipientTypes {
75+
if val == r.Type {
76+
return []byte(key + ":" + r.Value), nil
77+
}
78+
}
79+
return nil, ErrInvalidRecipientType
7280
}
7381

7482
func (r *Recipient) Validate() error {
@@ -81,7 +89,7 @@ func (r *Recipient) Validate() error {
8189
return err
8290
}
8391
case RecipientTypePhone:
84-
strings.TrimPrefix(r.Value, "+")
92+
r.Value = strings.TrimPrefix(r.Value, "+")
8593
if !isValidPhoneNumber(r.Value) {
8694
return ErrInvalidPhoneNumberFormat
8795
}
@@ -95,3 +103,15 @@ func (r *Recipient) Validate() error {
95103
}
96104
return nil
97105
}
106+
107+
func isValidPhoneNumber(number string) bool {
108+
if len(number) == 0 {
109+
return false
110+
}
111+
for _, ch := range number {
112+
if ch < '0' || ch > '9' {
113+
return false
114+
}
115+
}
116+
return true
117+
}
Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
package threemagateway
2+
3+
import (
4+
"errors"
5+
"strings"
6+
"testing"
7+
)
8+
9+
func TestRecipient_UnmarshalText_And_MarshalText(t *testing.T) {
10+
scenarios := []struct {
11+
name string
12+
input string
13+
expected Recipient
14+
expectError error
15+
}{
16+
{
17+
name: "default recipient type",
18+
input: "individual",
19+
expected: Recipient{Type: defaultRecipientType, Value: "individual"},
20+
expectError: nil,
21+
},
22+
{
23+
name: "empty input",
24+
input: "",
25+
expected: Recipient{Type: defaultRecipientType, Value: ""},
26+
expectError: nil,
27+
},
28+
{
29+
name: "invalid format",
30+
input: "type:value:extra",
31+
expectError: ErrInvalidRecipientFormat,
32+
},
33+
{
34+
name: "invalid recipient type",
35+
input: "unknown:value",
36+
expectError: ErrInvalidRecipientType,
37+
},
38+
{
39+
name: "valid phone recipient",
40+
input: "phone:+1234567890",
41+
expected: Recipient{Type: RecipientTypePhone, Value: "+1234567890"},
42+
expectError: nil,
43+
},
44+
{
45+
name: "valid email recipient",
46+
input: "email:[email protected]",
47+
expected: Recipient{Type: RecipientTypeEmail, Value: "[email protected]"},
48+
expectError: nil,
49+
},
50+
}
51+
52+
for _, scenario := range scenarios {
53+
t.Run(scenario.name, func(t *testing.T) {
54+
var recipient Recipient
55+
err := recipient.UnmarshalText([]byte(scenario.input))
56+
57+
if !errors.Is(err, scenario.expectError) {
58+
t.Fatalf("expected error for scenario '%s': %v, got: %v", scenario.name, scenario.expectError, err)
59+
}
60+
61+
if scenario.expectError == nil && recipient != scenario.expected {
62+
t.Fatalf("expected recipient for scenario '%s': %+v, got: %+v", scenario.name, scenario.expected, recipient)
63+
}
64+
65+
if scenario.expectError == nil {
66+
marshaled, err := recipient.MarshalText()
67+
if err != nil {
68+
t.Fatalf("unexpected error during marshaling for scenario '%s': %v", scenario.name, err)
69+
}
70+
expectedMarshaled := scenario.input
71+
if strings.Contains(scenario.input, ":") == false {
72+
expectedMarshaled = "id:" + scenario.input
73+
}
74+
if string(marshaled) != expectedMarshaled {
75+
t.Fatalf("expected marshaled text for scenario '%s': %s, got: %s", scenario.name, expectedMarshaled, string(marshaled))
76+
}
77+
}
78+
})
79+
}
80+
}
81+
82+
func TestRecipient_Validate(t *testing.T) {
83+
scenarios := []struct {
84+
name string
85+
input Recipient
86+
expectError error
87+
}{
88+
{
89+
name: "empty recipient",
90+
input: Recipient{Type: defaultRecipientType, Value: ""},
91+
expectError: ErrInvalidRecipientFormat,
92+
},
93+
{
94+
name: "valid id recipient",
95+
input: Recipient{Type: RecipientTypeId, Value: "ABCDEFGH"},
96+
expectError: nil,
97+
},
98+
{
99+
name: "valid phone recipient",
100+
input: Recipient{Type: RecipientTypePhone, Value: "+1234567890"},
101+
expectError: nil,
102+
},
103+
{
104+
name: "invalid phone recipient",
105+
input: Recipient{Type: RecipientTypePhone, Value: "123-456-7890"},
106+
expectError: ErrInvalidPhoneNumberFormat,
107+
},
108+
{
109+
name: "valid email recipient",
110+
input: Recipient{Type: RecipientTypeEmail, Value: "[email protected]"},
111+
expectError: nil,
112+
},
113+
{
114+
name: "invalid email recipient",
115+
input: Recipient{Type: RecipientTypeEmail, Value: "mailtest.com"},
116+
expectError: ErrInvalidEmailAddressFormat,
117+
},
118+
{
119+
name: "invalid recipient type",
120+
input: Recipient{Type: RecipientTypeInvalid, Value: "value"},
121+
expectError: ErrInvalidRecipientType,
122+
},
123+
}
124+
125+
for _, scenario := range scenarios {
126+
t.Run(scenario.name, func(t *testing.T) {
127+
err := scenario.input.Validate()
128+
129+
if !errors.Is(err, scenario.expectError) {
130+
t.Fatalf("expected error for scenario '%s': %v, got: %v", scenario.name, scenario.expectError, err)
131+
}
132+
})
133+
}
134+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package threemagateway
2+
3+
import (
4+
"errors"
5+
"testing"
6+
)
7+
8+
func TestSendMode_UnmarshalText_And_MarshalText(t *testing.T) {
9+
scenarios := []struct {
10+
name string
11+
input string
12+
expected SendMode
13+
expectError error
14+
}{
15+
{
16+
name: "default mode",
17+
input: "",
18+
expected: SendMode{Value: defaultMode, Type: validModeTypes[defaultMode]},
19+
expectError: nil,
20+
},
21+
{
22+
name: "basic mode",
23+
input: "basic",
24+
expected: SendMode{Value: "basic", Type: ModeTypeBasic},
25+
expectError: nil,
26+
},
27+
{
28+
name: "e2ee mode",
29+
input: "e2ee",
30+
expected: SendMode{Value: "e2ee", Type: ModeTypeE2EE},
31+
expectError: nil,
32+
},
33+
{
34+
name: "invalid mode",
35+
input: "invalid-mode",
36+
expected: SendMode{Value: "invalid-mode", Type: ModeTypeInvalid},
37+
expectError: ErrModeTypeInvalid,
38+
},
39+
}
40+
41+
for _, scenario := range scenarios {
42+
t.Run(scenario.name, func(t *testing.T) {
43+
var mode SendMode
44+
err := mode.UnmarshalText([]byte(scenario.input))
45+
46+
if !errors.Is(err, scenario.expectError) {
47+
t.Fatalf("expected error for scenario '%s': %v, got: %v", scenario.name, scenario.expectError, err)
48+
}
49+
50+
if scenario.expectError == nil && mode != scenario.expected {
51+
t.Fatalf("expected mode for scenario '%s': %+v, got: %+v", scenario.name, scenario.expected, mode)
52+
}
53+
54+
if scenario.expectError == nil {
55+
marshaled, err := mode.MarshalText()
56+
if err != nil {
57+
t.Fatalf("unexpected error during marshaling for scenario '%s': %v", scenario.name, err)
58+
}
59+
expectedMarshaled := scenario.input
60+
if len(scenario.input) == 0 {
61+
expectedMarshaled = defaultMode
62+
}
63+
if string(marshaled) != expectedMarshaled {
64+
t.Fatalf("expected marshaled mode for scenario '%s': '%s', got: '%s'", scenario.name, expectedMarshaled, string(marshaled))
65+
}
66+
}
67+
})
68+
}
69+
}

0 commit comments

Comments
 (0)