Skip to content

Commit

Permalink
feat(ums): pass extra fields to sign-up
Browse files Browse the repository at this point in the history
  • Loading branch information
KutsenkoA committed Feb 13, 2020
1 parent 508af5d commit 2b9aca5
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 3 deletions.
31 changes: 31 additions & 0 deletions e2e/stories/ums/ums.e2e.ts
Expand Up @@ -42,6 +42,37 @@ describe("User Management Service", () => {
it("should have the same email", () => {
expect(user.email).toEqual(credentials.email);
});

describe("additional fields", () => {
let creds;
let extra: any;
let createdUser: any;

it("should create a user with extra fields", async () => {
creds = {
email: faker.internet.email(),
password: faker.internet.password(),
};
extra = {
bool: faker.random.boolean(),
num: faker.random.number(),
str: faker.random.alphaNumeric(),
};

createdUser = await ums.signUp(creds, extra);

joiAssert(createdUser, UserSchema.append({
bool: Joi.boolean().required(),
num: Joi.number().required(),
str: Joi.string().required()
}));
});

it("should return the same values of extra fields", () => {
const { bool, num, str } = createdUser;
expect({ bool, num, str }).toEqual(extra);
});
});
});

describe("when created user sign-in", () => {
Expand Down
32 changes: 31 additions & 1 deletion extra_docs/ums/README.md
Expand Up @@ -83,7 +83,8 @@ You can sign up a new user to the project.
```javascript
const user = await ums.signUp({
email: "robert@company.com",
password: "qwert1234" });
password: "qwert1234"
});

/* returned user:
{
Expand All @@ -95,6 +96,35 @@ password: "qwert1234" });
} */
```

You can also pass an extra fields during sing-up. These fields will be saved in the UMS and you can receive them by
"getUser()" method:
```javascript
const user = await ums.signUp(
{ email: "john@company.com", password: "rewq4321" },
{
age: 25,
address: {
city: "Apeldoorn",
country: "NL"
}
}
);

/* returned user:
{
"active": true,
"address": {
"city": "Apeldoorn",
"country": "NL"
},
"age": 25,
"created_at": "2020-02-12T11:09:36.017824Z",
"email": "john@company.com",
"id": "736afb7d-fbc2-4ef2-9ffe-bbdc454e68a3",
"updated_at": "2020-02-12T11:09:36.017824Z"
} */
```

### Fetch user by alias alias (or email)
```javascript
const user = await ums.getUser('Elon Musk');
Expand Down
18 changes: 18 additions & 0 deletions src/api/ums/umsModule.spec.ts
Expand Up @@ -141,6 +141,24 @@ describe("UMS Module", () => {
}
);
});

it("should call correct API with correct data if there is extra field", async () => {
const { subject, systemDefer, requestAdapterMock, init } = createSubject();
systemDefer.resolve();
await init();
await subject.signUp(testCredentials, { extraField: true });
expect(requestAdapterMock.execute).toBeCalledWith(
`${API.PROTOCOL}://${projectID}.${API.HOST}.${API.DOMAIN}:${API.PORT}/${API.UMS.ENDPOINT}/${API.UMS.SIGNUP}`,
{
body: {
email: testCredentials.email,
password: testCredentials.password,
extraField: true,
},
method: "POST"
}
);
});
});

describe("user sign-in", () => {
Expand Down
8 changes: 6 additions & 2 deletions src/api/ums/umsModule.ts
Expand Up @@ -18,6 +18,8 @@ export interface IUMSSignInOptions {

export type IUMSCredentials = Pick<IUMSSignInOptions, "email" | "password">;

export type IUMSExtraFields = Omit<{ [key: string]: any }, "email" | "password">;

/**
* Default UMS interface type
*/
Expand Down Expand Up @@ -96,11 +98,13 @@ export class UMSModule<
/**
* Create a new UMS user
* @param credentials {IUMSCredentials} email and password of created user
* @param extra {IUMSExtraFields} optional list of the additional user fields
*/
public signUp(credentials: IUMSCredentials): Promise<D> {
public signUp(credentials: IUMSCredentials, extra: IUMSExtraFields = {}): Promise<D> {
const body = {
email: credentials.email,
password: credentials.password
password: credentials.password,
...extra
};
return this.requestAdapter.execute<D>(
this.getUrl(API.UMS.SIGNUP),
Expand Down

0 comments on commit 2b9aca5

Please sign in to comment.