forked from keighl/postmark
-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #68 from mcarey1590/forks/mcarey/fix-server-edit
feat: update server requests
- Loading branch information
Showing
2 changed files
with
294 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,83 @@ import ( | |
"goji.io/pat" | ||
) | ||
|
||
func TestGetServers(t *testing.T) { | ||
responseJSON := `{ | ||
"TotalCount": 2, | ||
"Servers": [ | ||
{ | ||
"ID": 1, | ||
"Name": "Production01", | ||
"ApiTokens": [ | ||
"server token" | ||
], | ||
"Color": "red", | ||
"SmtpApiActivated": true, | ||
"RawEmailEnabled": false, | ||
"DeliveryType": "Live", | ||
"ServerLink": "https://postmarkapp.com/servers/1/streams", | ||
"InboundAddress": "[email protected]", | ||
"InboundHookUrl": "http://inboundhook.example.com/inbound", | ||
"BounceHookUrl": "http://bouncehook.example.com/bounce", | ||
"OpenHookUrl": "http://openhook.example.com/open", | ||
"DeliveryHookUrl": "http://hooks.example.com/delivery", | ||
"PostFirstOpenOnly": true, | ||
"InboundDomain": "", | ||
"InboundHash": "yourhash", | ||
"InboundSpamThreshold": 5, | ||
"TrackOpens": false, | ||
"TrackLinks": "None", | ||
"IncludeBounceContentInHook": true, | ||
"ClickHookUrl": "http://hooks.example.com/click", | ||
"EnableSmtpApiErrorHooks": false | ||
}, | ||
{ | ||
"ID": 2, | ||
"Name": "Production02", | ||
"ApiTokens": [ | ||
"server token" | ||
], | ||
"Color": "green", | ||
"SmtpApiActivated": true, | ||
"RawEmailEnabled": false, | ||
"DeliveryType": "Sandbox", | ||
"ServerLink": "https://postmarkapp.com/servers/2/streams", | ||
"InboundAddress": "[email protected]", | ||
"InboundHookUrl": "", | ||
"BounceHookUrl": "", | ||
"OpenHookUrl": "", | ||
"DeliveryHookUrl": "http://hooks.example.com/delivery", | ||
"PostFirstOpenOnly": false, | ||
"InboundDomain": "", | ||
"InboundHash": "yourhash", | ||
"InboundSpamThreshold": 0, | ||
"TrackOpens": true, | ||
"TrackLinks": "HtmlAndText", | ||
"IncludeBounceContentInHook": false, | ||
"ClickHookUrl": "", | ||
"EnableSmtpApiErrorHooks": false | ||
} | ||
] | ||
}` | ||
|
||
tMux.HandleFunc(pat.Get("/servers"), func(w http.ResponseWriter, _ *http.Request) { | ||
_, _ = w.Write([]byte(responseJSON)) | ||
}) | ||
|
||
res, err := client.GetServers(context.Background(), 100, 10, "") | ||
if err != nil { | ||
t.Fatalf("GetServers: %s", err.Error()) | ||
} | ||
|
||
if len(res.Servers) == 0 { | ||
t.Fatalf("GetServers: unmarshaled to empty") | ||
} | ||
|
||
if res.TotalCount != 2 { | ||
t.Fatalf("GetServers: unmarshaled to empty") | ||
} | ||
} | ||
|
||
func TestGetServer(t *testing.T) { | ||
responseJSON := `{ | ||
"ID": 1, | ||
|
@@ -34,7 +111,7 @@ func TestGetServer(t *testing.T) { | |
_, _ = w.Write([]byte(responseJSON)) | ||
}) | ||
|
||
res, err := client.GetServer(context.Background(), "1") | ||
res, err := client.GetServer(context.Background(), 1) | ||
if err != nil { | ||
t.Fatalf("GetServer: %s", err.Error()) | ||
} | ||
|
@@ -44,6 +121,57 @@ func TestGetServer(t *testing.T) { | |
} | ||
} | ||
|
||
func TestCreateServer(t *testing.T) { | ||
responseJSON := `{ | ||
"ID": 1, | ||
"Name": "Staging Testing", | ||
"ApiTokens": [ | ||
"server token" | ||
], | ||
"Color": "red", | ||
"SmtpApiActivated": true, | ||
"RawEmailEnabled": false, | ||
"DeliveryType": "Live", | ||
"ServerLink": "https://postmarkapp.com/servers/1/streams", | ||
"InboundAddress": "[email protected]", | ||
"InboundHookUrl": "http://hooks.example.com/inbound", | ||
"PostFirstOpenOnly": false, | ||
"InboundDomain": "", | ||
"InboundHash": "yourhash", | ||
"InboundSpamThreshold": 5, | ||
"TrackOpens": false, | ||
"TrackLinks": "None", | ||
"IncludeBounceContentInHook": true, | ||
"EnableSmtpApiErrorHooks": false | ||
}` | ||
|
||
tMux.HandleFunc(pat.Post("/servers"), func(w http.ResponseWriter, _ *http.Request) { | ||
_, _ = w.Write([]byte(responseJSON)) | ||
}) | ||
|
||
res, err := client.CreateServer(context.Background(), ServerCreateRequest{ | ||
Name: "Staging Testing", | ||
Color: "red", | ||
SMTPAPIActivated: true, | ||
RawEmailEnabled: false, | ||
InboundHookURL: "http://hooks.example.com/inbound", | ||
PostFirstOpenOnly: false, | ||
InboundDomain: "", | ||
InboundSpamThreshold: 5, | ||
TrackOpens: false, | ||
TrackLinks: "None", | ||
IncludeBounceContentInHook: true, | ||
EnableSMTPAPIErrorHooks: false, | ||
}) | ||
if err != nil { | ||
t.Fatalf("CreateServer: %s", err.Error()) | ||
} | ||
|
||
if res.Name != "Staging Testing" { | ||
t.Fatalf("CreateServer: wrong name!") | ||
} | ||
} | ||
|
||
func TestEditServer(t *testing.T) { | ||
responseJSON := `{ | ||
"ID": 1, | ||
|
@@ -70,7 +198,7 @@ func TestEditServer(t *testing.T) { | |
_, _ = w.Write([]byte(responseJSON)) | ||
}) | ||
|
||
res, err := client.EditServer(context.Background(), "1234", Server{ | ||
res, err := client.EditServer(context.Background(), 1234, ServerEditRequest{ | ||
Name: "Production Testing", | ||
}) | ||
if err != nil { | ||
|
@@ -81,3 +209,31 @@ func TestEditServer(t *testing.T) { | |
t.Fatalf("EditServer: wrong name!: %s", res.Name) | ||
} | ||
} | ||
|
||
func TestDeleteServer(t *testing.T) { | ||
responseJSON := `{ | ||
"ErrorCode": 0, | ||
"Message": "Server 1234 removed." | ||
}` | ||
|
||
tMux.HandleFunc(pat.Delete("/servers/:serverID"), func(w http.ResponseWriter, _ *http.Request) { | ||
_, _ = w.Write([]byte(responseJSON)) | ||
}) | ||
|
||
// Success | ||
err := client.DeleteServer(context.Background(), 1234) | ||
if err != nil { | ||
t.Fatalf("DeleteServer: %s", err.Error()) | ||
} | ||
|
||
// Failure | ||
responseJSON = `{ | ||
"ErrorCode": 402, | ||
"Message": "Invalid JSON" | ||
}` | ||
|
||
err = client.DeleteServer(context.Background(), 1234) | ||
if err == nil { | ||
t.Fatalf("DeleteServer: should have failed") | ||
} | ||
} |