Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add full name to profile api #28

Merged
merged 3 commits into from
Aug 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
258 changes: 124 additions & 134 deletions src/resolvers/userResolver.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -355,8 +355,7 @@ function updateUserTestCases() {
const user = await saveUserDirectlyToDb(generateRandomEtheriumAddress());
const accessToken = await generateTestAccessToken(user.id);
const updateUserData = {
firstName: 'firstName',
lastName: 'lastName',
fullName: 'firstName lastName',
email: '[email protected]',
avatar: 'pinata address',
url: 'website url',
Expand All @@ -379,22 +378,18 @@ function updateUserTestCases() {
id: user.id,
},
});
assert.equal(updatedUser?.firstName, updateUserData.firstName);
assert.equal(updatedUser?.lastName, updateUserData.lastName);
assert.equal(updatedUser?.firstName, updateUserData.fullName.split(' ')[0]);
assert.equal(updatedUser?.lastName, updateUserData.fullName.split(' ')[1]);
assert.equal(updatedUser?.email, updateUserData.email);
assert.equal(updatedUser?.avatar, updateUserData.avatar);
assert.equal(updatedUser?.url, updateUserData.url);
assert.equal(
updatedUser?.name,
updateUserData.firstName + ' ' + updateUserData.lastName,
);
assert.equal(updatedUser?.name, updateUserData.fullName);
});
it('should update user with sending all data and then call userByAddress query', async () => {
const user = await saveUserDirectlyToDb(generateRandomEtheriumAddress());
const accessToken = await generateTestAccessToken(user.id);
const updateUserData = {
firstName: 'firstName',
lastName: 'lastName',
fullName: 'firstName lastName',
email: '[email protected]',
avatar: 'pinata address',
url: 'website url',
Expand All @@ -417,18 +412,15 @@ function updateUserTestCases() {
id: user.id,
},
});
assert.equal(updatedUser?.firstName, updateUserData.firstName);
assert.equal(updatedUser?.lastName, updateUserData.lastName);
assert.equal(updatedUser?.firstName, updateUserData.fullName.split(' ')[0]);
assert.equal(updatedUser?.lastName, updateUserData.fullName.split(' ')[1]);
assert.equal(updatedUser?.email, updateUserData.email);
assert.equal(updatedUser?.avatar, updateUserData.avatar);
assert.equal(updatedUser?.url, updateUserData.url);
assert.equal(
updatedUser?.name,
updateUserData.firstName + ' ' + updateUserData.lastName,
);
assert.equal(updatedUser?.name, updateUserData.fullName);
});

