| 1 | package client |
| 2 | |
| 3 | import ( |
| 4 | "errors" |
| 5 | "fmt" |
| 6 | "net/http" |
| 7 | "net/http/httptest" |
| 8 | "testing" |
| 9 | "time" |
| 10 | |
| 11 | "github.com/google/go-cmp/cmp" |
| 12 | "github.com/xperimental/nextcloud-exporter/internal/testutil" |
| 13 | "github.com/xperimental/nextcloud-exporter/serverinfo" |
| 14 | ) |
| 15 | |
| 16 | func TestClient(t *testing.T) { |
| 17 | wantUserAgent := "test-ua" |
| 18 | wantUsername := "test-user" |
| 19 | wantPassword := "test-password" |
| 20 | wantToken := "test-token" |
| 21 | |
| 22 | tt := []struct { |
| 23 | desc string |
| 24 | password string |
| 25 | token string |
| 26 | handler func(t *testing.T) http.Handler |
| 27 | wantInfo *serverinfo.ServerInfo |
| 28 | wantErr error |
| 29 | }{ |
| 30 | { |
| 31 | desc: "password", |
| 32 | password: wantPassword, |
| 33 | token: "", |
| 34 | handler: func(t *testing.T) http.Handler { |
| 35 | return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { |
| 36 | user, password, ok := req.BasicAuth() |
| 37 | if !ok { |
| 38 | t.Error("failed to get authentication header") |
| 39 | } |
| 40 | |
| 41 | if user != wantUsername { |
| 42 | t.Errorf("got username %q, want %q", user, wantUsername) |
| 43 | } |
| 44 | |
| 45 | if password != wantPassword { |
| 46 | t.Errorf("got password %q, want %q", password, wantPassword) |
| 47 | } |
| 48 | |
| 49 | fmt.Fprintln(w, "{}") |
| 50 | }) |
| 51 | }, |
| 52 | wantInfo: &serverinfo.ServerInfo{}, |
| 53 | wantErr: nil, |
| 54 | }, |
| 55 | { |
| 56 | desc: "token", |
| 57 | password: "", |
| 58 | token: wantToken, |
| 59 | handler: func(t *testing.T) http.Handler { |
| 60 | return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { |
| 61 | token := req.Header.Get(nextcloudTokenHeader) |
| 62 | if token != wantToken { |
| 63 | t.Errorf("got token %q, want %q", token, wantToken) |
| 64 | } |
| 65 | |
| 66 | fmt.Fprintln(w, "{}") |
| 67 | }) |
| 68 | }, |
| 69 | wantInfo: &serverinfo.ServerInfo{}, |
| 70 | wantErr: nil, |
| 71 | }, |
| 72 | { |
| 73 | desc: "user-agent", |
| 74 | token: wantToken, |
| 75 | handler: func(t *testing.T) http.Handler { |
| 76 | return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { |
| 77 | ua := req.UserAgent() |
| 78 | if ua != wantUserAgent { |
| 79 | t.Errorf("got user-agent %q, want %q", ua, wantUserAgent) |
| 80 | } |
| 81 | |
| 82 | fmt.Fprintln(w, "{}") |
| 83 | }) |
| 84 | }, |
| 85 | wantInfo: &serverinfo.ServerInfo{}, |
| 86 | wantErr: nil, |
| 87 | }, |
| 88 | { |
| 89 | desc: "auth error", |
| 90 | password: "", |
| 91 | token: "", |
| 92 | handler: func(t *testing.T) http.Handler { |
| 93 | return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { |
| 94 | w.WriteHeader(http.StatusUnauthorized) |
| 95 | }) |
| 96 | }, |
| 97 | wantInfo: nil, |
| 98 | wantErr: ErrNotAuthorized, |
| 99 | }, |
| 100 | { |
| 101 | desc: "simple info", |
| 102 | token: wantToken, |
| 103 | handler: func(t *testing.T) http.Handler { |
| 104 | return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { |
| 105 | fmt.Fprintln(w, `{"ocs": {"meta": {"status": "OK", "statuscode": 200}}}`) |
| 106 | }) |
| 107 | }, |
| 108 | wantInfo: &serverinfo.ServerInfo{ |
| 109 | Meta: serverinfo.Meta{ |
| 110 | Status: "OK", |
| 111 | StatusCode: http.StatusOK, |
| 112 | }, |
| 113 | }, |
| 114 | wantErr: nil, |
| 115 | }, |
| 116 | { |
| 117 | desc: "parse error", |
| 118 | password: "", |
| 119 | token: "", |
| 120 | handler: func(t *testing.T) http.Handler { |
| 121 | return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { |
| 122 | w.WriteHeader(http.StatusOK) |
| 123 | }) |
| 124 | }, |
| 125 | wantInfo: nil, |
| 126 | wantErr: errors.New("can not parse server info: EOF"), |
| 127 | }, |
| 128 | { |
| 129 | desc: "ratelimit", |
| 130 | handler: func(t *testing.T) http.Handler { |
| 131 | return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { |
| 132 | w.WriteHeader(http.StatusTooManyRequests) |
| 133 | }) |
| 134 | }, |
| 135 | wantErr: ErrRatelimit, |
| 136 | }, |
| 137 | } |
| 138 | |
| 139 | for _, tc := range tt { |
| 140 | tc := tc |
| 141 | t.Run(tc.desc, func(t *testing.T) { |
| 142 | t.Parallel() |
| 143 | |
| 144 | s := httptest.NewServer(tc.handler(t)) |
| 145 | defer s.Close() |
| 146 | |
| 147 | client := New(s.URL, wantUsername, tc.password, tc.token, time.Second, wantUserAgent, false) |
| 148 | |
| 149 | info, err := client() |
| 150 | |
| 151 | if !testutil.EqualErrorMessage(err, tc.wantErr) { |
| 152 | t.Errorf("got error %q, want %q", err, tc.wantErr) |
| 153 | } |
| 154 | |
| 155 | if err != nil { |
| 156 | return |
| 157 | } |
| 158 | |
| 159 | if diff := cmp.Diff(info, tc.wantInfo); diff != "" { |
| 160 | t.Errorf("info differs: -got+want\n%s", diff) |
| 161 | } |
| 162 | }) |
| 163 | } |
| 164 | } |