Skip to content

Commit

Permalink
chore: adding apikey+owner auth model to verify that subscriptions wh…
Browse files Browse the repository at this point in the history
…ere owner is undefined work
  • Loading branch information
alharris-at committed Sep 30, 2022
1 parent c0d92c7 commit a8d1550
Show file tree
Hide file tree
Showing 14 changed files with 326 additions and 150 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/// <reference types='cypress' />


const uuid = () => Cypress._.random(0, 1e6)

const TEST_ID = `${uuid()}`;
const PAGE_ROUTE = 'http://localhost:3000/auth-modes';
const SUCCESS_MARK = '✅';
const FAILURE_MARK = '❌';
const PAGE_TITLE = 'Multiauth Controls';

/**
* It's not great practice, but these tests rely on being in-order for now.
*/
describe('auth-mode interactions', () => {
before(() => {
cy.visit(PAGE_ROUTE)
});

describe('page state is stable', () => {
it('loads', () => {
cy.contains(PAGE_TITLE);
})

/**
* Non-owner based subscriptions work for models which also have owner-based auth attached.
*/
it('initializes subscriptions when owner auth is not selected, despite existing on the model', () => {
cy.get('#subscription-state').contains(SUCCESS_MARK);
})
});

describe('simple create model emits an event', () => {
it('creates a record and sees the result in the observable', () => {
cy.get('#MultiAuth-id-input').clear().type(TEST_ID);
cy.get('#MultiAuth-create').click();
cy.get('#MultiAuth-is-created').within(() => { cy.contains(SUCCESS_MARK) });
cy.get('#created-MultiAuths-subscription').within(() => { cy.contains(TEST_ID) });
});
});
});
2 changes: 2 additions & 0 deletions client-test-apps/js/api-model-relationship-app/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
Todos,
Blogs,
Listings,
AuthModes,
} from './pages';

Amplify.configure(awsconfig);
Expand All @@ -26,6 +27,7 @@ const App = () => {
<Route path="todos" element={<Todos />} />
<Route path="blogs" element={<Blogs />} />
<Route path="listings" element={<Listings />} />
<Route path="auth-modes" element={<AuthModes />} />
</Route>
</Routes>
</BrowserRouter>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ export const NavBar = () => {
<Flex direction='row'>
<Link to="/todos">Todos</Link> |{" "}
<Link to="/blogs">Blogs</Link> |{" "}
<Link to="/listings">Listings</Link>
<Link to="/listings">Listings</Link> |{" "}
<Link to="/auth-modes">Auth Modes</Link>
</Flex>
);
};
Original file line number Diff line number Diff line change
Expand Up @@ -3,70 +3,78 @@ import { AmplifyCLI } from './utils/amplifyCLI';
import { executeAmplifyTestHarness } from './utils/testHarness';

