1717package filter
1818
1919import (
20- "errors"
2120 "net/http"
22- "net/url"
2321 "testing"
2422 "time"
2523
2624 "github.com/dgrijalva/jwt-go"
27- "github.com/shiningrush/droplet"
28- "github.com/shiningrush/droplet/data"
29- "github.com/shiningrush/droplet/middleware"
25+ "github.com/gin-gonic/gin"
3026 "github.com/stretchr/testify/assert"
3127
3228 "github.com/apisix/manager-api/internal/conf"
@@ -44,73 +40,35 @@ func genToken(username string, issueAt, expireAt int64) string {
4440 return signedToken
4541}
4642
47- type mockMiddleware struct {
48- middleware.BaseMiddleware
49- }
50-
51- func (mw * mockMiddleware ) Handle (ctx droplet.Context ) error {
52- return errors .New ("next middleware" )
53- }
54-
55- func testPanic (t * testing.T , mw AuthenticationMiddleware , ctx droplet.Context ) {
56- defer func () {
57- panicErr := recover ()
58- assert .Contains (t , panicErr .(error ).Error (), "input middleware cannot get http request" )
59- }()
60- _ = mw .Handle (ctx )
61- }
62-
6343func TestAuthenticationMiddleware_Handle (t * testing.T ) {
64- ctx := droplet .NewContext ()
65- fakeReq , _ := http .NewRequest (http .MethodGet , "" , nil )
66- expectOutput := & data.SpecCodeResponse {
67- Response : data.Response {
68- Code : 010013 ,
69- Message : "request unauthorized" ,
70- },
71- StatusCode : http .StatusUnauthorized ,
72- }
73-
74- mw := AuthenticationMiddleware {}
75- mockMw := mockMiddleware {}
76- mw .SetNext (& mockMw )
77-
78- // test without http.Request
79- testPanic (t , mw , ctx )
80-
81- ctx .Set (middleware .KeyHttpRequest , fakeReq )
44+ r := gin .New ()
45+ r .Use (Authentication ())
46+ r .GET ("/*path" , func (c * gin.Context ) {
47+ })
8248
83- // test without token check
84- fakeReq .URL = & url.URL {Path : "/apisix/admin/user/login" }
85- assert .Equal (t , mw .Handle (ctx ), errors .New ("next middleware" ))
49+ w := performRequest (r , "GET" , "/apisix/admin/user/login" , nil )
50+ assert .Equal (t , http .StatusOK , w .Code )
8651
87- // test without authorization header
88- fakeReq .URL = & url.URL {Path : "/apisix/admin/routes" }
89- assert .Nil (t , mw .Handle (ctx ))
90- assert .Equal (t , expectOutput , ctx .Output ().(* data.SpecCodeResponse ))
52+ w = performRequest (r , "GET" , "/apisix/admin/routes" , nil )
53+ assert .Equal (t , http .StatusUnauthorized , w .Code )
9154
9255 // test with token expire
9356 expireToken := genToken ("admin" , time .Now ().Unix (), time .Now ().Unix ()- 60 * 3600 )
94- fakeReq .Header .Set ("Authorization" , expireToken )
95- assert .Nil (t , mw .Handle (ctx ))
96- assert .Equal (t , expectOutput , ctx .Output ().(* data.SpecCodeResponse ))
57+ w = performRequest (r , "GET" , "/apisix/admin/routes" , map [string ]string {"Authorization" : expireToken })
58+ assert .Equal (t , http .StatusUnauthorized , w .Code )
9759
98- // test with temp subject
99- tempSubjectToken := genToken ("" , time .Now ().Unix (), time .Now ().Unix ()+ 60 * 3600 )
100- fakeReq .Header .Set ("Authorization" , tempSubjectToken )
101- assert .Nil (t , mw .Handle (ctx ))
102- assert .Equal (t , expectOutput , ctx .Output ().(* data.SpecCodeResponse ))
60+ // test with empty subject
61+ emptySubjectToken := genToken ("" , time .Now ().Unix (), time .Now ().Unix ()+ 60 * 3600 )
62+ w = performRequest (r , "GET" , "/apisix/admin/routes" , map [string ]string {"Authorization" : emptySubjectToken })
63+ assert .Equal (t , http .StatusUnauthorized , w .Code )
10364
104- // test username doesn't exist
105- userToken := genToken ("user1" , time .Now ().Unix (), time .Now ().Unix ()+ 60 * 3600 )
106- fakeReq .Header .Set ("Authorization" , userToken )
107- assert .Nil (t , mw .Handle (ctx ))
108- assert .Equal (t , expectOutput , ctx .Output ().(* data.SpecCodeResponse ))
65+ // test token with nonexistent username
66+ nonexistentUserToken := genToken ("user1" , time .Now ().Unix (), time .Now ().Unix ()+ 60 * 3600 )
67+ w = performRequest (r , "GET" , "/apisix/admin/routes" , map [string ]string {"Authorization" : nonexistentUserToken })
68+ assert .Equal (t , http .StatusUnauthorized , w .Code )
10969
11070 // test auth success
111- adminToken := genToken ("admin" , time .Now ().Unix (), time .Now ().Unix ()+ 60 * 3600 )
112- fakeReq .Header .Set ("Authorization" , adminToken )
113- ctx .SetOutput ("test data" )
114- assert .Equal (t , mw .Handle (ctx ), errors .New ("next middleware" ))
115- assert .Equal (t , "test data" , ctx .Output ().(string ))
71+ validToken := genToken ("admin" , time .Now ().Unix (), time .Now ().Unix ()+ 60 * 3600 )
72+ w = performRequest (r , "GET" , "/apisix/admin/routes" , map [string ]string {"Authorization" : validToken })
73+ assert .Equal (t , http .StatusOK , w .Code )
11674}
0 commit comments