1515'use strict' ;
1616
1717const assert = require ( 'assert' ) ;
18- const { spawn} = require ( 'child_process' ) ;
1918const { Storage} = require ( '@google-cloud/storage' ) ;
2019const sinon = require ( 'sinon' ) ;
21- const { request } = require ( 'gaxios ' ) ;
22- const waitPort = require ( 'wait-port ' ) ;
20+ const supertest = require ( 'supertest ' ) ;
21+ const functionsFramework = require ( '@google-cloud/functions-framework/testing ' ) ;
2322
2423const storage = new Storage ( ) ;
2524
@@ -28,31 +27,11 @@ const {BLURRED_BUCKET_NAME} = process.env;
2827const blurredBucket = storage . bucket ( BLURRED_BUCKET_NAME ) ;
2928
3029const testFiles = {
31- safe : 'bicycle .jpg' ,
30+ safe : 'wakeupcat .jpg' ,
3231 offensive : 'zombie.jpg' ,
3332} ;
3433
35- async function startFF ( port ) {
36- const ffProc = spawn ( 'npx' , [
37- 'functions-framework' ,
38- '--target' ,
39- 'blurOffensiveImages' ,
40- '--signature-type' ,
41- 'event' ,
42- '--port' ,
43- port ,
44- ] ) ;
45- const ffProcHandler = new Promise ( ( resolve , reject ) => {
46- let stdout = '' ;
47- let stderr = '' ;
48- ffProc . stdout . on ( 'data' , data => ( stdout += data ) ) ;
49- ffProc . stderr . on ( 'data' , data => ( stderr += data ) ) ;
50- ffProc . on ( 'error' , reject ) ;
51- ffProc . on ( 'exit' , c => ( c === 0 ? resolve ( stdout ) : reject ( stderr ) ) ) ;
52- } ) ;
53- await waitPort ( { host : 'localhost' , port} ) ;
54- return { ffProc, ffProcHandler} ;
55- }
34+ require ( '../index' ) ;
5635
5736describe ( 'functions/imagemagick tests' , ( ) => {
5837 before ( async ( ) => {
@@ -72,50 +51,48 @@ describe('functions/imagemagick tests', () => {
7251 }
7352 } ) ;
7453
75- beforeEach ( ( ) => sinon . stub ( console , 'error ' ) ) ;
76- afterEach ( ( ) => console . error . restore ( ) ) ;
54+ beforeEach ( ( ) => sinon . stub ( console , 'log ' ) ) ;
55+ afterEach ( ( ) => console . log . restore ( ) ) ;
7756
7857 describe ( 'functions_imagemagick_setup functions_imagemagick_analyze functions_imagemagick_blur' , ( ) => {
7958 it ( 'blurOffensiveImages detects safe images using Cloud Vision' , async ( ) => {
80- const PORT = 8080 ;
81- const { ffProc, ffProcHandler} = await startFF ( PORT ) ;
82-
83- await request ( {
84- url : `http://localhost:${ PORT } /blurOffensiveImages` ,
85- method : 'POST' ,
59+ const event = {
60+ id : '1234' ,
61+ type : 'mock-gcs-event' ,
8662 data : {
87- data : {
88- bucket : BUCKET_NAME ,
89- name : testFiles . safe ,
90- } ,
63+ bucket : BUCKET_NAME ,
64+ name : testFiles . safe ,
9165 } ,
92- } ) ;
93- ffProc . kill ( ) ;
94- const stdout = await ffProcHandler ;
95- assert . ok ( stdout . includes ( `Detected ${ testFiles . safe } as OK.` ) ) ;
66+ } ;
67+
68+ const server = functionsFramework . getTestServer ( 'blurOffensiveImages' ) ;
69+ await supertest ( server )
70+ . post ( '/' )
71+ . send ( event )
72+ . set ( 'Content-Type' , 'application/json' )
73+ . expect ( 204 ) ;
9674 } ) ;
9775
9876 it ( 'blurOffensiveImages successfully blurs offensive images' , async ( ) => {
99- const PORT = 8081 ;
100- const { ffProc, ffProcHandler} = await startFF ( PORT ) ;
101-
102- await request ( {
103- url : `http://localhost:${ PORT } /blurOffensiveImages` ,
104- method : 'POST' ,
77+ const event = {
78+ id : '1234' ,
79+ type : 'mock-gcs-event' ,
10580 data : {
106- data : {
107- bucket : BUCKET_NAME ,
108- name : testFiles . offensive ,
109- } ,
81+ bucket : BUCKET_NAME ,
82+ name : testFiles . offensive ,
11083 } ,
111- } ) ;
112-
113- ffProc . kill ( ) ;
114- const stdout = await ffProcHandler ;
115-
116- assert . ok ( stdout . includes ( `Blurred image: ${ testFiles . offensive } ` ) ) ;
117- assert . ok (
118- stdout . includes (
84+ } ;
85+
86+ const server = functionsFramework . getTestServer ( 'blurOffensiveImages' ) ;
87+ await supertest ( server )
88+ . post ( '/' )
89+ . send ( event )
90+ . set ( 'Content-Type' , 'application/json' )
91+ . expect ( 204 ) ;
92+
93+ assert ( console . log . calledWith ( `Blurred image: ${ testFiles . offensive } ` ) ) ;
94+ assert (
95+ console . log . calledWith (
11996 `Uploaded blurred image to: gs://${ BLURRED_BUCKET_NAME } /${ testFiles . offensive } `
12097 )
12198 ) ;
@@ -128,32 +105,32 @@ describe('functions/imagemagick tests', () => {
128105 } ) ;
129106
130107 it ( 'blurOffensiveImages detects missing images as safe using Cloud Vision' , async ( ) => {
131- const PORT = 8082 ;
132- const { ffProc, ffProcHandler} = await startFF ( PORT ) ;
133108 const missingFileName = 'file-does-not-exist.jpg' ;
134-
135- await request ( {
136- url : `http://localhost:${ PORT } /blurOffensiveImages` ,
137- method : 'POST' ,
109+ const event = {
110+ id : '1234' ,
111+ type : 'mock-gcs-event' ,
138112 data : {
139- data : {
140- bucket : BUCKET_NAME ,
141- name : missingFileName ,
142- } ,
113+ bucket : BUCKET_NAME ,
114+ name : missingFileName ,
143115 } ,
144- } ) ;
116+ } ;
117+
118+ const server = functionsFramework . getTestServer ( 'blurOffensiveImages' ) ;
119+ await supertest ( server )
120+ . post ( '/' )
121+ . send ( event )
122+ . set ( 'Content-Type' , 'application/json' )
123+ . expect ( 204 ) ;
145124
146- ffProc . kill ( ) ;
147- const stdout = await ffProcHandler ;
148- assert . ok ( stdout . includes ( `Detected ${ missingFileName } as OK.` ) ) ;
125+ assert ( console . log . calledWith ( `Detected ${ missingFileName } as OK.` ) ) ;
149126 } ) ;
150127 } ) ;
151128
152129 after ( async ( ) => {
153130 try {
154131 await blurredBucket . file ( testFiles . offensive ) . delete ( ) ;
155132 } catch ( err ) {
156- console . log ( 'Error deleting uploaded file:' , err . message ) ;
133+ console . error ( 'Error deleting uploaded file:' , err . message ) ;
157134 }
158135 } ) ;
159136} ) ;
0 commit comments