@@ -105,10 +105,10 @@ describe('/mc/v1/servers/blocked/known', () => {
105105} ) ;
106106
107107describe ( '/mc/v1/servers/blocked/check' , ( ) => {
108- test . each ( [
109- [ 'example.com' , { 'example.com' : true , '*.example.com' : true , '*.com' : false } ] ,
110- [ 'example.com:25565' , { 'example.com' : true , '*.example.com' : true , '*.com' : false } ] ,
111- [ 'lobby.mc.example.com' , {
108+ test . only . each ( [
109+ [ 'example.com' , 1 , { 'example.com' : true , '*.example.com' : true , '*.com' : false } ] ,
110+ [ 'example.com:25565' , 1 , { 'example.com' : true , '*.example.com' : true , '*.com' : false } ] ,
111+ [ 'lobby.mc.example.com' , 5 , {
112112 '*.lobby.mc.example.com' : false ,
113113 'lobby.mc.example.com' : false ,
114114 '*.mc.example.com' : false ,
@@ -117,45 +117,46 @@ describe('/mc/v1/servers/blocked/check', () => {
117117 'example.com' : true ,
118118 '*.com' : false ,
119119 } ] ,
120- [ '1.1.1.1' , { '1.1.1.1' : false , '1.1.1.*' : false , '1.1.*' : false , '1.*' : false } ] ,
121- [ '1.1.1.1:25565' , { '1.1.1.1' : false , '1.1.1.*' : false , '1.1.*' : false , '1.*' : false } ] ,
122- [ '2606:4700:4700::1111' , { '2606:4700:4700::1111' : false } ] ,
123- ] ) ( 'Expect hosts to be hashed and checked: %j' , async ( host : string , expected : { [ key : string ] : boolean } ) => {
124- const databaseClient = container . resolve ( DatabaseClient ) as DeepMockProxy < DatabaseClient > ;
125- databaseClient . serverBlocklist . findMany
126- . mockResolvedValue ( [
127- { sha1 : Buffer . from ( '0caaf24ab1a0c33440c06afe99df986365b0781f' , 'hex' ) , host : 'example.com' } ,
128- { sha1 : Buffer . from ( '8c7122d652cb7be22d1986f1f30b07fd5108d9c0' , 'hex' ) , host : '*.example.com' } ,
129- { sha1 : Buffer . from ( '9b054583eccc3422abd9da7b80c185853c1dd61d' , 'hex' ) , host : null } ,
130- ] ) ;
131- databaseClient . $transaction . mockImplementation ( ( callback ) => callback ( databaseClient ) ) ;
132- databaseClient . serverBlocklistHostHashes . findMany . mockResolvedValue ( [ { host : 'example.com' } , { host : '*.example.com' } ] as any ) ;
133-
134- const fastifyWebServer = container . resolve ( FastifyWebServer ) ;
135- const fastify = ( fastifyWebServer as any ) . fastify as FastifyInstance ;
136- const response = await fastify . inject ( {
137- method : 'GET' ,
138- url : '/mc/v1/servers/blocked/check?host=' + encodeURIComponent ( host ) ,
139- } ) ;
140-
141- expect ( response . statusCode ) . toBe ( 200 ) ;
142- expect ( response . json ( ) ) . toEqual ( expected ) ;
143- expect ( response . headers [ 'content-type' ] ) . toBe ( 'application/json; charset=utf-8' ) ;
144- expect ( response . headers [ 'cache-control' ] ) . toBe ( 'public, max-age=120, s-maxage=120' ) ;
145-
146- expect ( databaseClient . serverBlocklist . findMany ) . toHaveBeenCalledTimes ( 1 ) ;
147- expect ( databaseClient . serverBlocklist . findMany ) . toHaveBeenCalledWith ( { select : { sha1 : true } } ) ;
148-
149- expect ( databaseClient . $transaction ) . toHaveBeenCalledTimes ( 1 ) ;
150- expect ( databaseClient . serverBlocklistHostHashes . findMany ) . toHaveBeenCalledTimes ( 1 ) ;
151- expect ( databaseClient . serverBlocklistHostHashes . findMany ) . toHaveBeenCalledWith ( {
152- where : { sha1 : { in : expect . any ( Array ) } } ,
153- select : { host : true } ,
120+ [ '1.1.1.1' , 4 , { '1.1.1.1' : false , '1.1.1.*' : false , '1.1.*' : false , '1.*' : false } ] ,
121+ [ '1.1.1.1:25565' , 4 , { '1.1.1.1' : false , '1.1.1.*' : false , '1.1.*' : false , '1.*' : false } ] ,
122+ [ '2606:4700:4700::1111' , 1 , { '2606:4700:4700::1111' : false } ] ,
123+ ] ) (
124+ 'Expect hosts to be hashed and checked: %j' ,
125+ async ( host : string , newHashes : number , expected : { [ key : string ] : boolean } ) => {
126+ const databaseClient = container . resolve ( DatabaseClient ) as DeepMockProxy < DatabaseClient > ;
127+ databaseClient . serverBlocklist . findMany
128+ . mockResolvedValue ( [
129+ { sha1 : Buffer . from ( '0caaf24ab1a0c33440c06afe99df986365b0781f' , 'hex' ) , host : 'example.com' } ,
130+ { sha1 : Buffer . from ( '8c7122d652cb7be22d1986f1f30b07fd5108d9c0' , 'hex' ) , host : '*.example.com' } ,
131+ { sha1 : Buffer . from ( '9b054583eccc3422abd9da7b80c185853c1dd61d' , 'hex' ) , host : null } ,
132+ ] ) ;
133+ databaseClient . serverBlocklistHostHashes . createMany . mockResolvedValue ( { count : newHashes } ) ;
134+
135+ const fastifyWebServer = container . resolve ( FastifyWebServer ) ;
136+ const fastify = ( fastifyWebServer as any ) . fastify as FastifyInstance ;
137+ const response = await fastify . inject ( {
138+ method : 'GET' ,
139+ url : '/mc/v1/servers/blocked/check?host=' + encodeURIComponent ( host ) ,
140+ } ) ;
141+
142+ expect ( response . statusCode ) . toBe ( 200 ) ;
143+ expect ( response . json ( ) ) . toEqual ( expected ) ;
144+ expect ( response . headers [ 'content-type' ] ) . toBe ( 'application/json; charset=utf-8' ) ;
145+ expect ( response . headers [ 'cache-control' ] ) . toBe ( 'public, max-age=120, s-maxage=120' ) ;
146+
147+ expect ( databaseClient . serverBlocklist . findMany ) . toHaveBeenCalledTimes ( 1 ) ;
148+ expect ( databaseClient . serverBlocklist . findMany ) . toHaveBeenCalledWith ( { select : { sha1 : true } } ) ;
149+
150+ expect ( databaseClient . serverBlocklistHostHashes . createMany ) . toHaveBeenCalledTimes ( 1 ) ;
151+
152+ if ( newHashes > 0 && Object . values ( expected ) . includes ( true ) ) {
153+ expect ( databaseClient . $executeRaw ) . toHaveBeenCalledTimes ( 1 ) ;
154+ expect ( databaseClient . $executeRaw ) . toHaveBeenCalledWith ( [ 'REFRESH MATERIALIZED VIEW server_blocklist;' ] ) ;
155+ } else {
156+ expect ( databaseClient . $executeRaw ) . toHaveBeenCalledTimes ( 0 ) ;
157+ }
154158 } ) ;
155159
156- expect ( databaseClient . serverBlocklistHostHashes . createMany ) . toHaveBeenCalledTimes ( 1 ) ;
157- } ) ;
158-
159160 test . each ( [
160161 [ 'https://example.com' ] ,
161162 [ 'com' ] ,
0 commit comments