it('should fail when dont sending firstName and lastName', async () => {
it('should fail when dont sending fullName', async () => {
const user = await saveUserDirectlyToDb(generateRandomEtheriumAddress());
const accessToken = await generateTestAccessToken(user.id);
const updateUserData = {
Expand All @@ -451,14 +443,14 @@ function updateUserTestCases() {

assert.equal(
result.data.errors[0].message,
errorMessages.BOTH_FIRST_NAME_AND_LAST_NAME_CANT_BE_EMPTY,
errorMessages.FULL_NAME_CAN_NOT_BE_EMPTY,
);
});
it('should fail when email is invalid', async () => {
const user = await saveUserDirectlyToDb(generateRandomEtheriumAddress());
const accessToken = await generateTestAccessToken(user.id);
const updateUserData = {
firstName: 'firstName',
fullName: 'fullName',
email: 'giveth',
avatar: 'pinata address',
url: 'website url',
Expand All @@ -482,7 +474,7 @@ function updateUserTestCases() {
const user = await saveUserDirectlyToDb(generateRandomEtheriumAddress());
const accessToken = await generateTestAccessToken(user.id);
const updateUserData = {
firstName: 'firstName',
fullName: 'fullName',
email: 'giveth @ giveth.com',
avatar: 'pinata address',
url: 'website url',
Expand All @@ -502,40 +494,11 @@ function updateUserTestCases() {

assert.equal(result.data.errors[0].message, errorMessages.INVALID_EMAIL);
});
it('should fail when sending empty string for firstName', async () => {
const user = await saveUserDirectlyToDb(generateRandomEtheriumAddress());
const accessToken = await generateTestAccessToken(user.id);
const updateUserData = {
firstName: '',
lastName: 'test lastName',
email: 'giveth @ giveth.com',
avatar: 'pinata address',
url: 'website url',
};
const result = await axios.post(
graphqlUrl,
{
query: updateUser,
variables: updateUserData,
},
{
headers: {
Authorization: `Bearer ${accessToken}`,
},
},
);

assert.equal(
result.data.errors[0].message,
errorMessages.FIRSTNAME_CANT_BE_EMPTY_STRING,
);
});
it('should fail when sending empty string for lastName', async () => {
it('should fail when sending empty string for fullName', async () => {
const user = await saveUserDirectlyToDb(generateRandomEtheriumAddress());
const accessToken = await generateTestAccessToken(user.id);
const updateUserData = {
lastName: '',
firstName: 'firstName',
fullName: '',
email: 'giveth @ giveth.com',
avatar: 'pinata address',
url: 'website url',
Expand All @@ -555,94 +518,121 @@ function updateUserTestCases() {

assert.equal(
result.data.errors[0].message,
errorMessages.LASTNAME_CANT_BE_EMPTY_STRING,
errorMessages.FULL_NAME_CAN_NOT_BE_EMPTY,
);
});

it('should update user and name of user when sending just lastName', async () => {
const user = await saveUserDirectlyToDb(generateRandomEtheriumAddress());
const firstName = 'firstName';
user.firstName = firstName;
user.name = firstName;
await user.save();
const accessToken = await generateTestAccessToken(user.id);
const updateUserData = {
email: '[email protected]',
avatar: 'pinata address',
url: 'website url',
lastName: new Date().getTime().toString(),
};
const result = await axios.post(
graphqlUrl,
{
query: updateUser,
variables: updateUserData,
},
{
headers: {
Authorization: `Bearer ${accessToken}`,
},
},
);

assert.isTrue(result.data.data.updateUser);
const updatedUser = await User.findOne({
where: {
id: user.id,
},
});
assert.equal(updatedUser?.email, updateUserData.email);
assert.equal(updatedUser?.avatar, updateUserData.avatar);
assert.equal(updatedUser?.url, updateUserData.url);
assert.equal(updatedUser?.name, firstName + ' ' + updateUserData.lastName);
assert.equal(updatedUser?.firstName, firstName);
});

it('should update user and name of user when sending just firstName', async () => {
const user = await saveUserDirectlyToDb(generateRandomEtheriumAddress());
const lastName = 'lastName';
user.lastName = lastName;
user.name = lastName;
await user.save();
const accessToken = await generateTestAccessToken(user.id);
const updateUserData = {
email: '[email protected]',
avatar: 'pinata address',
url: 'website url',
firstName: new Date().getTime().toString(),
};
const result = await axios.post(
graphqlUrl,
{
query: updateUser,
variables: updateUserData,
},
{
headers: {
Authorization: `Bearer ${accessToken}`,
},
},
);

assert.isTrue(result.data.data.updateUser);
const updatedUser = await User.findOne({
where: {
id: user.id,
},
});
assert.equal(updatedUser?.email, updateUserData.email);
assert.equal(updatedUser?.avatar, updateUserData.avatar);
assert.equal(updatedUser?.url, updateUserData.url);
assert.equal(updatedUser?.name, updateUserData.firstName + ' ' + lastName);
assert.equal(updatedUser?.lastName, lastName);
});
// it('should fail when sending empty string for lastName', async () => {
// const user = await saveUserDirectlyToDb(generateRandomEtheriumAddress());
// const accessToken = await generateTestAccessToken(user.id);
// const updateUserData = {
// lastName: '',
// firstName: 'firstName',
// email: 'giveth @ giveth.com',
// avatar: 'pinata address',
// url: 'website url',
// };
// const result = await axios.post(
// graphqlUrl,
// {
// query: updateUser,
// variables: updateUserData,
// },
// {
// headers: {
// Authorization: `Bearer ${accessToken}`,
// },
// },
// );
//
// assert.equal(
// result.data.errors[0].message,
// errorMessages.LASTNAME_CANT_BE_EMPTY_STRING,
// );
// });
//
// it('should update user and name of user when sending just lastName', async () => {
// const user = await saveUserDirectlyToDb(generateRandomEtheriumAddress());
// const firstName = 'firstName';
// user.firstName = firstName;
// user.name = firstName;
// await user.save();
// const accessToken = await generateTestAccessToken(user.id);
// const updateUserData = {
// email: '[email protected]',
// avatar: 'pinata address',
// url: 'website url',
// lastName: new Date().getTime().toString(),
// };
// const result = await axios.post(
// graphqlUrl,
// {
// query: updateUser,
// variables: updateUserData,
// },
// {
// headers: {
// Authorization: `Bearer ${accessToken}`,
// },
// },
// );
//
// assert.isTrue(result.data.data.updateUser);
// const updatedUser = await User.findOne({
// where: {
// id: user.id,
// },
// });
// assert.equal(updatedUser?.email, updateUserData.email);
// assert.equal(updatedUser?.avatar, updateUserData.avatar);
// assert.equal(updatedUser?.url, updateUserData.url);
// assert.equal(updatedUser?.name, firstName + ' ' + updateUserData.lastName);
// assert.equal(updatedUser?.firstName, firstName);
// });
//
// it('should update user and name of user when sending just firstName', async () => {
// const user = await saveUserDirectlyToDb(generateRandomEtheriumAddress());
// const lastName = 'lastName';
// user.lastName = lastName;
// user.name = lastName;
// await user.save();
// const accessToken = await generateTestAccessToken(user.id);
// const updateUserData = {
// email: '[email protected]',
// avatar: 'pinata address',
// url: 'website url',
// firstName: new Date().getTime().toString(),
// };
// const result = await axios.post(
// graphqlUrl,
// {
// query: updateUser,
// variables: updateUserData,
// },
// {
// headers: {
// Authorization: `Bearer ${accessToken}`,
// },
// },
// );
//
// assert.isTrue(result.data.data.updateUser);
// const updatedUser = await User.findOne({
// where: {
// id: user.id,
// },
// });
// assert.equal(updatedUser?.email, updateUserData.email);
// assert.equal(updatedUser?.avatar, updateUserData.avatar);
// assert.equal(updatedUser?.url, updateUserData.url);
// assert.equal(updatedUser?.name, updateUserData.firstName + ' ' + lastName);
// assert.equal(updatedUser?.lastName, lastName);
// });

it('should accept empty string for all fields except email', async () => {
const user = await saveUserDirectlyToDb(generateRandomEtheriumAddress());
const accessToken = await generateTestAccessToken(user.id);
const updateUserData = {
firstName: 'test firstName',
lastName: 'test lastName',
fullName: 'firstName lastName',
avatar: '',
url: '',
};
Expand All @@ -664,8 +654,8 @@ function updateUserTestCases() {
id: user.id,
},
});
assert.equal(updatedUser?.firstName, updateUserData.firstName);
assert.equal(updatedUser?.lastName, updateUserData.lastName);
assert.equal(updatedUser?.firstName, updateUserData.fullName.split(' ')[0]);
assert.equal(updatedUser?.lastName, updateUserData.fullName.split(' ')[1]);
assert.equal(updatedUser?.avatar, updateUserData.avatar);
assert.equal(updatedUser?.url, updateUserData.url);
});
Expand Down
Loading
Loading