@@ -22,9 +22,10 @@ import (
22
22
func TestOrganizationParam (t * testing.T ) {
23
23
t .Parallel ()
24
24
25
- setupAuthentication := func (db database.Store , r * http.Request ) database.User {
25
+ setupAuthentication := func (db database.Store ) ( * http.Request , database.User ) {
26
26
var (
27
27
id , secret = randomAPIKeyParts ()
28
+ r = httptest .NewRequest ("GET" , "/" , nil )
28
29
hashed = sha256 .Sum256 ([]byte (secret ))
29
30
)
30
31
r .AddCookie (& http.Cookie {
@@ -54,46 +55,53 @@ func TestOrganizationParam(t *testing.T) {
54
55
ExpiresAt : database .Now ().Add (time .Minute ),
55
56
})
56
57
require .NoError (t , err )
57
- return user
58
+ r = r .WithContext (context .WithValue (r .Context (), chi .RouteCtxKey , chi .NewRouteContext ()))
59
+ return r , user
58
60
}
59
61
60
62
t .Run ("None" , func (t * testing.T ) {
61
63
var (
62
- db = databasefake .New ()
63
- r = httptest .NewRequest ( "GET" , "/" , nil )
64
- rw = httptest . NewRecorder ( )
65
- _ = setupAuthentication ( db , r )
64
+ db = databasefake .New ()
65
+ rw = httptest .NewRecorder ( )
66
+ r , _ = setupAuthentication ( db )
67
+ rtr = chi . NewRouter ( )
66
68
)
67
- httpmw .ExtractAPIKey (db , nil )(httpmw .ExtractOrganizationParam (db )(http .HandlerFunc (func (rw http.ResponseWriter , r * http.Request ) {
68
- }))).ServeHTTP (rw , r )
69
+ rtr .Use (
70
+ httpmw .ExtractAPIKey (db , nil ),
71
+ httpmw .ExtractOrganizationParam (db ),
72
+ )
73
+ rtr .Get ("/" , nil )
74
+ rtr .ServeHTTP (rw , r )
69
75
res := rw .Result ()
70
76
defer res .Body .Close ()
71
77
require .Equal (t , http .StatusBadRequest , res .StatusCode )
72
78
})
73
79
74
80
t .Run ("NotFound" , func (t * testing.T ) {
75
81
var (
76
- db = databasefake .New ()
77
- r = httptest .NewRequest ("GET" , "/" , nil )
78
- rw = httptest .NewRecorder ()
79
- _ = setupAuthentication (db , r )
82
+ db = databasefake .New ()
83
+ rw = httptest .NewRecorder ()
84
+ r , _ = setupAuthentication (db )
85
+ rtr = chi .NewRouter ()
86
+ )
87
+ chi .RouteContext (r .Context ()).URLParams .Add ("organization" , "nothin" )
88
+ rtr .Use (
89
+ httpmw .ExtractAPIKey (db , nil ),
90
+ httpmw .ExtractOrganizationParam (db ),
80
91
)
81
- routeContext := chi .NewRouteContext ()
82
- routeContext .URLParams .Add ("organization" , "example" )
83
- r = r .WithContext (context .WithValue (r .Context (), chi .RouteCtxKey , routeContext ))
84
- httpmw .ExtractAPIKey (db , nil )(httpmw .ExtractOrganizationParam (db )(http .HandlerFunc (func (rw http.ResponseWriter , r * http.Request ) {
85
- }))).ServeHTTP (rw , r )
92
+ rtr .Get ("/" , nil )
93
+ rtr .ServeHTTP (rw , r )
86
94
res := rw .Result ()
87
95
defer res .Body .Close ()
88
96
require .Equal (t , http .StatusNotFound , res .StatusCode )
89
97
})
90
98
91
99
t .Run ("NotInOrganization" , func (t * testing.T ) {
92
100
var (
93
- db = databasefake .New ()
94
- r = httptest .NewRequest ( "GET" , "/" , nil )
95
- rw = httptest . NewRecorder ( )
96
- _ = setupAuthentication ( db , r )
101
+ db = databasefake .New ()
102
+ rw = httptest .NewRecorder ( )
103
+ r , _ = setupAuthentication ( db )
104
+ rtr = chi . NewRouter ( )
97
105
)
98
106
organization , err := db .InsertOrganization (r .Context (), database.InsertOrganizationParams {
99
107
ID : uuid .NewString (),
@@ -102,22 +110,24 @@ func TestOrganizationParam(t *testing.T) {
102
110
UpdatedAt : database .Now (),
103
111
})
104
112
require .NoError (t , err )
105
- routeContext := chi .NewRouteContext ()
106
- routeContext .URLParams .Add ("organization" , organization .Name )
107
- r = r .WithContext (context .WithValue (r .Context (), chi .RouteCtxKey , routeContext ))
108
- httpmw .ExtractAPIKey (db , nil )(httpmw .ExtractOrganizationParam (db )(http .HandlerFunc (func (rw http.ResponseWriter , r * http.Request ) {
109
- }))).ServeHTTP (rw , r )
113
+ chi .RouteContext (r .Context ()).URLParams .Add ("organization" , organization .Name )
114
+ rtr .Use (
115
+ httpmw .ExtractAPIKey (db , nil ),
116
+ httpmw .ExtractOrganizationParam (db ),
117
+ )
118
+ rtr .Get ("/" , nil )
119
+ rtr .ServeHTTP (rw , r )
110
120
res := rw .Result ()
111
121
defer res .Body .Close ()
112
122
require .Equal (t , http .StatusUnauthorized , res .StatusCode )
113
123
})
114
124
115
125
t .Run ("Success" , func (t * testing.T ) {
116
126
var (
117
- db = databasefake .New ()
118
- r = httptest .NewRequest ( "GET" , "/" , nil )
119
- rw = httptest . NewRecorder ( )
120
- user = setupAuthentication ( db , r )
127
+ db = databasefake .New ()
128
+ rw = httptest .NewRecorder ( )
129
+ r , user = setupAuthentication ( db )
130
+ rtr = chi . NewRouter ( )
121
131
)
122
132
organization , err := db .InsertOrganization (r .Context (), database.InsertOrganizationParams {
123
133
ID : uuid .NewString (),
@@ -133,15 +143,19 @@ func TestOrganizationParam(t *testing.T) {
133
143
UpdatedAt : database .Now (),
134
144
})
135
145
require .NoError (t , err )
136
- routeContext := chi .NewRouteContext ()
137
- routeContext .URLParams .Add ("organization" , organization .Name )
138
- r = r .WithContext (context .WithValue (r .Context (), chi .RouteCtxKey , routeContext ))
139
- httpmw .ExtractAPIKey (db , nil )(httpmw .ExtractOrganizationParam (db )(http .HandlerFunc (func (rw http.ResponseWriter , r * http.Request ) {
146
+ chi .RouteContext (r .Context ()).URLParams .Add ("organization" , organization .Name )
147
+ rtr .Use (
148
+ httpmw .ExtractAPIKey (db , nil ),
149
+ httpmw .ExtractOrganizationParam (db ),
150
+ )
151
+ rtr .Get ("/" , func (rw http.ResponseWriter , r * http.Request ) {
140
152
_ = httpmw .OrganizationParam (r )
141
153
_ = httpmw .OrganizationMemberParam (r )
142
- }))).ServeHTTP (rw , r )
154
+ rw .WriteHeader (http .StatusOK )
155
+ })
156
+ rtr .ServeHTTP (rw , r )
143
157
res := rw .Result ()
144
158
defer res .Body .Close ()
145
- require .Equal (t , http .StatusUnauthorized , res .StatusCode )
159
+ require .Equal (t , http .StatusOK , res .StatusCode )
146
160
})
147
161
}
0 commit comments