Skip to content

Commit dad09a7

Browse files
committed
user services test
1 parent bb5ebab commit dad09a7

File tree

5 files changed

+256
-55
lines changed

5 files changed

+256
-55
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
"homepage": "https://github.com/tocausan/express-api-ts#readme",
2323
"scripts": {
2424
"start": "node dist/bundle.js",
25-
"test": "mocha -r ts-node/register src/test/index.ts",
25+
"test": "mocha -r ts-node/register src/test/index.ts --exit",
2626
"watch:test": "mocha -r ts-node/register src/test/index.ts --watch",
2727
"build": "node node_modules/webpack/bin/webpack.js --config webpack.config.js",
2828
"postinstall": "node node_modules/typings/dist/bin.js install --overwrite",

src/models/User.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ export class User {
55
public id: string;
66
public username: string;
77
public email: string;
8-
public language: string;
8+
public language: number;
99
public role: number;
1010

1111
constructor(data?: any) {

src/services/user.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,9 @@ export const UserServices = {
3737
},
3838

3939
getUser: async (username: string): Promise<User> => {
40-
return new User((await DbClient.findOne(Config.database.collections.users, {username: username})));
40+
const result = await DbClient.findOne(Config.database.collections.users, {username: username});
41+
if (_.isNil(result)) throw new Error('user not found');
42+
return new User(result);
4143
},
4244

4345
updateUser: async (username: string, update: any): Promise<User> => {
@@ -47,9 +49,9 @@ export const UserServices = {
4749

4850
deleteUser: async (username: string): Promise<void> => {
4951
try {
50-
await DbClient.findOneAndDelete(Config.database.collections.users, {username: username})
5152
await UserServices.deletePassword(username);
5253
await UserServices.deleteToken(username);
54+
await DbClient.findOneAndDelete(Config.database.collections.users, {username: username});
5355
} catch (err) {
5456
throw err;
5557
}
@@ -61,7 +63,7 @@ export const UserServices = {
6163
userId: user.id,
6264
password: password
6365
});
64-
return await DbClient.insertOneIfNotExist(Config.database.collections.passwords, {userId: user.id}, psswrd);
66+
return await DbClient.findOneAndUpdateOrInsert(Config.database.collections.passwords, {userId: user.id}, psswrd);
6567
},
6668

6769
getPassword: async (username: string): Promise<Password> => {

src/test/services/auth.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ let authTestUser = {
55
username: 'authTestUser',
66
password: 'authTestUser',
77
passwordConfirmation: 'authTestUser',
8-
role: '3'
8+
role: 3
99
};
1010

1111
before(done => {
@@ -21,6 +21,10 @@ describe('auth', () => {
2121
.then(res => {
2222
result = res;
2323
done();
24+
})
25+
.catch(err => {
26+
console.log(err);
27+
done();
2428
});
2529
});
2630

@@ -30,7 +34,11 @@ describe('auth', () => {
3034
});
3135
});
3236

33-
after(after => {
37+
after(done => {
3438
UserServices.deleteUser(authTestUser.username)
35-
.then(() => after());
39+
.then(() => done())
40+
.catch(err => {
41+
console.log(err);
42+
done();
43+
});
3644
});

0 commit comments

Comments
 (0)