-
-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(core): Verify admin-created Customers if password supplied
Relates to #171
- Loading branch information
1 parent
70e857d
commit 9931e25
Showing
5 changed files
with
128 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,18 @@ | ||
import { OnModuleInit } from '@nestjs/common'; | ||
import { omit } from '@vendure/common/lib/omit'; | ||
import gql from 'graphql-tag'; | ||
import path from 'path'; | ||
|
||
import { EventBus } from '../src/event-bus/event-bus'; | ||
import { EventBusModule } from '../src/event-bus/event-bus.module'; | ||
import { AccountRegistrationEvent } from '../src/event-bus/events/account-registration-event'; | ||
import { VendurePlugin } from '../src/plugin/vendure-plugin'; | ||
|
||
import { TEST_SETUP_TIMEOUT_MS } from './config/test-config'; | ||
import { CUSTOMER_FRAGMENT } from './graphql/fragments'; | ||
import { | ||
CreateAddress, | ||
CreateCustomer, | ||
DeleteCustomer, | ||
DeleteCustomerAddress, | ||
DeletionResult, | ||
|
@@ -23,6 +30,7 @@ import { TestServer } from './test-server'; | |
import { assertThrowsWithMessage } from './utils/assert-throws-with-message'; | ||
|
||
// tslint:disable:no-non-null-assertion | ||
let sendEmailFn: jest.Mock; | ||
|
||
describe('Customer resolver', () => { | ||
const adminClient = new TestAdminClient(); | ||
|
@@ -33,10 +41,15 @@ describe('Customer resolver', () => { | |
let thirdCustomer: GetCustomerList.Items; | ||
|
||
beforeAll(async () => { | ||
const token = await server.init({ | ||
productsCsvPath: path.join(__dirname, 'fixtures/e2e-products-minimal.csv'), | ||
customerCount: 5, | ||
}); | ||
const token = await server.init( | ||
{ | ||
productsCsvPath: path.join(__dirname, 'fixtures/e2e-products-minimal.csv'), | ||
customerCount: 5, | ||
}, | ||
{ | ||
plugins: [TestEmailPlugin], | ||
}, | ||
); | ||
await adminClient.init(); | ||
}, TEST_SETUP_TIMEOUT_MS); | ||
|
||
|
@@ -303,9 +316,51 @@ describe('Customer resolver', () => { | |
}); | ||
}); | ||
|
||
describe('creation', () => { | ||
it('triggers verification event if no password supplied', async () => { | ||
sendEmailFn = jest.fn(); | ||
const { createCustomer } = await adminClient.query< | ||
CreateCustomer.Mutation, | ||
CreateCustomer.Variables | ||
>(CREATE_CUSTOMER, { | ||
input: { | ||
emailAddress: '[email protected]', | ||
firstName: 'New', | ||
lastName: 'Customer', | ||
}, | ||
}); | ||
|
||
expect(createCustomer.user!.verified).toBe(false); | ||
expect(sendEmailFn).toHaveBeenCalledTimes(1); | ||
expect(sendEmailFn.mock.calls[0][0] instanceof AccountRegistrationEvent).toBe(true); | ||
expect(sendEmailFn.mock.calls[0][0].user.identifier).toBe('[email protected]'); | ||
}); | ||
|
||
it('creates a verified Customer', async () => { | ||
sendEmailFn = jest.fn(); | ||
const { createCustomer } = await adminClient.query< | ||
CreateCustomer.Mutation, | ||
CreateCustomer.Variables | ||
>(CREATE_CUSTOMER, { | ||
input: { | ||
emailAddress: '[email protected]', | ||
firstName: 'New', | ||
lastName: 'Customer', | ||
}, | ||
password: 'test', | ||
}); | ||
|
||
expect(createCustomer.user!.verified).toBe(true); | ||
expect(sendEmailFn).toHaveBeenCalledTimes(0); | ||
}); | ||
}); | ||
|
||
describe('deletion', () => { | ||
it('deletes a customer', async () => { | ||
const result = await adminClient.query<DeleteCustomer.Mutation, DeleteCustomer.Variables>(DELETE_CUSTOMER, { id: thirdCustomer.id }); | ||
const result = await adminClient.query<DeleteCustomer.Mutation, DeleteCustomer.Variables>( | ||
DELETE_CUSTOMER, | ||
{ id: thirdCustomer.id }, | ||
); | ||
|
||
expect(result.deleteCustomer).toEqual({ result: DeletionResult.DELETED }); | ||
}); | ||
|
@@ -406,6 +461,15 @@ const GET_CUSTOMER_ORDERS = gql` | |
} | ||
`; | ||
|
||
export const CREATE_CUSTOMER = gql` | ||
mutation CreateCustomer($input: CreateCustomerInput!, $password: String) { | ||
createCustomer(input: $input, password: $password) { | ||
...Customer | ||
} | ||
} | ||
${CUSTOMER_FRAGMENT} | ||
`; | ||
|
||
export const UPDATE_CUSTOMER = gql` | ||
mutation UpdateCustomer($input: UpdateCustomerInput!) { | ||
updateCustomer(input: $input) { | ||
|
@@ -422,3 +486,19 @@ const DELETE_CUSTOMER = gql` | |
} | ||
} | ||
`; | ||
|
||
/** | ||
* This mock plugin simulates an EmailPlugin which would send emails | ||
* on the registration & password reset events. | ||
*/ | ||
@VendurePlugin({ | ||
imports: [EventBusModule], | ||
}) | ||
class TestEmailPlugin implements OnModuleInit { | ||
constructor(private eventBus: EventBus) {} | ||
onModuleInit() { | ||
this.eventBus.ofType(AccountRegistrationEvent).subscribe(event => { | ||
sendEmailFn(event); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters