-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathclient_test.go
More file actions
150 lines (120 loc) · 3.91 KB
/
client_test.go
File metadata and controls
150 lines (120 loc) · 3.91 KB
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package cloudmailin
import (
"os"
"strings"
"testing"
"github.com/google/go-cmp/cmp"
)
func TestNewClient_ENV_SMTP_URL(t *testing.T) {
original := os.Getenv("CLOUDMAILIN_SMTP_URL")
defer os.Setenv("CLOUDMAILIN_SMTP_URL", original)
t.Run("Not a URL", func(t *testing.T) {
os.Setenv("CLOUDMAILIN_SMTP_URL", "http://\\example.com/foo")
_, err := NewClient()
if err == nil {
t.Error("Expected error got nil")
}
})
t.Run("Missing params", func(t *testing.T) {
os.Setenv("CLOUDMAILIN_SMTP_URL", "http://example.com/foo")
_, err := NewClient()
expected := "missing client values"
if err == nil || !strings.Contains(err.Error(), expected) {
t.Errorf("Expected error got {%v}", err)
}
})
t.Run("SMTPToken", func(t *testing.T) {
os.Setenv("CLOUDMAILIN_SMTP_URL", "smtp://user:pass@localhost/path")
client, _ := NewClient()
if client.SMTPToken != "pass" {
t.Error("Expected client token", client)
}
})
t.Run("SMTPAccount", func(t *testing.T) {
os.Setenv("CLOUDMAILIN_SMTP_URL", "smtp://user:pass@localhost/path")
client, _ := NewClient()
if client.SMTPAccountID != "user" {
t.Error("Expected client account", client)
}
})
}
func TestNewClient_ENV_BASE_URL(t *testing.T) {
original := os.Getenv("CLOUDMAILIN_API_BASE_URL")
defer os.Setenv("CLOUDMAILIN_API_BASE_URL", original)
t.Run("Default (no ENV)", func(t *testing.T) {
expected := "https://api.cloudmailin.com/api/v0.1"
os.Setenv("CLOUDMAILIN_API_BASE_URL", "")
client, _ := NewClient()
if client.BaseURL != expected {
t.Errorf("Expected vs Got BaseURL {%s}",
cmp.Diff(expected, client.BaseURL))
}
})
t.Run("When set in ENV", func(t *testing.T) {
expected := "http://localhost:3000/api/v0.1"
os.Setenv("CLOUDMAILIN_API_BASE_URL", expected)
client, _ := NewClient()
if client.BaseURL != expected {
t.Errorf("Expected vs Got BaseURL {%s}",
cmp.Diff(expected, client.BaseURL))
}
})
}
func TestNewClientFromURL(t *testing.T) {
original := os.Getenv("CLOUDMAILIN_API_BASE_URL")
defer os.Setenv("CLOUDMAILIN_API_BASE_URL", original)
t.Run("Not a URL", func(t *testing.T) {
_, err := NewClientFromURL("http://\\example.com/foo")
if err == nil {
t.Error("Expected error got nil")
}
})
t.Run("Empty URL", func(t *testing.T) {
_, err := NewClientFromURL("")
expected := "missing client values"
if err == nil || !strings.Contains(err.Error(), expected) {
t.Errorf("Expected error got {%v}", err)
}
})
t.Run("Missing credentials", func(t *testing.T) {
_, err := NewClientFromURL("http://example.com/foo")
expected := "missing client values"
if err == nil || !strings.Contains(err.Error(), expected) {
t.Errorf("Expected error got {%v}", err)
}
})
t.Run("Valid URL with credentials", func(t *testing.T) {
client, err := NewClientFromURL("smtp://user:pass@localhost/path")
if err != nil {
t.Errorf("Unexpected error: %v", err)
}
if client.SMTPToken != "pass" {
t.Errorf("Expected SMTPToken to be 'pass', got %q", client.SMTPToken)
}
if client.SMTPAccountID != "user" {
t.Errorf("Expected SMTPAccountID to be 'user', got %q", client.SMTPAccountID)
}
})
t.Run("Uses default BaseURL when env not set", func(t *testing.T) {
os.Setenv("CLOUDMAILIN_API_BASE_URL", "")
client, err := NewClientFromURL("smtp://user:pass@localhost/path")
if err != nil {
t.Errorf("Unexpected error: %v", err)
}
expected := "https://api.cloudmailin.com/api/v0.1"
if client.BaseURL != expected {
t.Errorf("Expected BaseURL to be %q, got %q", expected, client.BaseURL)
}
})
t.Run("Uses BaseURL from env when set", func(t *testing.T) {
expected := "http://localhost:3000/api/v0.1"
os.Setenv("CLOUDMAILIN_API_BASE_URL", expected)
client, err := NewClientFromURL("smtp://user:pass@localhost/path")
if err != nil {
t.Errorf("Unexpected error: %v", err)
}
if client.BaseURL != expected {
t.Errorf("Expected BaseURL to be %q, got %q", expected, client.BaseURL)
}
})
}