-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi_test.go
163 lines (130 loc) · 3.32 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
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
159
160
161
162
163
package cpanel
import (
"encoding/json"
"fmt"
"testing"
"github.com/lucidcube/go-cpanel/uapi"
"github.com/lucidcube/go-cpanel/whm"
)
const (
testUser = "provtester"
testToken = "PMIK472JO3JNYT6NCOA9W3V5C9UFNGBB"
testHost = "cpanel.lucidcube.com"
testKey = "test"
)
// TestAPICall is test function for iterative testing
func TestAPICalls(t *testing.T) {
conn, err := New(testToken, testUser, testHost)
if err != nil {
t.Fatal(err)
}
// Login URL
r, err := conn.WHM.GetLoginURL()
if err != nil {
t.Fatal(err)
}
fmt.Printf("\nLogin: %s\n", r)
// Account Stats
r2, err := conn.UAPI.GetAccountStats(uapi.AccountStatCollection{uapi.FTPAccounts, uapi.EmailAccounts})
if err != nil {
t.Fatal(err)
}
gotFTP := false
gotEmail := false
for _, i := range r2 {
switch i.StatType {
case uapi.FTPAccounts:
gotFTP = true
case uapi.EmailAccounts:
gotEmail = true
}
}
if !gotFTP || !gotEmail {
t.Fatal("Did not result in expected stats")
}
fmt.Printf("\nAccount Stats: %v\n", r2)
// Email
r3, err := conn.WHM.GetEmailAccountList()
if err != nil {
t.Fatal(err)
}
fmt.Printf("\nEmail accounts: %v\n", r3)
// Files
r4, err := conn.UAPI.GetDirectoryFileListing("")
if err != nil {
t.Fatal(err)
}
fmt.Printf("Files: %d\n", len(r4))
// Disk usage
r5, err := conn.WHM.GetDiskUsage()
if err != nil {
t.Fatal(err)
}
fmt.Printf("Disk Usage contents: %d\n", len(r5.CPanelResult.Data))
// Site stats
r6, err := conn.WHM.GetStatSites()
if err != nil {
t.Fatal(err)
}
fmt.Printf("AWSites: %d\n", len(r6))
// Create email account
r7, err := conn.UAPI.CreateEmailAccount("qwe@provtest.com", "WQE1242!#!@")
if err != nil {
if err.Error() != "The account qwe@provtest.com already exists!" {
t.Fatal(err)
}
}
fmt.Printf("Created email address: %v\n", r7)
// Domain listing
r8, err := conn.UAPI.GetDomainListing()
if err != nil {
t.Fatal(err)
}
fmt.Printf("Domains: %d\n", len(r8))
}
func TestCreateAccountResponse(t *testing.T) {
rawResponse := `{"metadata":{"command":"createacct","output":{"raw":"RAWOUTPUT"},"version":1,"result":1,"reason":"Account Creation Ok"},"data":{"nameserver2":"ns2.lucidcube.com","nameserver4":"",
"nameservera3":null,"nameservera2":null,"package":"default","nameserverentry":null,"ip":"123.123.123.123","nameserverentry3":null,"nameserver3":"","nameservera4":null,"nameservera":null,
"nameserverentry4":null,"nameserverentry2":null,"nameserver":"ns1.lucidcube.com"}}`
resp := &whm.CreateAccountResponse{}
decodeErr := json.Unmarshal([]byte(rawResponse), resp)
if decodeErr != nil {
t.Fail()
}
if resp.MetaData.Command != "createacct" {
t.Log("Incorrect command")
t.Fail()
}
if resp.MetaData.Version != 1 {
t.Log("Incorrect version")
t.Fail()
}
if resp.MetaData.Output.Raw != "RAWOUTPUT" {
t.Log("Incorrect raw output")
t.Fail()
}
if resp.MetaData.Result != 1 {
t.Log("Incorrect result")
t.Fail()
}
if resp.MetaData.Reason != "Account Creation Ok" {
t.Log("Incorrect reason")
t.Fail()
}
if resp.Data.Nameserver != "ns1.lucidcube.com" {
t.Log("Incorrect nameserver")
t.Fail()
}
if resp.Data.Nameserver2 != "ns2.lucidcube.com" {
t.Log("Incorrect nameserver")
t.Fail()
}
if resp.Data.Package != "default" {
t.Log("Incorrect package")
t.Fail()
}
if resp.Data.IP != "123.123.123.123" {
t.Log("Incorrect IP")
t.Fail()
}
}