@@ -11,46 +11,7 @@ import XCTest
11
11
@testable import AmplifyTestCommon
12
12
@testable import AWSPluginsCore
13
13
14
- /*
15
- type ModelWithOwnerField
16
- @model
17
- @auth(rules: [ { allow: owner, ownerField: "author" } ])
18
- {
19
- id: ID!
20
- content: String!
21
- author: String
22
- }
23
- */
24
- public struct ModelWithOwnerField : Model {
25
- public let id : String
26
- public var content : String
27
- public var author : String ?
28
- public init ( id: String = UUID ( ) . uuidString,
29
- content: String ,
30
- author: String ? ) {
31
- self . id = id
32
- self . content = content
33
- self . author = author
34
- }
35
- public enum CodingKeys : String , ModelKey {
36
- case id
37
- case content
38
- case author
39
- }
40
- public static let keys = CodingKeys . self
41
-
42
- public static let schema = defineSchema { model in
43
- let modelWithOwnerField = ModelWithOwnerField . keys
44
- model. authRules = [
45
- rule ( allow: . owner, ownerField: " author " )
46
- ]
47
- model. fields (
48
- . id( ) ,
49
- . field( modelWithOwnerField. content, is: . required, ofType: . string) ,
50
- . field( modelWithOwnerField. author, is: . optional, ofType: . string) )
51
- }
52
- }
53
-
14
+ // swiftlint:disable type_body_length
54
15
class ModelWithOwnerFieldAuthRuleTests : XCTestCase {
55
16
56
17
override func setUp( ) {
@@ -286,4 +247,194 @@ class ModelWithOwnerFieldAuthRuleTests: XCTestCase {
286
247
}
287
248
XCTAssertEqual ( variables [ " author " ] as? String , " user1 " )
288
249
}
250
+
251
+ func testModelWithMultipleAuthRules_Subscription( ) {
252
+ var documentBuilder = ModelBasedGraphQLDocumentBuilder ( modelSchema: ModelWithMultipleAuthRules . schema,
253
+ operationType: . subscription)
254
+ documentBuilder. add ( decorator: DirectiveNameDecorator ( type: . onCreate) )
255
+ documentBuilder. add ( decorator: AuthRuleDecorator ( . subscription( . onCreate, nil ) ) )
256
+ let document = documentBuilder. build ( )
257
+ let expectedQueryDocument = """
258
+ subscription OnCreateModelWithMultipleAuthRules($author: String!) {
259
+ onCreateModelWithMultipleAuthRules(author: $author) {
260
+ id
261
+ author
262
+ content
263
+ __typename
264
+ }
265
+ }
266
+ """
267
+ XCTAssertEqual ( document. name, " onCreateModelWithMultipleAuthRules " )
268
+ XCTAssertEqual ( document. stringValue, expectedQueryDocument)
269
+ }
270
+
271
+ func testModelWithMultipleAuthRulesAPIKey_Subscription( ) {
272
+ var documentBuilder = ModelBasedGraphQLDocumentBuilder ( modelSchema: ModelWithMultipleAuthRules . schema,
273
+ operationType: . subscription)
274
+ documentBuilder. add ( decorator: DirectiveNameDecorator ( type: . onCreate) )
275
+ documentBuilder. add ( decorator: AuthRuleDecorator ( . subscription( . onCreate, nil ) ,
276
+ authType: . apiKey) )
277
+ let document = documentBuilder. build ( )
278
+ let expectedQueryDocument = """
279
+ subscription OnCreateModelWithMultipleAuthRules {
280
+ onCreateModelWithMultipleAuthRules {
281
+ id
282
+ author
283
+ content
284
+ __typename
285
+ }
286
+ }
287
+ """
288
+ XCTAssertEqual ( document. name, " onCreateModelWithMultipleAuthRules " )
289
+ XCTAssertEqual ( document. stringValue, expectedQueryDocument)
290
+ }
291
+
292
+ func testModelWithOIDCOwner_Subscription( ) {
293
+ var documentBuilder = ModelBasedGraphQLDocumentBuilder ( modelSchema: ModelWithOIDCOwnerField . schema,
294
+ operationType: . subscription)
295
+ documentBuilder. add ( decorator: DirectiveNameDecorator ( type: . onCreate) )
296
+ documentBuilder. add ( decorator: AuthRuleDecorator ( . subscription( . onCreate, nil ) ) )
297
+ let document = documentBuilder. build ( )
298
+ let expectedQueryDocument = """
299
+ subscription OnCreateModelWithOIDCOwnerField($author: String!) {
300
+ onCreateModelWithOIDCOwnerField(author: $author) {
301
+ id
302
+ author
303
+ content
304
+ __typename
305
+ }
306
+ }
307
+ """
308
+ XCTAssertEqual ( document. name, " onCreateModelWithOIDCOwnerField " )
309
+ XCTAssertEqual ( document. stringValue, expectedQueryDocument)
310
+ }
311
+ }
312
+
313
+ // MARK: Test schemas
314
+
315
+ /*
316
+ type ModelWithOwnerField
317
+ @model
318
+ @auth(rules: [ { allow: owner, ownerField: "author" } ])
319
+ {
320
+ id: ID!
321
+ content: String!
322
+ author: String
323
+ }
324
+ */
325
+ public struct ModelWithOwnerField : Model {
326
+ public let id : String
327
+ public var content : String
328
+ public var author : String ?
329
+ public init ( id: String = UUID ( ) . uuidString,
330
+ content: String ,
331
+ author: String ? ) {
332
+ self . id = id
333
+ self . content = content
334
+ self . author = author
335
+ }
336
+ public enum CodingKeys : String , ModelKey {
337
+ case id
338
+ case content
339
+ case author
340
+ }
341
+ public static let keys = CodingKeys . self
342
+
343
+ public static let schema = defineSchema { model in
344
+ let modelWithOwnerField = ModelWithOwnerField . keys
345
+ model. authRules = [
346
+ rule ( allow: . owner, ownerField: " author " )
347
+ ]
348
+ model. fields (
349
+ . id( ) ,
350
+ . field( modelWithOwnerField. content, is: . required, ofType: . string) ,
351
+ . field( modelWithOwnerField. author, is: . optional, ofType: . string) )
352
+ }
353
+ }
354
+
355
+ /*
356
+ type ModelWithOIDCOwnerField
357
+ @model
358
+ @auth(rules: [ { allow: owner, ownerField: "author" } ])
359
+ {
360
+ id: ID!
361
+ content: String!
362
+ author: String
363
+ }
364
+ */
365
+ public struct ModelWithOIDCOwnerField : Model {
366
+ public let id : String
367
+ public var content : String
368
+ public var author : String ?
369
+ public init ( id: String = UUID ( ) . uuidString,
370
+ content: String ,
371
+ author: String ? ) {
372
+ self . id = id
373
+ self . content = content
374
+ self . author = author
375
+ }
376
+ public enum CodingKeys : String , ModelKey {
377
+ case id
378
+ case content
379
+ case author
380
+ }
381
+ public static let keys = CodingKeys . self
382
+
383
+ public static let schema = defineSchema { model in
384
+ let modelWithOwnerField = ModelWithOwnerField . keys
385
+ model. authRules = [
386
+ rule ( allow: . owner, ownerField: " author " , provider: . oidc)
387
+ ]
388
+ model. fields (
389
+ . id( ) ,
390
+ . field( modelWithOwnerField. content, is: . required, ofType: . string) ,
391
+ . field( modelWithOwnerField. author, is: . optional, ofType: . string) )
392
+ }
393
+ }
394
+
395
+ /*
396
+ Example of model with multiple authorization rules,
397
+ and one of them doesn't require an `owner`.
398
+
399
+ type ModelWithOwnerField
400
+ @model
401
+ @auth(rules: [
402
+ { allow: owner, ownerField: "author" },
403
+ { allow: public, provider: "apiKey" }
404
+ ])
405
+ {
406
+ id: ID!
407
+ content: String!
408
+ author: String
409
+ }
410
+ */
411
+ public struct ModelWithMultipleAuthRules : Model {
412
+ public let id : String
413
+ public var content : String
414
+ public var author : String ?
415
+ public init ( id: String = UUID ( ) . uuidString,
416
+ content: String ,
417
+ author: String ? ) {
418
+ self . id = id
419
+ self . content = content
420
+ self . author = author
421
+ }
422
+ public enum CodingKeys : String , ModelKey {
423
+ case id
424
+ case content
425
+ case author
426
+ }
427
+ public static let keys = CodingKeys . self
428
+
429
+ public static let schema = defineSchema { model in
430
+ let modelWithMultipleAuthRules = ModelWithMultipleAuthRules . keys
431
+ model. authRules = [
432
+ rule ( allow: . owner, ownerField: " author " , provider: . userPools) ,
433
+ rule ( allow: . public, provider: . apiKey)
434
+ ]
435
+ model. fields (
436
+ . id( ) ,
437
+ . field( modelWithMultipleAuthRules. content, is: . required, ofType: . string) ,
438
+ . field( modelWithMultipleAuthRules. author, is: . optional, ofType: . string) )
439
+ }
289
440
}
0 commit comments