func testProtectedReadWithCachingSegregation()

in AWSAppSyncIntegrationTests/AWSAppSyncMultiAuthClientsTests.swift [130:233]


    func testProtectedReadWithCachingSegregation() throws {
        let testBundle = Bundle(for: AWSAppSyncCognitoAuthTests.self)
        
        // Create IAM based client
        let iamHelper = try AppSyncClientTestHelper(
            with: .cognitoIdentityPools,
            testBundle: testBundle
        )
        let iamAppSyncClient = iamHelper.appSyncClient
        
        // Create API Key based client
        let apiKeyHelper = try AppSyncClientTestHelper(
            with: .apiKeyWithIAMEndpoint,
            testBundle: testBundle
        )
        let apiKeyAppSyncClient = apiKeyHelper.appSyncClient
        
        let postCreated = expectation(description: "Post created successfully.")
        
        let addPost = DefaultTestPostData.defaultCreatePostWithoutFileUsingParametersMutation
        var createdId: GraphQLID?
        iamAppSyncClient.perform(mutation: addPost, queue: AWSAppSyncMultiAuthClientsTests.mutationQueue, resultHandler:  { result, error in
            XCTAssertNil(error)
            XCTAssertNotNil(result?.data?.createPostWithoutFileUsingParameters?.id)
            createdId = result!.data!.createPostWithoutFileUsingParameters!.id
            XCTAssertEqual(
                result!.data!.createPostWithoutFileUsingParameters?.author,
                DefaultTestPostData.author
            )
            postCreated.fulfill()
        })
        
        wait(for: [postCreated], timeout: AWSAppSyncMultiAuthClientsTests.networkOperationTimeout)

        let getPostQuery = GetPostQuery(id: createdId!)

        let apiKeyGetPost = expectation(description: "Query done successfully.")

        apiKeyAppSyncClient.fetch(query: getPostQuery, cachePolicy: .fetchIgnoringCacheData, queue: AWSAppSyncMultiAuthClientsTests.subscriptionAndFetchQueue) { result, error in
            XCTAssertNil(error)
            XCTAssertNotNil(result?.data)
            XCTAssertNil(result?.data?.getPost?.url)
            XCTAssert((result?.errors![0].message.contains("Not Authorized to access url on type Post"))!)
            apiKeyGetPost.fulfill()
        }

        wait(for: [apiKeyGetPost], timeout: AWSAppSyncMultiAuthClientsTests.networkOperationTimeout)

        let iamGetPost = expectation(description: "Query done successfully.")

        iamAppSyncClient.fetch(query: getPostQuery, cachePolicy: .fetchIgnoringCacheData, queue: AWSAppSyncMultiAuthClientsTests.subscriptionAndFetchQueue) { result, error in
            XCTAssertNil(error)
            XCTAssertNotNil(result?.data)
            XCTAssertNotNil(result?.data?.getPost?.url)
            iamGetPost.fulfill()
        }

        wait(for: [iamGetPost], timeout: AWSAppSyncMultiAuthClientsTests.networkOperationTimeout)

        let apiKeyGetPostCached = expectation(description: "Query done successfully.")

        apiKeyAppSyncClient.fetch(query: getPostQuery, cachePolicy: .returnCacheDataDontFetch, queue: AWSAppSyncMultiAuthClientsTests.subscriptionAndFetchQueue) { result, error in
            XCTAssertNil(error)
            XCTAssertNotNil(result?.data)
            XCTAssertNil(result?.data?.getPost?.url)
            apiKeyGetPostCached.fulfill()
        }

        wait(for: [apiKeyGetPostCached], timeout: AWSAppSyncMultiAuthClientsTests.networkOperationTimeout)

        let iamGetPostCached = expectation(description: "Query done successfully.")

        iamAppSyncClient.fetch(query: getPostQuery, cachePolicy: .returnCacheDataDontFetch, queue: AWSAppSyncMultiAuthClientsTests.subscriptionAndFetchQueue) { result, error in
            XCTAssertNil(error)
            XCTAssertNotNil(result?.data)
            XCTAssertNotNil(result?.data?.getPost?.url)
            iamGetPostCached.fulfill()
        }

        wait(for: [iamGetPostCached], timeout: AWSAppSyncMultiAuthClientsTests.networkOperationTimeout)

        try apiKeyAppSyncClient.clearCaches()

        let apiKeyGetPostCachedAfterApiKeyClear = expectation(description: "Query done successfully.")

        apiKeyAppSyncClient.fetch(query: getPostQuery, cachePolicy: .returnCacheDataDontFetch, queue: AWSAppSyncMultiAuthClientsTests.subscriptionAndFetchQueue) { result, error in
            XCTAssertNil(error)
            XCTAssertNil(result?.data)
            apiKeyGetPostCachedAfterApiKeyClear.fulfill()
        }

        wait(for: [apiKeyGetPostCachedAfterApiKeyClear], timeout: AWSAppSyncMultiAuthClientsTests.networkOperationTimeout)

        let iamGetPostCachedAfterApiKeyClear = expectation(description: "Query done successfully.")

        iamAppSyncClient.fetch(query: getPostQuery, cachePolicy: .returnCacheDataDontFetch, queue: AWSAppSyncMultiAuthClientsTests.subscriptionAndFetchQueue) { result, error in
            XCTAssertNil(error)
            XCTAssertNotNil(result?.data)
            XCTAssertNotNil(result?.data?.getPost?.url)
            iamGetPostCachedAfterApiKeyClear.fulfill()
        }

        wait(for: [iamGetPostCachedAfterApiKeyClear], timeout: AWSAppSyncMultiAuthClientsTests.networkOperationTimeout)
    }