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

Do not read user password from DB closes #232 #233

Merged
3 commits merged into from
Jun 2, 2021
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
13 changes: 11 additions & 2 deletions generators/server/templates/server/e2e/account.e2e-spec.ts.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@ import { RolesGuard } from '../src/security/guards/roles.guard';
import { UserDTO } from '../src/service/dto/user.dto';
import { UserService } from '../src/service/user.service';
import { PasswordChangeDTO } from '../src/service/dto/password-change.dto';
import { AuthService } from '../src/service/auth.service';

describe('Account', () => {
let app: INestApplication;
let service: UserService;
let authService: AuthService;

const testUserDTO: UserDTO = {
login: 'userTestLogin',
Expand All @@ -23,6 +25,7 @@ describe('Account', () => {
login: 'userlogged',
email: '[email protected]',
password: 'userloggedPassword',
activated: true
};

const testPasswordChange: PasswordChangeDTO = {
Expand Down Expand Up @@ -55,6 +58,7 @@ describe('Account', () => {
app = moduleFixture.createNestApplication();
await app.init();
service = moduleFixture.get<UserService>(UserService);
authService = moduleFixture.get<AuthService>(AuthService);
userAuthenticated = await service.save(testUserAuthenticated);
});

Expand Down Expand Up @@ -127,8 +131,13 @@ describe('Account', () => {
.send(testPasswordChange)
.expect(201);

const updatedUserPassword: UserDTO = await service.findByfields({ where: { login: testUserAuthenticated.login } });
expect(updatedUserPassword.password).toEqual(testPasswordChange.newPassword);
const successFullyLoggedInWithNewPassword = await authService.login(
{
username: testUserAuthenticated.login,
password: testPasswordChange.newPassword
}).then(() => true, () => false);

expect(successFullyLoggedInWithNewPassword).toEqual(true);
});

it('/POST reset password init', async () => {
Expand Down
4 changes: 3 additions & 1 deletion generators/server/templates/server/e2e/user.e2e-spec.ts.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ describe('User', () => {
it('/GET user with a login name', async () => {
testUserDTO.login = 'TestUserGet';
const savedUser: UserDTO = await service.save(testUserDTO);
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const { password, ...savedUserWithoutPassword } = savedUser;

const getUser: UserDTO = (
await request(app.getHttpServer())
Expand All @@ -94,7 +96,7 @@ describe('User', () => {
).body;

<%_ if (databaseType !== 'mongodb') { _%>
expect(getUser).toEqual(savedUser);
expect(getUser).toEqual(savedUserWithoutPassword);
<%_ } else { _%>
expect(getUser.login).toEqual(savedUser.login);
<%_ } _%>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ export class User extends BaseEntity {
algorithm: 'aes-256-cbc',
ivLength: 16,
iv: config.get('crypto.iv')
})
}),
select: false
})
password: string;
@Column({ nullable: true })
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ export class AuthService {
}
async changePassword(userLogin: string, currentClearTextPassword: string, newPassword: string): Promise<void> {
const userFind: UserDTO = await this.userService.findByfields({ where: { login: userLogin } });
const userFind: UserDTO = await this.userService.findByfields({ where: { login: userLogin }, select: [ 'id', 'password' ] });
if (!userFind) {
throw new HttpException('Invalid login name!', HttpStatus.BAD_REQUEST);
}
Expand Down