@@ -30,7 +30,22 @@ def setUpClass(cls):
3030 {
3131 'article_id' : 'testid000002' ,
3232 'user_id' : 'test02' ,
33- 'created_at' : 1520150273
33+ 'created_at' : 1520150274
34+ },
35+ {
36+ 'article_id' : 'testid000003' ,
37+ 'user_id' : 'test02' ,
38+ 'created_at' : 1520150275
39+ },
40+ {
41+ 'article_id' : 'testid000004' ,
42+ 'user_id' : 'test02' ,
43+ 'created_at' : 1520150276
44+ },
45+ {
46+ 'article_id' : 'testid000005' ,
47+ 'user_id' : 'test02' ,
48+ 'created_at' : 1520150277
3449 }
3550 ]
3651 TestsUtil .create_table (
@@ -55,7 +70,22 @@ def setUpClass(cls):
5570 'article_id' : 'testid000002' ,
5671 'status' : 'draft' ,
5772 'sort_key' : 1520150272000002
58- }
73+ },
74+ {
75+ 'article_id' : 'testid000003' ,
76+ 'status' : 'public' ,
77+ 'sort_key' : 1520150272000003
78+ },
79+ {
80+ 'article_id' : 'testid000004' ,
81+ 'status' : 'public' ,
82+ 'sort_key' : 1520150272000004
83+ },
84+ {
85+ 'article_id' : 'testid000005' ,
86+ 'status' : 'public' ,
87+ 'sort_key' : 1520150272000005
88+ },
5989 ]
6090 TestsUtil .create_table (
6191 cls .dynamodb ,
@@ -79,11 +109,109 @@ def test_main_ok_exist_article_id(self):
79109 'pathParameters' : {
80110 'article_id' : self .article_fraud_user_table_items [0 ]['article_id' ]
81111 },
112+ 'requestContext' : {
113+ 'authorizer' : {
114+ 'claims' : {
115+ 'cognito:username' : 'test03'
116+ }
117+ }
118+ }
119+ }
120+
121+ article_fraud_user_table = self .dynamodb .Table (os .environ ['ARTICLE_FRAUD_USER_TABLE_NAME' ])
122+ article_fraud_user_before = article_fraud_user_table .scan ()['Items' ]
123+
124+ article_fraud_user = MeArticlesFraudCreate (event = params , context = {}, dynamodb = self .dynamodb )
125+ response = article_fraud_user .main ()
126+
127+ article_fraud_user_after = article_fraud_user_table .scan ()['Items' ]
128+
129+ target_article_id = params ['pathParameters' ]['article_id' ]
130+ target_user_id = params ['requestContext' ]['authorizer' ]['claims' ]['cognito:username' ]
131+
132+ article_fraud_user = self .get_article_fraud_user (target_article_id , target_user_id )
133+
134+ expected_items = {
135+ 'article_id' : target_article_id ,
136+ 'user_id' : target_user_id ,
137+ 'reason' : target_reason ,
138+ 'plagiarism_url' : target_plagiarism_url ,
139+ 'plagiarism_description' : target_plagiarism_description ,
140+ 'illegal_content' : target_illegal_content ,
141+ 'created_at' : 1520150272000003
142+ }
143+
144+ self .assertEqual (response ['statusCode' ], 200 )
145+ self .assertEqual (len (article_fraud_user_after ), len (article_fraud_user_before ) + 1 )
146+ article_fraud_user_param_names = [
147+ 'article_id' ,
148+ 'user_id' ,
149+ 'created_at'
150+ ]
151+ for key in article_fraud_user_param_names :
152+ self .assertEqual (expected_items [key ], article_fraud_user [key ])
153+
154+ @patch ('time.time' , MagicMock (return_value = 1520150272000003 ))
155+ def test_main_ok_added_reason (self ):
156+ params = {
157+ 'pathParameters' : {
158+ 'article_id' : self .article_fraud_user_table_items [1 ]['article_id' ]
159+ },
160+ 'body' : json .dumps ({
161+ 'reason' : 'violence'
162+ }),
163+ 'requestContext' : {
164+ 'authorizer' : {
165+ 'claims' : {
166+ 'cognito:username' : 'test03'
167+ }
168+ }
169+ }
170+ }
171+
172+ article_fraud_user_table = self .dynamodb .Table (os .environ ['ARTICLE_FRAUD_USER_TABLE_NAME' ])
173+ article_fraud_user_before = article_fraud_user_table .scan ()['Items' ]
174+
175+ article_fraud_user = MeArticlesFraudCreate (event = params , context = {}, dynamodb = self .dynamodb )
176+ response = article_fraud_user .main ()
177+
178+ article_fraud_user_after = article_fraud_user_table .scan ()['Items' ]
179+
180+ target_article_id = params ['pathParameters' ]['article_id' ]
181+ target_user_id = params ['requestContext' ]['authorizer' ]['claims' ]['cognito:username' ]
182+ body = json .loads (params ['body' ])
183+ target_reason = body .get ('reason' )
184+
185+ article_fraud_user = self .get_article_fraud_user (target_article_id , target_user_id )
186+
187+ expected_items = {
188+ 'article_id' : target_article_id ,
189+ 'user_id' : target_user_id ,
190+ 'reason' : target_reason ,
191+ 'created_at' : 1520150272000003
192+ }
193+
194+ self .assertEqual (response ['statusCode' ], 200 )
195+ self .assertEqual (len (article_fraud_user_after ), len (article_fraud_user_before ) + 1 )
196+ article_fraud_user_param_names = [
197+ 'article_id' ,
198+ 'user_id' ,
199+ 'reason' ,
200+ 'created_at'
201+ ]
202+ for key in article_fraud_user_param_names :
203+ self .assertEqual (expected_items [key ], article_fraud_user [key ])
204+
205+ @patch ('time.time' , MagicMock (return_value = 1520150272000003 ))
206+ def test_main_ok_reason_is_plagiarism (self ):
207+ params = {
208+ 'pathParameters' : {
209+ 'article_id' : self .article_fraud_user_table_items [3 ]['article_id' ]
210+ },
82211 'body' : json .dumps ({
83212 'reason' : 'plagiarism' ,
84213 'plagiarism_url' : 'http://test.com' ,
85214 'plagiarism_description' : 'plagiarism description' ,
86- 'illegal_content' : 'illegal content'
87215 }),
88216 'requestContext' : {
89217 'authorizer' : {
@@ -108,7 +236,6 @@ def test_main_ok_exist_article_id(self):
108236 target_reason = body .get ('reason' )
109237 target_plagiarism_url = body .get ('plagiarism_url' )
110238 target_plagiarism_description = body .get ('plagiarism_description' )
111- target_illegal_content = body .get ('illegal_content' )
112239
113240 article_fraud_user = self .get_article_fraud_user (target_article_id , target_user_id )
114241
@@ -118,7 +245,6 @@ def test_main_ok_exist_article_id(self):
118245 'reason' : target_reason ,
119246 'plagiarism_url' : target_plagiarism_url ,
120247 'plagiarism_description' : target_plagiarism_description ,
121- 'illegal_content' : target_illegal_content ,
122248 'created_at' : 1520150272000003
123249 }
124250
@@ -130,6 +256,60 @@ def test_main_ok_exist_article_id(self):
130256 'reason' ,
131257 'plagiarism_url' ,
132258 'plagiarism_description' ,
259+ 'created_at'
260+ ]
261+ for key in article_fraud_user_param_names :
262+ self .assertEqual (expected_items [key ], article_fraud_user [key ])
263+
264+ @patch ('time.time' , MagicMock (return_value = 1520150272000003 ))
265+ def test_main_ok_reason_is_other (self ):
266+ params = {
267+ 'pathParameters' : {
268+ 'article_id' : self .article_fraud_user_table_items [4 ]['article_id' ]
269+ },
270+ 'body' : json .dumps ({
271+ 'reason' : 'other' ,
272+ 'illegal_content' : 'illegal content'
273+ }),
274+ 'requestContext' : {
275+ 'authorizer' : {
276+ 'claims' : {
277+ 'cognito:username' : 'test03'
278+ }
279+ }
280+ }
281+ }
282+
283+ article_fraud_user_table = self .dynamodb .Table (os .environ ['ARTICLE_FRAUD_USER_TABLE_NAME' ])
284+ article_fraud_user_before = article_fraud_user_table .scan ()['Items' ]
285+
286+ article_fraud_user = MeArticlesFraudCreate (event = params , context = {}, dynamodb = self .dynamodb )
287+ response = article_fraud_user .main ()
288+
289+ article_fraud_user_after = article_fraud_user_table .scan ()['Items' ]
290+
291+ target_article_id = params ['pathParameters' ]['article_id' ]
292+ target_user_id = params ['requestContext' ]['authorizer' ]['claims' ]['cognito:username' ]
293+ body = json .loads (params ['body' ])
294+ target_reason = body .get ('reason' )
295+ target_illegal_content = body .get ('illegal_content' )
296+
297+ article_fraud_user = self .get_article_fraud_user (target_article_id , target_user_id )
298+
299+ expected_items = {
300+ 'article_id' : target_article_id ,
301+ 'user_id' : target_user_id ,
302+ 'reason' : target_reason ,
303+ 'illegal_content' : target_illegal_content ,
304+ 'created_at' : 1520150272000003
305+ }
306+
307+ self .assertEqual (response ['statusCode' ], 200 )
308+ self .assertEqual (len (article_fraud_user_after ), len (article_fraud_user_before ) + 1 )
309+ article_fraud_user_param_names = [
310+ 'article_id' ,
311+ 'user_id' ,
312+ 'reason' ,
133313 'illegal_content' ,
134314 'created_at'
135315 ]
@@ -212,9 +392,7 @@ def test_validation_required_plagiarism_detail_when_reason_is_plagiarism(self):
212392 body = {
213393 'body' : json .dumps (
214394 {
215- 'reason' : 'plagiarism' ,
216- 'plagiarism_url' : '' ,
217- 'plagiarism_description' : ''
395+ 'reason' : 'plagiarism'
218396 }
219397 )
220398 }
@@ -237,8 +415,7 @@ def test_validation_required_illegal_content_when_reason_is_illegal(self):
237415 body = {
238416 'body' : json .dumps (
239417 {
240- 'reason' : 'illegal' ,
241- 'illegal_content' : ''
418+ 'reason' : 'illegal'
242419 }
243420 )
244421 }
@@ -249,8 +426,7 @@ def test_validation_required_illegal_content_when_reason_is_other(self):
249426 body = {
250427 'body' : json .dumps (
251428 {
252- 'reason' : 'other' ,
253- 'illegal_content' : ''
429+ 'reason' : 'other'
254430 }
255431 )
256432 }
@@ -291,7 +467,7 @@ def get_article_fraud_user(self, article_id, user_id):
291467 def get_parameters_other_than_body (self ):
292468 basic_params = {
293469 'pathParameters' : {
294- 'article_id' : self .article_fraud_user_table_items [1 ]['article_id' ]
470+ 'article_id' : self .article_fraud_user_table_items [5 ]['article_id' ]
295471 },
296472 'requestContext' : {
297473 'authorizer' : {
0 commit comments