Skip to content
Snippets Groups Projects
Commit 6ef76938 authored by Jonny Ngo Luong's avatar Jonny Ngo Luong
Browse files

Merge branch '14-slette-bruker' into 'master'

Resolve "Slette bruker"

Closes #14

See merge request !23
parents dd4b41b3 f5b17a9f
No related branches found
No related tags found
1 merge request!23Resolve "Slette bruker"
<p>user-profile works!</p>
<p>Userid: {{user.getUserId}}</p>
<p>username: {{user.getUsername}}</p>
<p>email: {{user.getEmail}}</p>
\ No newline at end of file
<p>email: {{user.getEmail}}</p>
<div>
<app-button text="Slett bruker" (click)="deleteUser()"></app-button>
</div>
\ No newline at end of file
......@@ -3,14 +3,16 @@ import { ComponentFixture, TestBed } from '@angular/core/testing';
import { RouterTestingModule } from '@angular/router/testing';
import { AuthService } from 'src/app/authentication/auth.service';
import { User } from 'src/app/models/user.model';
import { SharedModule } from 'src/app/shared/shared.module';
import { UserLoginFormComponent } from '../user-login-form/user-login-form.component';
import { UserService } from '../user.service';
import { UserProfileComponent } from './user-profile.component';
describe('UserProfileComponent', () => {
let component: UserProfileComponent;
let fixture: ComponentFixture<UserProfileComponent>;
let mockAuthService;
let mockAuthService, mockUserService;
beforeEach(async () => {
// AuthService mock setup
......@@ -22,6 +24,15 @@ describe('UserProfileComponent', () => {
password: "1234",
create_time: 513498
}));
// UserService mock setup
mockUserService = jasmine.createSpyObj(['deleteUser']);
mockUserService.deleteUser.and.returnValue(
new Promise<any>(
(resolve) => {
resolve({data: []})
})
);
await TestBed.configureTestingModule({
......@@ -33,7 +44,8 @@ describe('UserProfileComponent', () => {
])
],
providers: [
{ provide: AuthService, useValue: mockAuthService }
{ provide: AuthService, useValue: mockAuthService },
{ provide: UserService, useValue: mockUserService }
]
})
.compileComponents();
......@@ -60,4 +72,11 @@ describe('UserProfileComponent', () => {
create_time: 513498
}));
});
it('should delete current user', async () => {
// Waits for ngOnInit and checks that we can delete the current user
await fixture.whenStable();
component.deleteUser();
expect(mockUserService.deleteUser).toHaveBeenCalledWith(4);
});
});
......@@ -13,9 +13,22 @@ import { UserService } from '../user.service';
export class UserProfileComponent implements OnInit {
user: User = new User();
constructor(private authService: AuthService, private router: Router) { }
constructor(private authService: AuthService, private userService: UserService, private router: Router) { }
ngOnInit(): void {
this.user = this.authService.getCurrentUser();
}
/**
* Deletes user in database and navigates to login
*/
deleteUser() {
this.userService.deleteUser(this.user.getUserId).then(data => {
console.log("Successfully deleted user: " + this.user.getUserId);
this.authService.logout();
this.router.navigateByUrl("/login");
}).catch(error => {
console.log(error);
});
}
}
......@@ -75,6 +75,35 @@ describe('UserService', () => {
req.error(new ErrorEvent("400"));
});
});
describe('deleteUser', () => {
it('should delete user', () => {
// Deletes user with id = 2
service.deleteUser(2)
.then(data => {})
.catch(error => {
fail();
});
// Mocks and checks HTTP request
const req = httpMock.expectOne("api/user/2");
expect(req.request.method).toBe("DELETE");
req.flush({
data: []
});
});
it('should reject on http error', () => {
// Deletes user with id = 2, but should catch HTTP error
service.deleteUser(2).then(data => {
fail();
}).catch(error => {});
// Mocks and checks HTTP request
const req = httpMock.expectOne("api/user/2");
expect(req.request.method).toBe("DELETE");
req.error(new ErrorEvent("400"));
});
});
});
......@@ -76,4 +76,28 @@ export class UserService {
private get_all_users() {
return this.http.get(this.userUrl);
}
}
/**
* Deletes an user from the database by id.
*/
deleteUser(id: number): Promise<User> {
return new Promise<User>(
(resolve, reject) => {
this.delete_user(id).subscribe((data: any) => {
try {
resolve(data);
} catch (err: any) {
reject(err);
}
},
(err: any) => {
console.log(err.message);
reject(err);
});
}
);
}
private delete_user(id: number) {
return this.http.delete(this.userUrl + id);
}
}
\ No newline at end of file
......@@ -61,7 +61,7 @@ router.route('/:userId').put(async (request: Request, response: Response) => {
/* ============================= DELETE ============================= */
// Delete user from id `/api/user/:id`
router.route('/:userId').delete(async (request: Request, response: Response) => {
router.route('/:userId').delete(authenticateToken, async (request: Request, response: Response) => {
const userId = request.params.userId;
try {
const input = `DELETE FROM user WHERE (userId=?);`;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment