-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnextcloudgo_test.go
87 lines (80 loc) · 2.38 KB
/
nextcloudgo_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
package nextcloudgo
import (
"fmt"
"net/http"
"net/http/httptest"
"testing"
)
func TestConnect(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
if r.RequestURI == "/status.php" {
fmt.Fprintln(w, `{"installed":true,"maintenance":false,"needsDbUpgrade":false,"version":"13.0.0.6","versionstring":"13.0.0 Beta 1","edition":"","productname":"Nextcloud"}`)
return
}
}))
defer ts.Close()
nc := NextcloudGo{ServerURL: ts.URL, User: "admin", Password: "admin"}
if !nc.isConnected() {
t.Error("Server URL not set")
}
if !nc.isLoggedIn() {
t.Error("User name or password not set")
}
}
func TestStatus(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
if r.RequestURI == "/status.php" {
fmt.Fprintln(w, `{"installed":true,"maintenance":false,"needsDbUpgrade":false,"version":"13.0.0.6","versionstring":"13.0.0 Beta 1","edition":"","productname":"Nextcloud"}`)
return
}
}))
defer ts.Close()
nc := NextcloudGo{ServerURL: ts.URL, User: "admin", Password: "admin"}
s, err := nc.Status()
if err != nil {
t.Error("Could not get status from server")
}
if !s.Installed {
t.Error("Server should be installed")
}
if s.Maintenance {
t.Error("Server should be not be in maintenance")
}
if s.Version != "13.0.0.6" {
t.Error("Version was not extracted correctly")
}
}
func TestStatusNotConnected(t *testing.T) {
nc := NextcloudGo{ServerURL: "", User: "admin", Password: "admin"}
s, err := nc.Status()
if err != ErrNotConnected {
t.Error("Should receive ErrNotConnected error when calling status without ServerURL")
}
if s.Installed {
t.Error("Server is not installed")
}
if !s.Maintenance {
t.Error("Server is in maintenance")
}
if s.Version != "" {
t.Error("Version should be empty")
}
}
func TestStatusFailed(t *testing.T) {
nc := NextcloudGo{ServerURL: "https://ihopenooneeverregistersthissubdomain.nextcloud.com", User: "admin", Password: "admin"}
s, err := nc.Status()
if err == nil {
t.Error("Should receive an error when calling status with an invalid ServerURL")
}
if s.Installed {
t.Error("Server is not installed")
}
if !s.Maintenance {
t.Error("Server is in maintenance")
}
if s.Version != "" {
t.Error("Version should be empty")
}
}