-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapi_test.go
85 lines (76 loc) · 1.85 KB
/
api_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
package iland_test
import (
"encoding/json"
"log"
"os"
"testing"
iland "github.com/ilanddev/golang-sdk"
"github.com/stretchr/testify/assert"
)
// Load API configuration properties from config.json file.
func loadConfig() *iland.Config {
file, err := os.Open("config.json")
if err != nil {
log.Fatal("Error loading config file:", err)
}
decoder := json.NewDecoder(file)
configuration := iland.Config{}
err = decoder.Decode(&configuration)
if err != nil {
log.Fatal("Error loading config file:", err)
}
return &configuration
}
var c = iland.NewClient(loadConfig())
// Test Get request.
func TestGet(t *testing.T) {
_, err := c.Get("/user/testman?expand=orgs")
if err != nil {
t.Fatal(err)
}
}
// Test Post request.
func TestPostGet(t *testing.T) {
updateEmails := "\n{\"username\":\"testman\",\"emails\":[\"[email protected]\"]}"
x, err := c.Post("/user/testman/alert-emails", updateEmails)
log.Println(x)
if err != nil {
t.Fatal(err)
}
afterUpdate, err := c.Get("/user/testman/alert-emails")
if err != nil {
t.Fatal(err)
}
assert.Equal(t, updateEmails, afterUpdate)
}
// Test Put Request.
func TestPutGet(t *testing.T) {
updatePrefs := "\n{\"pref_map\":{\"a\":\"1\"}}"
_, err := c.Put("/user/testman/preferences", updatePrefs)
if err != nil {
t.Fatal(err)
}
afterUpdate, err := c.Get("/user/testman/preferences")
if err != nil {
t.Fatal(err)
}
assert.Equal(t, updatePrefs, afterUpdate)
}
//Test Error handling.
func TestError404(t *testing.T) {
_, err := c.Get("/user/fakeUserHereMan")
if err == nil {
t.Fatal("Should have gotten an API error")
} else {
log.Println("Error successfully caught: ", err)
}
}
// Test a delete request.
func TestDelete404(t *testing.T) {
_, err := c.Delete("/user/XXXXXXXXXXX")
if err == nil {
t.Fatal("Should have gotten an API error")
} else {
log.Println("Error succesfully caught: ", err)
}
}