|
1 | 1 | package main |
2 | 2 |
|
3 | 3 | import ( |
4 | | - "context" |
5 | | - "encoding/base64" |
6 | | - "net/http" |
7 | | - "net/http/httptest" |
8 | | - "path" |
| 4 | + "bytes" |
9 | 5 | "strings" |
10 | 6 | "testing" |
11 | 7 |
|
12 | | - "github.com/adrg/xdg" |
13 | | - "github.com/coreos/go-systemd/v22/dbus" |
14 | 8 | "github.com/stretchr/testify/assert" |
15 | | - "github.com/zalando/go-keyring" |
16 | 9 | ) |
17 | 10 |
|
18 | | -func TestHttpHandlerRoot(t *testing.T) { |
19 | | - h := newHttpHandler(context.Background(), func() {}) |
20 | | - req := httptest.NewRequest("GET", "/", nil) |
21 | | - rw := httptest.NewRecorder() |
22 | | - |
23 | | - h.ServeHTTP(rw, req) |
24 | | - if rw.Code != http.StatusOK { |
25 | | - t.Errorf("expected 200 OK, got %d", rw.Code) |
26 | | - } |
27 | | -} |
28 | | - |
29 | | -func TestHttpHandlerLoginMissingFields(t *testing.T) { |
30 | | - h := newHttpHandler(context.Background(), func() {}) |
31 | | - req := httptest.NewRequest("POST", "/login", strings.NewReader("")) |
32 | | - req.Header.Set("Content-Type", "application/x-www-form-urlencoded") |
33 | | - rw := httptest.NewRecorder() |
34 | | - |
35 | | - h.ServeHTTP(rw, req) |
36 | | - if rw.Code != http.StatusBadRequest { |
37 | | - t.Errorf("expected 400 Bad Request, got %d", rw.Code) |
38 | | - } |
39 | | -} |
40 | | - |
41 | | -func TestHttpHandlerAuthInvalidState(t *testing.T) { |
42 | | - h := newHttpHandler(context.Background(), func() {}) |
43 | | - req := httptest.NewRequest("GET", "/auth?state=invalid", nil) |
44 | | - rw := httptest.NewRecorder() |
45 | | - |
46 | | - h.ServeHTTP(rw, req) |
47 | | - if rw.Code != http.StatusBadRequest { |
48 | | - t.Errorf("expected 400 Bad Request, got %d", rw.Code) |
49 | | - } |
50 | | -} |
51 | | - |
52 | | -func TestHttpHandlerGenerateMissingToken(t *testing.T) { |
53 | | - h := newHttpHandler(context.Background(), func() {}) |
54 | | - req := httptest.NewRequest("POST", "/generate", nil) |
55 | | - rw := httptest.NewRecorder() |
56 | | - |
57 | | - h.ServeHTTP(rw, req) |
58 | | - if rw.Code != http.StatusBadRequest { |
59 | | - t.Errorf("expected 400 Bad Request, got %d", rw.Code) |
60 | | - } |
61 | | -} |
62 | | - |
63 | | -func TestHttpHandlerGenerateMissingOidc(t *testing.T) { |
64 | | - h := newHttpHandler(context.Background(), func() {}) |
65 | | - req := httptest.NewRequest("POST", "/generate", nil) |
66 | | - req.AddCookie(&http.Cookie{Name: "token", Value: base64.StdEncoding.EncodeToString([]byte("dummy"))}) |
67 | | - rw := httptest.NewRecorder() |
68 | | - |
69 | | - h.ServeHTTP(rw, req) |
70 | | - if rw.Code != http.StatusBadRequest { |
71 | | - t.Errorf("expected 400 Bad Request, got %d", rw.Code) |
72 | | - } |
73 | | -} |
74 | | - |
75 | | -func TestCredentialGetAndSet(t *testing.T) { |
76 | | - keyring.MockInit() |
77 | | - |
78 | | - clientID := "test_client_id" |
79 | | - clientSecret := "test_client_secret" |
80 | | - |
81 | | - err := setCredentials(clientID, clientSecret) |
82 | | - assert.NoError(t, err) |
83 | | - |
84 | | - retrievedClientID, retrievedClientSecret, err := getCredentials() |
85 | | - assert.NoError(t, err) |
86 | | - assert.Equal(t, clientID, retrievedClientID) |
87 | | - assert.Equal(t, clientSecret, retrievedClientSecret) |
88 | | -} |
89 | | - |
90 | | -func TestSanitizeDriveName(t *testing.T) { |
91 | | - for _, test := range []struct { |
92 | | - input string |
93 | | - expected string |
94 | | - }{ |
95 | | - {"My Drive", "My_Drive"}, |
96 | | - {"My/Drive", "My_Drive"}, |
97 | | - {"My\\Drive", "My_Drive"}, |
98 | | - {"My:Drive", "My_Drive"}, |
99 | | - {"My?Drive", "My_Drive"}, |
100 | | - {"My*Drive", "My_Drive"}, |
101 | | - {"My\"Drive", "My_Drive"}, |
102 | | - {"My<Drive", "My_Drive"}, |
103 | | - {"My>Drive", "My_Drive"}, |
104 | | - {"My|Drive", "My_Drive"}, |
105 | | - {"My&Drive", "My_Drive"}, |
106 | | - } { |
107 | | - result := sanitizeDriveName(test.input) |
108 | | - assert.Equal(t, test.expected, result) |
109 | | - } |
110 | | -} |
111 | | - |
112 | | -func TestUnitNameToDriveName(t *testing.T) { |
113 | | - for _, test := range []struct { |
114 | | - input string |
115 | | - expected string |
116 | | - }{ |
117 | | - {"rclone@mydrive.service", "mydrive"}, |
118 | | - {"rclone@my-drive-1.service", "my-drive-1"}, |
119 | | - {"rclone@my_drive_xyz.service", "my_drive_xyz"}, |
120 | | - } { |
121 | | - result := unitNameToDriveName(test.input) |
122 | | - assert.Equal(t, test.expected, result) |
123 | | - } |
124 | | -} |
125 | | - |
126 | | -func TestDriveNameToUnitName(t *testing.T) { |
127 | | - for _, test := range []struct { |
128 | | - input string |
129 | | - expected string |
130 | | - }{ |
131 | | - {"mydrive", "rclone@mydrive.service"}, |
132 | | - {"my-drive-1", "rclone@my-drive-1.service"}, |
133 | | - {"my_drive_xyz", "rclone@my_drive_xyz.service"}, |
134 | | - } { |
135 | | - result := driveNameToUnitName(test.input) |
136 | | - assert.Equal(t, test.expected, result) |
137 | | - } |
138 | | -} |
139 | | - |
140 | | -func TestStatusesToServiceStatuses(t *testing.T) { |
141 | | - for _, test := range []struct { |
142 | | - input []dbus.UnitStatus |
143 | | - expected []serviceStatus |
144 | | - }{ |
145 | | - { |
146 | | - input: []dbus.UnitStatus{ |
147 | | - { |
148 | | - Name: "rclone@test.service", |
149 | | - Description: "Test Service", |
150 | | - ActiveState: "active", |
151 | | - }, |
152 | | - { |
153 | | - Name: "rclone@my_drive.service", |
154 | | - Description: "My Drive Service", |
155 | | - ActiveState: "inactive", |
156 | | - }, |
157 | | - }, |
158 | | - expected: []serviceStatus{ |
159 | | - { |
160 | | - Name: "test", |
161 | | - Status: "active", |
162 | | - MountPath: path.Join(xdg.Home, "google", "test"), |
163 | | - }, |
164 | | - { |
165 | | - Name: "my_drive", |
166 | | - Status: "inactive", |
167 | | - MountPath: path.Join(xdg.Home, "google", "my_drive"), |
168 | | - }, |
169 | | - }, |
170 | | - }, |
171 | | - } { |
172 | | - result := statusesToServiceStatuses(test.input) |
173 | | - assert.Equal(t, test.expected, result) |
174 | | - } |
175 | | - |
| 11 | +func TestRootCmd_UsageAndShort(t *testing.T) { |
| 12 | + assert.Equal(t, "adfinis-rclone-mgr", rootCmd.Use) |
| 13 | + assert.Contains(t, rootCmd.Short, "Google Drive") |
| 14 | + assert.Equal(t, Version, rootCmd.Version) |
| 15 | +} |
| 16 | + |
| 17 | +func TestRootCmd_HasSubcommands(t *testing.T) { |
| 18 | + subNames := []string{} |
| 19 | + for _, c := range rootCmd.Commands() { |
| 20 | + subNames = append(subNames, c.Name()) |
| 21 | + } |
| 22 | + expected := []string{ |
| 23 | + "gdrive-config", |
| 24 | + "mount", |
| 25 | + "umount", |
| 26 | + "ls", |
| 27 | + "version", |
| 28 | + } |
| 29 | + for _, name := range expected { |
| 30 | + assert.Contains(t, subNames, name) |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +func TestRootCmd_HelpOutput(t *testing.T) { |
| 35 | + buf := new(bytes.Buffer) |
| 36 | + rootCmd.SetOut(buf) |
| 37 | + rootCmd.SetArgs([]string{}) |
| 38 | + _ = rootCmd.Execute() |
| 39 | + out := buf.String() |
| 40 | + assert.Contains(t, out, "Manage rclone mounts") |
| 41 | + assert.Contains(t, out, "Usage:") |
| 42 | + assert.Contains(t, out, "Available Commands:") |
| 43 | +} |
| 44 | + |
| 45 | +func TestRootCmd_UnknownCommand(t *testing.T) { |
| 46 | + buf := new(bytes.Buffer) |
| 47 | + rootCmd.SetOut(buf) |
| 48 | + rootCmd.SetErr(buf) |
| 49 | + rootCmd.SetArgs([]string{"doesnotexist"}) |
| 50 | + err := rootCmd.Execute() |
| 51 | + assert.Error(t, err) |
| 52 | + out := buf.String() |
| 53 | + assert.True(t, strings.Contains(out, "unknown command") || strings.Contains(out, "Usage:")) |
176 | 54 | } |
0 commit comments