-
Notifications
You must be signed in to change notification settings - Fork 1
/
mauth_app_test.go
158 lines (142 loc) · 4.93 KB
/
mauth_app_test.go
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
151
152
153
154
155
156
157
158
package go_mauth_client
import (
"fmt"
"io/ioutil"
"log"
"path/filepath"
"testing"
)
func TestLoadMauth(t *testing.T) {
mauth, err := LoadMauth(MAuthOptions{app_id, filepath.Join("test", "private_key.pem"), false})
if err != nil {
t.Error("Error creating the MAuth Struct")
}
if mauth.AppId != app_id {
t.Error("App ID doesn't match")
}
if mauth.RsaPrivateKey.Validate() != nil {
t.Error("Error validating key")
}
}
func TestLoadMauthMissingFile(t *testing.T) {
_, err := LoadMauth(MAuthOptions{app_id, filepath.Join("test", "banana.pem"), false})
if err == nil {
t.Error("Expected Error creating the MAuth Struct")
}
}
func TestLoadMauthNotKey(t *testing.T) {
_, err := LoadMauth(MAuthOptions{app_id, filepath.Join("test", "junk.pem"), false})
if err == nil {
t.Error("Expected Error loading an empty Private Key file")
}
}
func TestLoadMauthInvalidKey(t *testing.T) {
_, err := LoadMauth(MAuthOptions{app_id, filepath.Join("test", "invalid.pem"), false})
if err == nil {
t.Error("Expected Error loading an invalid Private Key")
}
}
func TestLoadMauthFromString(t *testing.T) {
keyContent, _ := ioutil.ReadFile(filepath.Join("test", "private_key.pem"))
mauth, err := LoadMauth(MAuthOptions{app_id, string(keyContent), false})
if err != nil {
t.Error("Error creating the MAuth Struct")
}
if mauth.AppId != app_id {
t.Error("App ID doesn't match")
}
if mauth.RsaPrivateKey.Validate() != nil {
t.Error("Error validating key")
}
}
func TestLoadMauthFromStringNotKey(t *testing.T) {
keyContent, _ := ioutil.ReadFile(filepath.Join("test", "junk.pem"))
_, err := LoadMauth(MAuthOptions{app_id, string(keyContent), false})
if err == nil {
t.Error("Expected Error loading an empty Private Key file")
}
}
func TestLoadMauthFromStringInvalidKey(t *testing.T) {
keyContent, _ := ioutil.ReadFile(filepath.Join("test", "invalid.pem"))
_, err := LoadMauth(MAuthOptions{app_id, string(keyContent), false})
if err == nil {
t.Error("Expected Error loading an invalid Private Key")
}
}
// Example of loading the MAuth configuration from a path
func ExampleLoadMauth() {
// given an APP_UUID
var appUUID = "7D0B2A90-0825-4AD8-9C1F-E9851795D428"
// and a path to a KeyFile
var keyPath = filepath.Join("test", "private_key.pem")
// create a MAuth client
var client *MAuthApp
client, err := LoadMauth(MAuthOptions{appUUID, keyPath, false})
if err != nil {
log.Fatal("Unable to create client: ", err)
}
println("Created MAuth App for APP_UUID ", client.AppId)
}
// Example for creating client where the Private Key is provided as a String
func ExampleLoadMauthFromString() {
// given an APP_UUID
var appUUID = "7D0B2A90-0825-4AD8-9C1F-E9851795D428"
// and the content of the Private Key
var keyString []byte
keyString, _ = ioutil.ReadFile(filepath.Join("test", "private_key.pem"))
// create a MAuth client
var client *MAuthApp
client, err := LoadMauth(MAuthOptions{appUUID, string(keyString), false})
if err != nil {
log.Fatal("Unable to create client: ", err)
}
println("Created MAuth App for APP_UUID ", client.AppId)
}
func TestMAuthApp_makeRequestUserAgent(t *testing.T) {
mauth, err := LoadMauth(MAuthOptions{app_id, filepath.Join("test", "private_key.pem"), false})
if err != nil {
t.Error("Error creating the MAuth Struct")
}
expected := fmt.Sprintf("go-mauth-client/%s", GetVersion())
extraHeaders := make(map[string][]string)
request, err := mauth.makeRequest("GET", "/some/url", "", extraHeaders)
if expected != request.Header.Get("User-Agent") {
t.Error("Expected User-Agent to be", expected, "got",
request.Header.Get("User-Agent"))
}
}
func TestMAuthApp_makeRequestEmpty(t *testing.T) {
mauth, err := LoadMauth(MAuthOptions{app_id, filepath.Join("test", "private_key.pem"), false})
if err != nil {
t.Error("Error creating the MAuth Struct")
}
extraHeaders := make(map[string][]string)
request, err := mauth.makeRequest("GET", "/some/url", "", extraHeaders)
if "" != request.Header.Get("Content-Type") {
t.Error("Expected Content-type to be empty got ",
request.Header.Get("Content-Type"))
}
}
func TestMAuthApp_makeRequestJSON(t *testing.T) {
mauth, err := LoadMauth(MAuthOptions{app_id, filepath.Join("test", "private_key.pem"), false})
if err != nil {
t.Error("Error creating the MAuth Struct")
}
extraHeaders := make(map[string][]string)
request, err := mauth.makeRequest("POST", "/some/url", `{"app_id":12345}`, extraHeaders)
if "application/json" != request.Header.Get("Content-type") {
t.Error("Expected Content-type to be 'application/json' got '",
request.Header.Get("Content-Type"), "'")
}
}
func TestMAuthApp_makeRequestInvalidURL(t *testing.T) {
mauth, err := LoadMauth(MAuthOptions{app_id, filepath.Join("test", "private_key.pem"), false})
if err != nil {
t.Error("Error creating the MAuth Struct")
}
extraHeaders := make(map[string][]string)
_, err = mauth.makeRequest("POST", "\x7f\x8c\x98", `{"app_id":12345}`, extraHeaders)
if err == nil {
t.Error("Expected Error with non-sensical URL")
}
}