-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathauth_test.go
More file actions
150 lines (133 loc) · 3.52 KB
/
auth_test.go
File metadata and controls
150 lines (133 loc) · 3.52 KB
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
package commands
import (
"bytes"
"fmt"
"io"
"net/http"
"testing"
"github.com/drycc/workflow-cli/pkg/testutil"
"github.com/stretchr/testify/assert"
)
const keyFixture = "fdbf3b34742e4ed2be4dfa848af13007"
func TestLogin(t *testing.T) {
t.Skip("Skip long running tests")
cf, server, err := testutil.NewTestServerAndClient()
if err != nil {
t.Fatal(err)
}
defer server.Close()
var b bytes.Buffer
cmdr := DryccCmd{WOut: &b, ConfigFile: cf}
server.Mux.HandleFunc("/v2/", func(w http.ResponseWriter, _ *http.Request) {
testutil.SetHeaders(w)
w.WriteHeader(http.StatusUnauthorized)
fmt.Fprintf(w, `{}`)
})
server.Mux.HandleFunc("/v2/auth/login/", func(w http.ResponseWriter, r *http.Request) {
body, err := io.ReadAll(r.Body)
if err != nil {
fmt.Println(err)
w.WriteHeader(http.StatusInternalServerError)
w.Write(nil)
}
w.WriteHeader(http.StatusFound)
if len(body) == 0 {
testutil.SetHeaders(w)
w.Header().Add("Location", fmt.Sprintf("/v2/login/drycc/?key=%s/", keyFixture))
w.WriteHeader(http.StatusOK)
w.Write(nil)
} else {
w.Write([]byte(fmt.Sprintf(`{"key": "%s"}`, keyFixture)))
}
})
server.Mux.HandleFunc(fmt.Sprintf("/v2/auth/token/%s/", keyFixture), func(w http.ResponseWriter, _ *http.Request) {
testutil.SetHeaders(w)
w.WriteHeader(http.StatusOK)
w.Write([]byte(`{"username":"test-user","token":"eaf2d1d85f6b410b81d94bfec159019b"}`))
w.Write(nil)
})
err = cmdr.Login(server.Server.URL, false, "", "")
assert.NoError(t, err)
}
func TestLogout(t *testing.T) {
cf, server, err := testutil.NewTestServerAndClient()
if err != nil {
t.Fatal(err)
}
defer server.Close()
var b bytes.Buffer
cmdr := DryccCmd{WOut: &b, ConfigFile: cf}
err = cmdr.Logout()
assert.NoError(t, err)
assert.Equal(t, b.String(), "Logged out\n", "output")
}
func TestWhoami(t *testing.T) {
t.Parallel()
cf, server, err := testutil.NewTestServerAndClient()
if err != nil {
t.Fatal(err)
}
defer server.Close()
var b bytes.Buffer
cmdr := DryccCmd{WOut: &b, ConfigFile: cf}
server.Mux.HandleFunc("/v2/auth/whoami/", func(w http.ResponseWriter, _ *http.Request) {
testutil.SetHeaders(w)
fmt.Fprintf(w, `{
"email": "test@example.com",
"username": "test",
"first_name": "",
"last_name": "",
"is_superuser": true,
"is_staff": true,
"groups": [],
"user_permissions": [],
"last_login": "2016-09-12T22:15:26Z",
"date_joined": "2015-09-12T22:15:26Z",
"is_active": true
}`)
})
err = cmdr.Whoami(false)
assert.NoError(t, err)
expected := fmt.Sprintf("You are test at %s\n", server.Server.URL)
assert.Equal(t, b.String(), expected, "output")
}
func TestWhoamiAll(t *testing.T) {
t.Parallel()
cf, server, err := testutil.NewTestServerAndClient()
if err != nil {
t.Fatal(err)
}
defer server.Close()
var b bytes.Buffer
cmdr := DryccCmd{WOut: &b, ConfigFile: cf}
server.Mux.HandleFunc("/v2/auth/whoami/", func(w http.ResponseWriter, _ *http.Request) {
testutil.SetHeaders(w)
fmt.Fprintf(w, `{
"email": "test@example.com",
"username": "test",
"first_name": "test",
"last_name": "test",
"is_superuser": true,
"is_staff": true,
"groups": [],
"user_permissions": [],
"last_login": "2016-09-12T22:15:26Z",
"date_joined": "2015-09-12T22:15:26Z",
"is_active": true
}`)
})
err = cmdr.Whoami(true)
assert.NoError(t, err)
expected := `ID: 0
Username: test
Email: test@example.com
First Name: test
Last Name: test
Last Login: 2016-09-12T22:15:26Z
Is Superuser: true
Is Staff: true
Is Active: true
Date Joined: 2015-09-12T22:15:26Z
`
assert.Equal(t, b.String(), expected, "output")
}