@@ -70,3 +70,146 @@ where
70
70
71
71
Ok ( next. run ( request) . await )
72
72
}
73
+
74
+ #[ cfg( test) ]
75
+ mod test {
76
+ use axum:: {
77
+ body:: Body ,
78
+ http:: { Request , StatusCode } ,
79
+ middleware:: from_fn,
80
+ routing:: get,
81
+ Extension , Router ,
82
+ } ;
83
+ use tower:: Service ;
84
+
85
+ use adapter:: {
86
+ dummy:: Dummy ,
87
+ ethereum:: test_util:: { GANACHE_1 , GANACHE_1337 } ,
88
+ } ;
89
+ use primitives:: {
90
+ test_util:: { CAMPAIGNS , CREATOR , IDS } ,
91
+ ChainOf , Channel ,
92
+ } ;
93
+
94
+ use crate :: { db:: insert_channel, test_util:: setup_dummy_app} ;
95
+
96
+ use super :: * ;
97
+
98
+ #[ tokio:: test]
99
+ async fn test_channel_loading ( ) {
100
+ let app_guard = setup_dummy_app ( ) . await ;
101
+ let app = Arc :: new ( app_guard. app ) ;
102
+
103
+ let channel_context = CAMPAIGNS [ 0 ] . of_channel ( ) ;
104
+ let channel = channel_context. context ;
105
+
106
+ let build_request = |id : ChannelId , auth : Option < Auth > | {
107
+ let mut request = Request :: builder ( )
108
+ . uri ( format ! ( "/{id}/test" ) )
109
+ . extension ( app. clone ( ) ) ;
110
+ if let Some ( auth) = auth {
111
+ request = request. extension ( auth) ;
112
+ }
113
+
114
+ request. body ( Body :: empty ( ) ) . expect ( "Should build Request" )
115
+ } ;
116
+
117
+ async fn handle (
118
+ Extension ( channel_context) : Extension < ChainOf < Channel > > ,
119
+ Path ( ( id, another) ) : Path < ( ChannelId , String ) > ,
120
+ ) -> String {
121
+ assert_eq ! ( id, channel_context. context. id( ) ) ;
122
+ assert_eq ! ( another, "test" ) ;
123
+ "Ok" . into ( )
124
+ }
125
+
126
+ let mut router = Router :: new ( )
127
+ . route ( "/:id/:another" , get ( handle) )
128
+ . layer ( from_fn ( channel_load :: < Dummy , _ > ) ) ;
129
+
130
+ // bad ChannelId
131
+ {
132
+ let mut request = build_request ( channel. id ( ) , None ) ;
133
+ * request. uri_mut ( ) = "/WrongChannelId" . parse ( ) . unwrap ( ) ;
134
+
135
+ let response = router
136
+ . call ( request)
137
+ . await
138
+ . expect ( "Should make request to Router" ) ;
139
+
140
+ assert_eq ! ( StatusCode :: BAD_REQUEST , response. status( ) ) ;
141
+ }
142
+
143
+ // non-existent Channel
144
+ {
145
+ let request = build_request ( channel. id ( ) , None ) ;
146
+
147
+ let response = router
148
+ . call ( request)
149
+ . await
150
+ . expect ( "Should make request to Router" ) ;
151
+
152
+ assert_eq ! ( response. status( ) , StatusCode :: NOT_FOUND ) ;
153
+ }
154
+
155
+ // insert Channel
156
+ insert_channel ( & app. pool , & channel_context)
157
+ . await
158
+ . expect ( "Should insert Channel" ) ;
159
+
160
+ // existing Channel
161
+ {
162
+ let request = build_request ( channel. id ( ) , None ) ;
163
+
164
+ let response = router
165
+ . call ( request)
166
+ . await
167
+ . expect ( "Should make request to Router" ) ;
168
+
169
+ assert_eq ! ( response. status( ) , StatusCode :: OK ) ;
170
+ }
171
+
172
+ // existing Channel with Auth from a different Chain
173
+ {
174
+ let not_same_chain = Auth {
175
+ era : 1 ,
176
+ uid : IDS [ & CREATOR ] ,
177
+ chain : GANACHE_1 . clone ( ) ,
178
+ } ;
179
+
180
+ assert_ne ! ( channel_context. chain, not_same_chain. chain, "The chain of the Channel should be different than the chain of the Auth for this test!" ) ;
181
+
182
+ let request = build_request ( channel. id ( ) , Some ( not_same_chain) ) ;
183
+
184
+ let response = router
185
+ . call ( request)
186
+ . await
187
+ . expect ( "Should make request to Router" ) ;
188
+
189
+ assert_eq ! ( response. status( ) , StatusCode :: FORBIDDEN ) ;
190
+ }
191
+
192
+ // existing Channel with Auth with the same Chain
193
+ {
194
+ let same_chain = Auth {
195
+ era : 1 ,
196
+ uid : IDS [ & CREATOR ] ,
197
+ chain : GANACHE_1337 . clone ( ) ,
198
+ } ;
199
+
200
+ assert_eq ! (
201
+ channel_context. chain, same_chain. chain,
202
+ "The chain of the Channel should be the same as the Auth for this test!"
203
+ ) ;
204
+
205
+ let request = build_request ( channel. id ( ) , Some ( same_chain) ) ;
206
+
207
+ let response = router
208
+ . call ( request)
209
+ . await
210
+ . expect ( "Should make request to Router" ) ;
211
+
212
+ assert_eq ! ( response. status( ) , StatusCode :: OK ) ;
213
+ }
214
+ }
215
+ }
0 commit comments