const PROJECT_ROOT = path.join(__dirname, '..', '..');
const envName = 'devtest';
const projName = 'simplemodel';
const schemaText = `
# Standalone Model
# Non-Model Types
type Todo @model @auth(rules: [{ provider: apiKey, allow: public }]) {
id: ID!
content: String
metadata: TodoMetadata
}
executeAmplifyTestHarness('simple test', PROJECT_ROOT, async (cli: AmplifyCLI) => {
const envName = 'devtest';
const projName = 'simplemodel';
const schemaText = `
# Standalone Model
# Non-Model Types
type Todo @model @auth(rules: [{ allow: public }]) {
id: ID!
content: String
metadata: TodoMetadata
}
type TodoMetadata {
targetCompletionDate: AWSDate
percentChanceOfCompletion: Float
}
# HasMany Relationship
# BelongsTo Relationship
# Secondary Index
type Blog @model @auth(rules: [{ provider: apiKey, allow: public }]) {
id: ID!
title: String!
author: String! @index(name: "byAuthor", queryField: "blogByAuthor")
posts: [Post] @hasMany
}
type TodoMetadata {
targetCompletionDate: AWSDate
percentChanceOfCompletion: Float
}
type Post @model @auth(rules: [{ provider: apiKey, allow: public }]) {
id: ID!
title: String!
content: String!
blog: Blog @belongsTo
}
# HasMany Relationship
# BelongsTo Relationship
# Secondary Index
type Blog @model @auth(rules: [{ allow: public }]) {
id: ID!
title: String!
author: String! @index(name: "byAuthor", queryField: "blogByAuthor")
posts: [Post] @hasMany
}
type Post @model @auth(rules: [{ allow: public }]) {
id: ID!
title: String!
content: String!
blog: Blog @belongsTo
}
# ManyToMany Relationship
# Enum Values
type Listing @model @auth(rules: [{ provider: apiKey, allow: public }]) {
id: ID!
title: String!
bedroomCount: Int
bathroomCount: Int
listPriceUSD: Float
state: ListingState
isHotProperty: Boolean
tags: [Tag] @manyToMany(relationName: "ListingTags")
}
# ManyToMany Relationship
# Enum Values
type Listing @model {
id: ID!
title: String!
bedroomCount: Int
bathroomCount: Int
listPriceUSD: Float
state: ListingState
isHotProperty: Boolean
tags: [Tag] @manyToMany(relationName: "ListingTags")
}
enum ListingState {
OPEN
UNDER_REVIEW
SOLD
}
enum ListingState {
OPEN
UNDER_REVIEW
SOLD
}
type Tag @model {
id: ID!
label: String!
listings: [Listing] @manyToMany(relationName: "ListingTags")
}
`;
type Tag @model @auth(rules: [{ provider: apiKey, allow: public }]) {
id: ID!
label: String!
listings: [Listing] @manyToMany(relationName: "ListingTags")
}
type MultiAuth @model @auth(rules: [
{ provider: apiKey, allow: public },
{ provider: userPools, allow: owner }
]) {
id: ID!
content: String @default(value: "Default Content")
}
`;

executeAmplifyTestHarness('simple test', PROJECT_ROOT, async (cli: AmplifyCLI) => {
await cli.initializeProject({ name: projName, envName });
await cli.addApiWithoutSchema();
await cli.updateSchema(projName, schemaText);
await cli.addAuth();
await cli.push();
await cli.codegen({ statementDepth: 3 });
});
Original file line number Diff line number Diff line change
Expand Up @@ -386,4 +386,16 @@ export class AmplifyCLI {
.wait('Code generated successfully')
.runAsync();
}

addAuth (): Promise<void> {
console.log('Executing Add Auth');
return spawn(getCLIPath(), ['add', 'auth'], { cwd: this.projectRoot, noOutputTimeout: pushTimeoutMS })
.wait('Do you want to use the default authentication and security configuration?')
.sendCarriageReturn()
.wait('How do you want users to be able to sign in?')
.sendCarriageReturn()
.wait('Do you want to configure advanced settings?')
.sendCarriageReturn()
.runAsync();
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ export const executeAmplifyTestHarness = (testName: string, projectRoot: string,
*/
afterAll(async () => {
if (getTestExecutionStages().has(TestExecutionStage.TEARDOWN)) {
try {
try {
await cli.delete();
cleanupJSGeneratedFiles(projectRoot);
} catch (e) {}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { RadioGroupField, Radio } from "@aws-amplify/ui-react";
import { useState, SetStateAction, useEffect } from "react";
import { AuthMode } from "./HarnessContext";

type OverrideAuthMode = AuthMode | 'unset';

type AuthModePickerProps = {
initialAuthMode?: OverrideAuthMode;
onAuthModeUpdates: (updatedAuthMode?: AuthMode) => void;
};

export const AuthModePicker = ({ initialAuthMode, onAuthModeUpdates }: AuthModePickerProps) => {
const [overrideAuthMode, setOverrideAuthMode] = useState<OverrideAuthMode>(initialAuthMode ?? 'unset');

useEffect(() => {
onAuthModeUpdates(overrideAuthMode === 'unset' ? undefined : overrideAuthMode);
}, [overrideAuthMode, onAuthModeUpdates]);

return (
<RadioGroupField
label="Auth Type Override"
name="authTypeOverride"
value={overrideAuthMode}
onChange={(e) => setOverrideAuthMode(e.target.value as unknown as SetStateAction<OverrideAuthMode>)}
>
<Radio value='unset'>Not Set</Radio>
<Radio value='API_KEY'>API Key</Radio>
<Radio value='AMAZON_COGNITO_USER_POOLS'>User Pool</Radio>
</RadioGroupField>
);
};
Loading

0 comments on commit a8d1550

Please sign in to comment.