|
| 1 | +// |
| 2 | +// Copyright 2018-2020 Amazon.com, |
| 3 | +// Inc. or its affiliates. All Rights Reserved. |
| 4 | +// |
| 5 | +// SPDX-License-Identifier: Apache-2.0 |
| 6 | +// |
| 7 | + |
| 8 | +import XCTest |
| 9 | + |
| 10 | +@testable import Amplify |
| 11 | +@testable import AmplifyTestCommon |
| 12 | +@testable import AWSPluginsCore |
| 13 | + |
| 14 | +/* |
| 15 | + type OIDCGroupPost |
| 16 | + @model |
| 17 | + @auth( |
| 18 | + rules: [ |
| 19 | + { allow: owner, provider: oidc, identityClaim: "sub"}, |
| 20 | + { allow: groups, provider: oidc, groups: ["Admins"], |
| 21 | + groupClaim: "https://myapp.com/claims/groups"} |
| 22 | + ] |
| 23 | + ) { |
| 24 | + id: ID! |
| 25 | + title: String! |
| 26 | + owner: String |
| 27 | + } |
| 28 | + */ |
| 29 | + |
| 30 | +public struct OIDCGroupPost: Model { |
| 31 | + public let id: String |
| 32 | + public var title: String |
| 33 | + public var owner: String? |
| 34 | + |
| 35 | + public init(id: String = UUID().uuidString, |
| 36 | + title: String, |
| 37 | + owner: String? = nil) { |
| 38 | + self.id = id |
| 39 | + self.title = title |
| 40 | + self.owner = owner |
| 41 | + } |
| 42 | + |
| 43 | + // MARK: - CodingKeys |
| 44 | + public enum CodingKeys: String, ModelKey { |
| 45 | + case id |
| 46 | + case title |
| 47 | + case owner |
| 48 | + } |
| 49 | + |
| 50 | + public static let keys = CodingKeys.self |
| 51 | + public static let schema = defineSchema { model in |
| 52 | + let oIDCGroupPost = OIDCGroupPost.keys |
| 53 | + |
| 54 | + model.authRules = [ |
| 55 | + rule(allow: .owner, |
| 56 | + ownerField: "owner", |
| 57 | + identityClaim: "sub", |
| 58 | + operations: [.create, .update, .delete, .read]), |
| 59 | + rule(allow: .groups, |
| 60 | + groupClaim: "https://myapp.com/claims/groups", |
| 61 | + groups: ["Admins"], |
| 62 | + operations: [.create, .update, .delete, .read]) |
| 63 | + ] |
| 64 | + |
| 65 | + model.pluralName = "OIDCGroupPosts" |
| 66 | + |
| 67 | + model.fields( |
| 68 | + .id(), |
| 69 | + .field(oIDCGroupPost.title, is: .required, ofType: .string), |
| 70 | + .field(oIDCGroupPost.owner, is: .optional, ofType: .string) |
| 71 | + ) |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +class ModelWithOwnerAuthAndGroupWithGroupClaim: XCTestCase { |
| 76 | + override func setUp() { |
| 77 | + ModelRegistry.register(modelType: OIDCGroupPost.self) |
| 78 | + } |
| 79 | + |
| 80 | + override func tearDown() { |
| 81 | + ModelRegistry.reset() |
| 82 | + } |
| 83 | + |
| 84 | + func testOnCreateSubscription_NoGroupInfoPassed() { |
| 85 | + let claims = ["username": "user1", |
| 86 | + "sub": "123e4567-dead-beef-a456-426614174000"] as IdentityClaimsDictionary |
| 87 | + var documentBuilder = ModelBasedGraphQLDocumentBuilder(modelType: OIDCGroupPost.self, |
| 88 | + operationType: .subscription) |
| 89 | + documentBuilder.add(decorator: DirectiveNameDecorator(type: .onCreate)) |
| 90 | + documentBuilder.add(decorator: AuthRuleDecorator(.subscription(.onCreate, claims))) |
| 91 | + let document = documentBuilder.build() |
| 92 | + let expectedQueryDocument = """ |
| 93 | + subscription OnCreateOIDCGroupPost($owner: String!) { |
| 94 | + onCreateOIDCGroupPost(owner: $owner) { |
| 95 | + id |
| 96 | + owner |
| 97 | + title |
| 98 | + __typename |
| 99 | + } |
| 100 | + } |
| 101 | + """ |
| 102 | + XCTAssertEqual(document.name, "onCreateOIDCGroupPost") |
| 103 | + XCTAssertEqual(document.stringValue, expectedQueryDocument) |
| 104 | + guard let variables = document.variables else { |
| 105 | + XCTFail("The document doesn't contain variables") |
| 106 | + return |
| 107 | + } |
| 108 | + XCTAssertEqual(variables["owner"] as? String, "123e4567-dead-beef-a456-426614174000") |
| 109 | + } |
| 110 | + |
| 111 | + func testOnCreateSubscription_InAdminsGroup() { |
| 112 | + let claims = ["username": "user1", |
| 113 | + "sub": "123e4567-dead-beef-a456-426614174000", |
| 114 | + "https://myapp.com/claims/groups": ["Admins"]] as IdentityClaimsDictionary |
| 115 | + var documentBuilder = ModelBasedGraphQLDocumentBuilder(modelType: OIDCGroupPost.self, |
| 116 | + operationType: .subscription) |
| 117 | + documentBuilder.add(decorator: DirectiveNameDecorator(type: .onCreate)) |
| 118 | + documentBuilder.add(decorator: AuthRuleDecorator(.subscription(.onCreate, claims))) |
| 119 | + let document = documentBuilder.build() |
| 120 | + let expectedQueryDocument = """ |
| 121 | + subscription OnCreateOIDCGroupPost { |
| 122 | + onCreateOIDCGroupPost { |
| 123 | + id |
| 124 | + owner |
| 125 | + title |
| 126 | + __typename |
| 127 | + } |
| 128 | + } |
| 129 | + """ |
| 130 | + XCTAssertEqual(document.name, "onCreateOIDCGroupPost") |
| 131 | + XCTAssertEqual(document.stringValue, expectedQueryDocument) |
| 132 | + XCTAssert(document.variables.isEmpty) |
| 133 | + } |
| 134 | + |
| 135 | + func testOnCreateSubscription_InAdminsGroupAndAnother() { |
| 136 | + let claims = ["username": "user1", |
| 137 | + "sub": "123e4567-dead-beef-a456-426614174000", |
| 138 | + "https://myapp.com/claims/groups": ["Admins", "Users"]] as IdentityClaimsDictionary |
| 139 | + var documentBuilder = ModelBasedGraphQLDocumentBuilder(modelType: OIDCGroupPost.self, |
| 140 | + operationType: .subscription) |
| 141 | + documentBuilder.add(decorator: DirectiveNameDecorator(type: .onCreate)) |
| 142 | + documentBuilder.add(decorator: AuthRuleDecorator(.subscription(.onCreate, claims))) |
| 143 | + let document = documentBuilder.build() |
| 144 | + let expectedQueryDocument = """ |
| 145 | + subscription OnCreateOIDCGroupPost { |
| 146 | + onCreateOIDCGroupPost { |
| 147 | + id |
| 148 | + owner |
| 149 | + title |
| 150 | + __typename |
| 151 | + } |
| 152 | + } |
| 153 | + """ |
| 154 | + XCTAssertEqual(document.name, "onCreateOIDCGroupPost") |
| 155 | + XCTAssertEqual(document.stringValue, expectedQueryDocument) |
| 156 | + XCTAssert(document.variables.isEmpty) |
| 157 | + } |
| 158 | + |
| 159 | + func testOnCreateSubscription_NotInAdminsGroup() { |
| 160 | + let claims = ["username": "user1", |
| 161 | + "sub": "123e4567-dead-beef-a456-426614174000", |
| 162 | + "https://myapp.com/claims/groups": ["Users"]] as IdentityClaimsDictionary |
| 163 | + var documentBuilder = ModelBasedGraphQLDocumentBuilder(modelType: OIDCGroupPost.self, |
| 164 | + operationType: .subscription) |
| 165 | + documentBuilder.add(decorator: DirectiveNameDecorator(type: .onCreate)) |
| 166 | + documentBuilder.add(decorator: AuthRuleDecorator(.subscription(.onCreate, claims))) |
| 167 | + let document = documentBuilder.build() |
| 168 | + let expectedQueryDocument = """ |
| 169 | + subscription OnCreateOIDCGroupPost($owner: String!) { |
| 170 | + onCreateOIDCGroupPost(owner: $owner) { |
| 171 | + id |
| 172 | + owner |
| 173 | + title |
| 174 | + __typename |
| 175 | + } |
| 176 | + } |
| 177 | + """ |
| 178 | + XCTAssertEqual(document.name, "onCreateOIDCGroupPost") |
| 179 | + XCTAssertEqual(document.stringValue, expectedQueryDocument) |
| 180 | + guard let variables = document.variables else { |
| 181 | + XCTFail("The document doesn't contain variables") |
| 182 | + return |
| 183 | + } |
| 184 | + XCTAssertEqual(variables["owner"] as? String, "123e4567-dead-beef-a456-426614174000") |
| 185 | + } |
| 186 | +} |
0 commit comments