@@ -10,6 +10,14 @@ import (
1010 "github.com/stretchr/testify/require"
1111)
1212
13+ type loginInput struct {
14+ Type string `json:"type"`
15+ Username string `json:"username"`
16+ Password string `json:"password"`
17+ TTL int64 `json:"ttl"`
18+ Description string `json:"description"`
19+ }
20+
1321func TestDoUserLogin (t * testing.T ) {
1422 t .Run ("success" , func (t * testing.T ) {
1523 tokenID := "token-xqmfl"
@@ -20,12 +28,14 @@ func TestDoUserLogin(t *testing.T) {
2028 assert .Equal (t , "/v1-public/login" , r .URL .Path )
2129 assert .Equal (t , "application/json" , r .Header .Get ("Content-Type" ))
2230
23- var reqBody map [ string ] string
31+ var reqBody loginInput
2432 err := json .NewDecoder (r .Body ).Decode (& reqBody )
2533 require .NoError (t , err )
26- assert .Equal (t , "localProvider" , reqBody ["type" ])
27- assert .Equal (t , "admin" , reqBody ["username" ])
28- assert .Equal (t , "secret" , reqBody ["password" ])
34+ assert .Equal (t , "localProvider" , reqBody .Type )
35+ assert .Equal (t , bootstrapDefaultUser , reqBody .Username )
36+ assert .Equal (t , bootstrapDefaultPassword , reqBody .Password )
37+ assert .Equal (t , int64 (60000 ), reqBody .TTL )
38+ assert .Equal (t , bootstrapDefaultSessionDesc , reqBody .Description )
2939
3040 w .Header ().Set ("Content-Type" , "application/json" )
3141 _ = json .NewEncoder (w ).Encode (map [string ]any {
@@ -34,7 +44,7 @@ func TestDoUserLogin(t *testing.T) {
3444 }))
3545 defer srv .Close ()
3646
37- id , token , err := DoUserLogin (srv .URL , "admin" , "secret" , bootstrapDefaultTTL , bootstrapDefaultSessionDesc , "" , true )
47+ id , token , err := DoUserLogin (srv .URL , bootstrapDefaultUser , bootstrapDefaultPassword , bootstrapDefaultTTL , bootstrapDefaultSessionDesc , "" , true )
3848 require .NoError (t , err )
3949 assert .Equal (t , tokenID , id )
4050 assert .Equal (t , tokenValue , token )
@@ -52,7 +62,7 @@ func TestDoUserLogin(t *testing.T) {
5262 }))
5363 defer srv .Close ()
5464
55- id , token , err := DoUserLogin (srv .URL , "admin" , "secret" , bootstrapDefaultTTL , bootstrapDefaultSessionDesc , "" , true )
65+ id , token , err := DoUserLogin (srv .URL , bootstrapDefaultUser , bootstrapDefaultPassword , bootstrapDefaultTTL , bootstrapDefaultSessionDesc , "" , true )
5666 require .NoError (t , err )
5767 assert .Equal (t , "saml-user-abc123" , id , "ext/ prefix should be stripped from ID" )
5868 assert .Equal (t , tokenValue , token )
@@ -68,7 +78,7 @@ func TestDoUserLogin(t *testing.T) {
6878 }))
6979 defer srv .Close ()
7080
71- _ , _ , err := DoUserLogin (srv .URL , "admin" , "wrongpass" , bootstrapDefaultTTL , bootstrapDefaultSessionDesc , "" , true )
81+ _ , _ , err := DoUserLogin (srv .URL , bootstrapDefaultUser , "wrongpass" , bootstrapDefaultTTL , bootstrapDefaultSessionDesc , "" , true )
7282 require .Error (t , err )
7383 assert .Contains (t , err .Error (), "Unauthorized" )
7484 })
@@ -82,7 +92,7 @@ func TestDoUserLogin(t *testing.T) {
8292 }))
8393 defer srv .Close ()
8494
85- _ , _ , err := DoUserLogin (srv .URL , "admin" , "secret" , bootstrapDefaultTTL , bootstrapDefaultSessionDesc , "" , true )
95+ _ , _ , err := DoUserLogin (srv .URL , bootstrapDefaultUser , bootstrapDefaultPassword , bootstrapDefaultTTL , bootstrapDefaultSessionDesc , "" , true )
8696 require .Error (t , err )
8797 assert .Contains (t , err .Error (), "invalid token format" )
8898 })
@@ -99,7 +109,7 @@ func TestDoUserLogin(t *testing.T) {
99109 }))
100110 defer srv .Close ()
101111
102- _ , _ , err := DoUserLogin (srv .URL , "admin" , "secret" , bootstrapDefaultTTL , bootstrapDefaultSessionDesc , "" , true )
112+ _ , _ , err := DoUserLogin (srv .URL , bootstrapDefaultUser , bootstrapDefaultPassword , bootstrapDefaultTTL , bootstrapDefaultSessionDesc , "" , true )
103113 require .Error (t , err )
104114 })
105115
@@ -124,6 +134,12 @@ func TestDoUserLogin(t *testing.T) {
124134
125135 if r .URL .Path == "/v3-public/localProviders/local" {
126136 assert .Equal (t , "login" , r .URL .Query ().Get ("action" ))
137+
138+ var reqBody loginInput
139+ err := json .NewDecoder (r .Body ).Decode (& reqBody )
140+ require .NoError (t , err )
141+ assert .Equal (t , int64 (60000 ), reqBody .TTL )
142+
127143 _ = json .NewEncoder (w ).Encode (map [string ]any {
128144 "token" : tokenValue ,
129145 })
@@ -134,7 +150,7 @@ func TestDoUserLogin(t *testing.T) {
134150 }))
135151 defer srv .Close ()
136152
137- id , token , err := DoUserLogin (srv .URL , "admin" , "secret" , bootstrapDefaultTTL , bootstrapDefaultSessionDesc , "" , true )
153+ id , token , err := DoUserLogin (srv .URL , bootstrapDefaultUser , bootstrapDefaultPassword , bootstrapDefaultTTL , bootstrapDefaultSessionDesc , "" , true )
138154 require .NoError (t , err )
139155 assert .Equal (t , tokenID , id )
140156 assert .Equal (t , tokenValue , token )
@@ -162,8 +178,53 @@ func TestDoUserLogin(t *testing.T) {
162178 }))
163179 defer srv .Close ()
164180
165- _ , _ , err := DoUserLogin (srv .URL , "admin" , "wrongpass" , bootstrapDefaultTTL , bootstrapDefaultSessionDesc , "" , true )
181+ _ , _ , err := DoUserLogin (srv .URL , bootstrapDefaultUser , "wrongpass" , bootstrapDefaultTTL , bootstrapDefaultSessionDesc , "" , true )
166182 require .Error (t , err )
167183 assert .Contains (t , err .Error (), "Unauthorized" )
168184 })
185+
186+ t .Run ("invalid ttl value - not a number" , func (t * testing.T ) {
187+ srv := httptest .NewServer (http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
188+ t .Error ("should not make request with invalid TTL" )
189+ }))
190+ defer srv .Close ()
191+
192+ _ , _ , err := DoUserLogin (srv .URL , bootstrapDefaultUser , bootstrapDefaultPassword , "not-a-number" , bootstrapDefaultSessionDesc , "" , true )
193+ require .Error (t , err )
194+ assert .Contains (t , err .Error (), "Invalid ttl value" )
195+ })
196+
197+ t .Run ("invalid ttl value - negative number" , func (t * testing.T ) {
198+ srv := httptest .NewServer (http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
199+ t .Error ("should not make request with negative TTL" )
200+ }))
201+ defer srv .Close ()
202+
203+ _ , _ , err := DoUserLogin (srv .URL , bootstrapDefaultUser , bootstrapDefaultPassword , "-1000" , bootstrapDefaultSessionDesc , "" , true )
204+ require .Error (t , err )
205+ assert .Contains (t , err .Error (), "Invalid ttl value" )
206+ })
207+
208+ t .Run ("valid ttl value - zero" , func (t * testing.T ) {
209+ tokenID := "token-zero-ttl"
210+ tokenValue := tokenID + ":secrettoken"
211+
212+ srv := httptest .NewServer (http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
213+ var reqBody loginInput
214+ err := json .NewDecoder (r .Body ).Decode (& reqBody )
215+ require .NoError (t , err )
216+ assert .Equal (t , int64 (0 ), reqBody .TTL )
217+
218+ w .Header ().Set ("Content-Type" , "application/json" )
219+ _ = json .NewEncoder (w ).Encode (map [string ]any {
220+ "token" : tokenValue ,
221+ })
222+ }))
223+ defer srv .Close ()
224+
225+ id , token , err := DoUserLogin (srv .URL , bootstrapDefaultUser , bootstrapDefaultPassword , "0" , bootstrapDefaultSessionDesc , "" , true )
226+ require .NoError (t , err )
227+ assert .Equal (t , tokenID , id )
228+ assert .Equal (t , tokenValue , token )
229+ })
169230}
0 commit comments