@@ -4,9 +4,10 @@ defmodule MastaniServer.Test.Mutation.PostTest do
4
4
import MastaniServer.Factory
5
5
import MastaniServer.Test.ConnBuilder
6
6
import MastaniServer.Test.AssertHelper
7
+ import ShortMaps
7
8
8
9
alias MastaniServer.Repo
9
- alias MastaniServer.Accounts.User
10
+ # alias MastaniServer.Accounts.User
10
11
alias MastaniServer.CMS
11
12
alias Helper.ORM
12
13
@@ -29,9 +30,10 @@ defmodule MastaniServer.Test.Mutation.PostTest do
29
30
}
30
31
}
31
32
"""
32
- test "create comment to a exsit post" , % { post: post , user_conn: conn } do
33
+ # test "create comment to a exsit post", %{post: post, user_conn: user_conn} do
34
+ test "create comment to a exsit post" , ~m( post user_conn) a do
33
35
variables = % { type: "POST" , id: post . id , body: "a test comment" }
34
- created = conn |> mutation_result ( @ create_comment_query , variables , "createComment" )
36
+ created = user_conn |> mutation_result ( @ create_comment_query , variables , "createComment" )
35
37
36
38
assert created [ "body" ] == variables . body
37
39
end
@@ -43,15 +45,15 @@ defmodule MastaniServer.Test.Mutation.PostTest do
43
45
}
44
46
}
45
47
"""
46
- test "delete a comment" , % { post: post , user_conn: conn } do
48
+ test "delete a comment" , ~m ( post user_conn) a do
47
49
variables1 = % { type: "POST" , id: post . id , body: "a test comment" }
48
- created = conn |> mutation_result ( @ create_comment_query , variables1 , "createComment" )
50
+ created = user_conn |> mutation_result ( @ create_comment_query , variables1 , "createComment" )
49
51
assert created [ "body" ] == variables1 . body
50
52
51
53
variables2 = % { id: created [ "id" ] }
52
54
53
55
deleted_comment =
54
- conn |> mutation_result ( @ delete_comment_query , variables2 , "deleteComment" )
56
+ user_conn |> mutation_result ( @ delete_comment_query , variables2 , "deleteComment" )
55
57
56
58
assert deleted_comment [ "id" ] == created [ "id" ]
57
59
@@ -69,12 +71,12 @@ defmodule MastaniServer.Test.Mutation.PostTest do
69
71
}
70
72
}
71
73
"""
72
- test "create post with valid attrs" , % { user_conn: conn } do
74
+ test "create post with valid attrs" , ~m ( user_conn) a do
73
75
{ :ok , community } = db_insert ( :community )
74
76
post_attr = mock_attrs ( :post )
75
77
76
78
variables = post_attr |> Map . merge ( % { community: community . title } )
77
- created = conn |> mutation_result ( @ create_post_query , variables , "createPost" )
79
+ created = user_conn |> mutation_result ( @ create_post_query , variables , "createPost" )
78
80
post = Repo . get_by ( CMS.Post , title: post_attr . title )
79
81
80
82
assert created [ "id" ] == to_string ( post . id )
@@ -87,31 +89,31 @@ defmodule MastaniServer.Test.Mutation.PostTest do
87
89
}
88
90
}
89
91
"""
90
- test "delete a post by post's owner" , % { owner_conn: conn , post: post } do
91
- deleted = conn |> mutation_result ( @ query , % { id: post . id } , "deletePost" )
92
+ test "delete a post by post's owner" , ~m ( owner_conn post) a do
93
+ deleted = owner_conn |> mutation_result ( @ query , % { id: post . id } , "deletePost" )
92
94
93
95
assert deleted [ "id" ] == to_string ( post . id )
94
96
assert nil == Repo . get ( CMS.Post , deleted [ "id" ] )
95
97
end
96
98
97
- test "delete a post without login user fails" , % { guest_conn: conn , post: post } do
98
- assert conn |> mutation_get_error? ( @ query , % { id: post . id } )
99
+ test "delete a post without login user fails" , ~m ( guest_conn post) a do
100
+ assert guest_conn |> mutation_get_error? ( @ query , % { id: post . id } )
99
101
end
100
102
101
- test "login user with auth passport delete a post" , % { post: post } do
103
+ test "login user with auth passport delete a post" , ~m ( post) a do
102
104
post_communities_0 = post . communities |> List . first ( ) |> Map . get ( :title )
103
105
passport_rules = % { "cms" => % { post_communities_0 => % { "post.article.delete" => true } } }
104
- conn = mock_conn ( :user , passport_rules )
106
+ rule_conn = mock_conn ( :user , passport_rules )
105
107
106
108
# assert conn |> mutation_get_error?(@query, %{id: post.id})
107
109
108
- deleted = conn |> mutation_result ( @ query , % { id: post . id } , "deletePost" )
110
+ deleted = rule_conn |> mutation_result ( @ query , % { id: post . id } , "deletePost" )
109
111
110
112
assert deleted [ "id" ] == to_string ( post . id )
111
113
end
112
114
113
- test "login user with wrong passport delete a post fails" , % { user_conn: conn , post: post } do
114
- post_communities_0 = post . communities |> List . first ( ) |> Map . get ( :title )
115
+ test "login user with wrong passport delete a post fails" , ~m ( post) a do
116
+ # post_communities_0 = post.communities |> List.first() |> Map.get(:title)
115
117
# IO.inspect(post_communities_0, label: "hello")
116
118
# CMS.stamp_passport(%User{id: user.id}, community_rules)
117
119
@@ -121,8 +123,8 @@ defmodule MastaniServer.Test.Mutation.PostTest do
121
123
assert conn |> mutation_get_error? ( @ query , % { id: post . id } )
122
124
end
123
125
124
- test "login user without passport delete a post fails" , % { user_conn: conn , post: post } do
125
- assert conn |> mutation_get_error? ( @ query , % { id: post . id } )
126
+ test "login user without passport delete a post fails" , ~m ( user_conn post) a do
127
+ assert user_conn |> mutation_get_error? ( @ query , % { id: post . id } )
126
128
end
127
129
128
130
@ query """
@@ -134,7 +136,7 @@ defmodule MastaniServer.Test.Mutation.PostTest do
134
136
}
135
137
}
136
138
"""
137
- test "update a post without login user fails" , % { guest_conn: conn , post: post } do
139
+ test "update a post without login user fails" , ~m ( guest_conn post) a do
138
140
unique_num = System . unique_integer ( [ :positive , :monotonic ] )
139
141
140
142
variables = % {
@@ -143,10 +145,10 @@ defmodule MastaniServer.Test.Mutation.PostTest do
143
145
body: "updated body#{ unique_num } "
144
146
}
145
147
146
- assert conn |> mutation_get_error? ( @ query , variables )
148
+ assert guest_conn |> mutation_get_error? ( @ query , variables )
147
149
end
148
150
149
- test "update a post with by post's owner" , % { owner_conn: conn , post: post } do
151
+ test "update a post with by post's owner" , ~m ( owner_conn post) a do
150
152
unique_num = System . unique_integer ( [ :positive , :monotonic ] )
151
153
152
154
variables = % {
@@ -155,16 +157,16 @@ defmodule MastaniServer.Test.Mutation.PostTest do
155
157
body: "updated body#{ unique_num } "
156
158
}
157
159
158
- updated_post = conn |> mutation_result ( @ query , variables , "updatePost" )
160
+ updated_post = owner_conn |> mutation_result ( @ query , variables , "updatePost" )
159
161
160
162
assert updated_post [ "title" ] == variables . title
161
163
assert updated_post [ "body" ] == variables . body
162
164
end
163
165
164
- test "login user with auth passport update a post" , % { post: post } do
166
+ test "login user with auth passport update a post" , ~m ( post) a do
165
167
post_communities_0 = post . communities |> List . first ( ) |> Map . get ( :title )
166
168
passport_rules = % { "cms" => % { post_communities_0 => % { "post.article.edit" => true } } }
167
- conn = mock_conn ( :user , passport_rules )
169
+ rule_conn = mock_conn ( :user , passport_rules )
168
170
169
171
# assert conn |> mutation_get_error?(@query, %{id: post.id})
170
172
unique_num = System . unique_integer ( [ :positive , :monotonic ] )
@@ -175,12 +177,12 @@ defmodule MastaniServer.Test.Mutation.PostTest do
175
177
body: "updated body#{ unique_num } "
176
178
}
177
179
178
- updated_post = conn |> mutation_result ( @ query , variables , "updatePost" )
180
+ updated_post = rule_conn |> mutation_result ( @ query , variables , "updatePost" )
179
181
180
182
assert updated_post [ "id" ] == to_string ( post . id )
181
183
end
182
184
183
- test "login user without passport update post fails" , % { user_conn: conn , post: post } do
185
+ test "login user without passport update post fails" , ~m ( user_conn post) a do
184
186
unique_num = System . unique_integer ( [ :positive , :monotonic ] )
185
187
186
188
variables = % {
@@ -189,7 +191,7 @@ defmodule MastaniServer.Test.Mutation.PostTest do
189
191
body: "updated body#{ unique_num } "
190
192
}
191
193
192
- assert conn |> mutation_get_error? ( @ query , variables )
194
+ assert user_conn |> mutation_get_error? ( @ query , variables )
193
195
end
194
196
end
195
197
@@ -202,25 +204,25 @@ defmodule MastaniServer.Test.Mutation.PostTest do
202
204
}
203
205
}
204
206
"""
205
- test "can set a tag to post" , % { user_conn: conn , post: post } do
207
+ test "can set a tag to post" , ~m ( user_conn post) a do
206
208
{ :ok , tag } = db_insert ( :tag )
207
209
variables = % { id: post . id , tagId: tag . id }
208
- conn |> mutation_result ( @ set_tag_query , variables , "setTag" )
210
+ user_conn |> mutation_result ( @ set_tag_query , variables , "setTag" )
209
211
{ :ok , found } = ORM . find ( CMS.Post , post . id , preload: :tags )
210
212
211
213
assoc_tags = found . tags |> Enum . map ( & & 1 . id )
212
214
assert tag . id in assoc_tags
213
215
end
214
216
215
- test "can set multi tag to a post" , % { user_conn: conn , post: post } do
217
+ test "can set multi tag to a post" , ~m ( user_conn post) a do
216
218
{ :ok , tag } = db_insert ( :tag )
217
219
{ :ok , tag2 } = db_insert ( :tag )
218
220
219
221
variables = % { id: post . id , tagId: tag . id }
220
- conn |> mutation_result ( @ set_tag_query , variables , "setTag" )
222
+ user_conn |> mutation_result ( @ set_tag_query , variables , "setTag" )
221
223
222
224
variables2 = % { id: post . id , tagId: tag2 . id }
223
- conn |> mutation_result ( @ set_tag_query , variables2 , "setTag" )
225
+ user_conn |> mutation_result ( @ set_tag_query , variables2 , "setTag" )
224
226
225
227
{ :ok , found } = ORM . find ( CMS.Post , post . id , preload: :tags )
226
228
@@ -237,23 +239,23 @@ defmodule MastaniServer.Test.Mutation.PostTest do
237
239
}
238
240
}
239
241
"""
240
- test "can unset tag from a post" , % { user_conn: conn , post: post } do
242
+ test "can unset tag from a post" , ~m ( user_conn post) a do
241
243
{ :ok , tag } = db_insert ( :tag )
242
244
{ :ok , tag2 } = db_insert ( :tag )
243
245
244
246
variables = % { id: post . id , tagId: tag . id }
245
- conn |> mutation_result ( @ set_tag_query , variables , "setTag" )
247
+ user_conn |> mutation_result ( @ set_tag_query , variables , "setTag" )
246
248
247
249
variables2 = % { id: post . id , tagId: tag2 . id }
248
- conn |> mutation_result ( @ set_tag_query , variables2 , "setTag" )
250
+ user_conn |> mutation_result ( @ set_tag_query , variables2 , "setTag" )
249
251
250
252
{ :ok , found } = ORM . find ( CMS.Post , post . id , preload: :tags )
251
253
252
254
assoc_tags = found . tags |> Enum . map ( & & 1 . id )
253
255
assert tag . id in assoc_tags
254
256
assert tag2 . id in assoc_tags
255
257
256
- conn |> mutation_result ( @ unset_tag_query , variables , "unsetTag" )
258
+ user_conn |> mutation_result ( @ unset_tag_query , variables , "unsetTag" )
257
259
{ :ok , found } = ORM . find ( CMS.Post , post . id , preload: :tags )
258
260
assoc_tags = found . tags |> Enum . map ( & & 1 . id )
259
261
@@ -271,28 +273,25 @@ defmodule MastaniServer.Test.Mutation.PostTest do
271
273
}
272
274
}
273
275
"""
274
- test "can set a community to post" , % { user_conn: conn , post: post } do
276
+ test "can set a community to post" , ~m ( user_conn post) a do
275
277
{ :ok , community } = db_insert ( :community )
276
278
variables = % { id: post . id , communityId: community . id }
277
- conn |> mutation_result ( @ set_community_query , variables , "setCommunity" )
279
+ user_conn |> mutation_result ( @ set_community_query , variables , "setCommunity" )
278
280
{ :ok , found } = ORM . find ( CMS.Post , post . id , preload: :communities )
279
281
280
282
assoc_communities = found . communities |> Enum . map ( & & 1 . id )
281
283
assert community . id in assoc_communities
282
284
end
283
285
284
- test "can set multi community to a post" , % {
285
- user_conn: conn ,
286
- post: post
287
- } do
286
+ test "can set multi community to a post" , ~m( user_conn post) a do
288
287
{ :ok , community } = db_insert ( :community )
289
288
{ :ok , community2 } = db_insert ( :community )
290
289
291
290
variables = % { id: post . id , communityId: community . id }
292
- conn |> mutation_result ( @ set_community_query , variables , "setCommunity" )
291
+ user_conn |> mutation_result ( @ set_community_query , variables , "setCommunity" )
293
292
294
293
variables2 = % { id: post . id , communityId: community2 . id }
295
- conn |> mutation_result ( @ set_community_query , variables2 , "setCommunity" )
294
+ user_conn |> mutation_result ( @ set_community_query , variables2 , "setCommunity" )
296
295
297
296
{ :ok , found } = ORM . find ( CMS.Post , post . id , preload: :communities )
298
297
@@ -308,26 +307,23 @@ defmodule MastaniServer.Test.Mutation.PostTest do
308
307
}
309
308
}
310
309
"""
311
- test "can unset community from a post" , % {
312
- user_conn: conn ,
313
- post: post
314
- } do
310
+ test "can unset community from a post" , ~m( user_conn post) a do
315
311
{ :ok , community } = db_insert ( :community )
316
312
{ :ok , community2 } = db_insert ( :community )
317
313
318
314
variables = % { id: post . id , communityId: community . id }
319
- conn |> mutation_result ( @ set_community_query , variables , "setCommunity" )
315
+ user_conn |> mutation_result ( @ set_community_query , variables , "setCommunity" )
320
316
321
317
variables2 = % { id: post . id , communityId: community2 . id }
322
- conn |> mutation_result ( @ set_community_query , variables2 , "setCommunity" )
318
+ user_conn |> mutation_result ( @ set_community_query , variables2 , "setCommunity" )
323
319
324
320
{ :ok , found } = ORM . find ( CMS.Post , post . id , preload: :communities )
325
321
326
322
assoc_communities = found . communities |> Enum . map ( & & 1 . id )
327
323
assert community . id in assoc_communities
328
324
assert community2 . id in assoc_communities
329
325
330
- conn |> mutation_result ( @ unset_community_query , variables , "unsetCommunity" )
326
+ user_conn |> mutation_result ( @ unset_community_query , variables , "unsetCommunity" )
331
327
{ :ok , found } = ORM . find ( CMS.Post , post . id , preload: :communities )
332
328
assoc_communities = found . communities |> Enum . map ( & & 1 . id )
333
329
assert community . id not in assoc_communities