Skip to content

Commit

Permalink
test(api): gen2 integ tests (#3680)
Browse files Browse the repository at this point in the history
  • Loading branch information
lawmicha authored May 9, 2024
1 parent f4d2466 commit d888124
Show file tree
Hide file tree
Showing 20 changed files with 2,005 additions and 446 deletions.
460 changes: 194 additions & 266 deletions AmplifyPlugins/API/Tests/APIHostApp/APIHostApp.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,17 @@
ReferencedContainer = "container:APIHostApp.xcodeproj">
</BuildableReference>
</TestableReference>
<TestableReference
skipped = "NO"
parallelizable = "YES">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "2163D60B2BE96C90009689B1"
BuildableName = "AWSAPIPluginGen2GraphQLTests.xctest"
BlueprintName = "AWSAPIPluginGen2GraphQLTests"
ReferencedContainer = "container:APIHostApp.xcodeproj">
</BuildableReference>
</TestableReference>
</Testables>
</TestAction>
<LaunchAction
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,17 @@
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
shouldUseLaunchSchemeArgsEnv = "YES">
<TestPlans>
<TestPlanReference
reference = "container:AWSAPIPluginFunctionalTests/AWSAPIPluginGen2FunctionalTests.xctestplan"
default = "YES">
</TestPlanReference>
</TestPlans>
shouldUseLaunchSchemeArgsEnv = "YES"
shouldAutocreateTestPlan = "YES">
<Testables>
<TestableReference
skipped = "NO">
skipped = "NO"
parallelizable = "YES">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "21F7624D2BD6B0710048845A"
BuildableName = "AWSAPIPluginGen2FunctionalTests.xctest"
BlueprintName = "AWSAPIPluginGen2FunctionalTests"
BlueprintIdentifier = "2163D60B2BE96C90009689B1"
BuildableName = "AWSAPIPluginGen2GraphQLTests.xctest"
BlueprintName = "AWSAPIPluginGen2GraphQLTests"
ReferencedContainer = "container:APIHostApp.xcodeproj">
</BuildableReference>
</TestableReference>
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,16 @@
//

import Foundation
@_spi(InternalAmplifyConfiguration) @testable import Amplify
@testable import Amplify

class TestConfigHelper {

static var useGen2Configuration: Bool {
ProcessInfo.processInfo.arguments.contains("GEN2")
}

static func retrieveAmplifyConfiguration(forResource: String) throws -> AmplifyConfiguration {

let data = try retrieve(forResource: forResource)
return try AmplifyConfiguration.decodeAmplifyConfiguration(from: data)
}

static func retrieveAmplifyOutputsData(forResource: String) throws -> AmplifyOutputsData {
let data = try retrieve(forResource: forResource)
return try AmplifyOutputsData.decodeAmplifyOutputsData(from: data)
}

static func retrieveCredentials(forResource: String) throws -> [String: String] {
let data = try retrieve(forResource: forResource)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import XCTest
class GraphQLModelBasedTests: XCTestCase {

static let amplifyConfiguration = "testconfiguration/GraphQLModelBasedTests-amplifyconfiguration"
static let amplifyOutputs = "testconfiguration/GraphQLModelBasedTests-amplify_outputs"

final public class PostCommentModelRegistration: AmplifyModelRegistration {
public func registerModels(registry: ModelRegistry.Type) {
Expand All @@ -37,16 +36,10 @@ class GraphQLModelBasedTests: XCTestCase {

do {
try Amplify.add(plugin: plugin)

if TestConfigHelper.useGen2Configuration {
let amplifyConfig = try TestConfigHelper.retrieveAmplifyOutputsData(
forResource: GraphQLModelBasedTests.amplifyOutputs)
try Amplify.configure(amplifyConfig)
} else {
let amplifyConfig = try TestConfigHelper.retrieveAmplifyConfiguration(
forResource: GraphQLModelBasedTests.amplifyConfiguration)
try Amplify.configure(amplifyConfig)
}
let amplifyConfig = try TestConfigHelper.retrieveAmplifyConfiguration(
forResource: GraphQLModelBasedTests.amplifyConfiguration)
try Amplify.configure(amplifyConfig)

ModelRegistry.register(modelType: Comment.self)
ModelRegistry.register(modelType: Post.self)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -250,112 +250,3 @@ cp amplifyconfiguration.json ~/.aws-amplify/amplify-ios/testconfiguration/GraphQ
```
You can now run the tests!


## Schema: AWSAPIPluginGen2FunctionalTests

The following steps demonstrate how to set up an GraphQL endpoint with AppSync using Amplify CLI (Gen2). The auth configured will be API Key.

### Set-up

At the time this was written, it follows the steps from here https://docs.amplify.aws/gen2/deploy-and-host/fullstack-branching/mono-and-multi-repos/

1. From a new folder, run `npm create amplify@beta`. This uses the following versions of the Amplify CLI, see `package.json` file below.

```json
{
...
"devDependencies": {
"@aws-amplify/backend": "^0.13.0-beta.14",
"@aws-amplify/backend-cli": "^0.12.0-beta.16",
"aws-cdk": "^2.134.0",
"aws-cdk-lib": "^2.134.0",
"constructs": "^10.3.0",
"esbuild": "^0.20.2",
"tsx": "^4.7.1",
"typescript": "^5.4.3"
},
"dependencies": {
"aws-amplify": "^6.0.25"
}
}

```
2. Update `amplify/data/resource.ts` to allow `public` access. This allows using API Key as the auth type to perform CRUD operations against the Comment and Post models. The resulting file should look like this

```ts
const schema = a.schema({
Post: a
.model({
title: a.string().required(),
content: a.string().required(),
draft: a.boolean(),
rating: a.float(),
status: a.enum(["PRIVATE", "DRAFT", "PUBLISHED"]),
comments: a.hasMany('Comment')
})
.authorization([a.allow.public()]),
Comment: a
.model({
content: a.string().required(),
post: a.belongsTo('Post'),
})
.authorization([a.allow.public()]),
});
```

3. (Optional) Update the API Key expiry to the maximum. This should be done if this backend is used for CI testing.

```
export const data = defineData({
schema,
authorizationModes: {
defaultAuthorizationMode: 'apiKey',
// API Key is used for a.allow.public() rules
apiKeyAuthorizationMode: {
expiresInDays: 365,
},
},
});
```

4. Deploy the backend with npx amplify sandbox

For example, this deploys to a sandbox env and generates the amplify_outputs.json file.

```
npx amplify sandbox --config-out-dir ./config --config-version 1 --profile [PROFILE]
```

5. Copy the `amplify_outputs.json` file over to the test directory as `GraphQLModelBasedTests-amplify_outputs.json`. The tests will automatically pick this file up. Create the directories in this path first if it currently doesn't exist.

```
cp amplify_outputs.json ~/.aws-amplify/amplify-ios/testconfiguration/GraphQLModelBasedTests-amplify_outputs.json
```

6. (Optional) The code generated model files are already checked into the tests so you will only have to re-generate them if you are expecting modifications to them and replace the existing ones checked in.

```
npx amplify generate graphql-client-code --format=modelgen --model-target=swift --branch main --app-id [APP_ID] --profile [AWS_PROFILE]
```

### Deploying from a branch (Optional)

If you want to be able utilize Git commits for deployments

1. Commit and push the files to a git repository.

2. Navigate to the AWS Amplify console (https://us-east-1.console.aws.amazon.com/amplify/home?region=us-east-1#/)

3. Click on "Try Amplify Gen 2" button.

4. Choose "Option 2: Start with an existing app", and choose Github, and press Next.

5. Find the repository and branch, and click Next

6. Click "Save and deploy" and wait for deployment to finish.

7. Generate the `amplify_outputs.json` configuration file

```
npx amplify generate config --branch main --app-id [APP_ID] --profile [AWS_PROFILE] --config-version 1
```
Loading

0 comments on commit d888124

Please sign in to comment.