Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
1 change: 0 additions & 1 deletion frontend/src/app/components/class/class.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ import { AuthenticationService } from '../../services/authentication.service';
styleUrl: './class.component.less'
})
export class ClassComponent implements OnInit {

// The current activated route
private readonly route = inject(ActivatedRoute);

Expand Down
7 changes: 6 additions & 1 deletion frontend/src/app/interfaces/user/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,10 @@ export enum UserType {
}

export interface User {
name: string;
id: string;
email: string;
firstName: string;
familyName: string;
schoolName: string;
passwordHash: string;
}
48 changes: 48 additions & 0 deletions frontend/src/app/services/user.service.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { HttpClient } from "@angular/common/http";
import { AuthenticationService } from "./authentication.service";
import { ErrorService } from "./error.service";
import { of } from "rxjs";
import { User } from "../interfaces";
import { UserService } from "./user.service";

describe('UserService', () => {

let http: jasmine.SpyObj<HttpClient>;
let authService: jasmine.SpyObj<AuthenticationService>;
let errorService: jasmine.SpyObj<ErrorService>;
let service: UserService;

let fakeUser: User;

beforeEach(() => {
http = jasmine.createSpyObj('HttpClient', ['get']);
authService = jasmine.createSpyObj('AuthenticationService', ['retrieveUserId', 'retrieveToken']);
errorService = jasmine.createSpyObj('ErrorService', ['pipeHandler']);

// Mock the return values of the AuthenticationService methods
authService.retrieveUserId.and.returnValue("mockUserId");
authService.retrieveToken.and.returnValue("mockToken");
errorService.pipeHandler.and.callFake(() => (source) => source);

// Mock user
fakeUser = {
id: 'mockUserId',
email: 'mockEmail',
firstName: 'mockFirstName',
familyName: 'mockLastName',
schoolName: 'mockSchoolName',
passwordHash: 'mockPasswordHash',
};

service = new UserService(http, authService, errorService);
});

it('should call userWithId and return user data', () => {
http.get.and.returnValue(of(fakeUser));

service.userWithId('mockUserId').subscribe(user => {
expect(user).toEqual(fakeUser);
});
});

});
50 changes: 50 additions & 0 deletions frontend/src/app/services/user.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { Injectable } from "@angular/core";
import { HttpClient } from '@angular/common/http';
import { environment } from "../../environments/environment";
import { Observable } from "rxjs";
import { User } from "../interfaces";
import { AuthenticationService } from "./authentication.service";
import { ErrorService } from "./error.service";

@Injectable({
providedIn: 'root'
})
export class UserService {

private API_URL = environment.API_URL;
private userCreds;
private standardHeaders;

public constructor(
private http: HttpClient,
private authService: AuthenticationService,
private errorService: ErrorService
) {
this.userCreds = {
userId: this.authService.retrieveUserId(),
userToken: this.authService.retrieveToken()
};

this.standardHeaders = {
headers: {
"Content-Type": "application/json",
"Authorization": `Bearer ${this.userCreds.userToken}`
}
};
}

public userWithId(id: string): Observable<User> {
return this.http.get<User>(
`${this.API_URL}/users/${id}`, {
...this.standardHeaders,
params: {
'id': this.userCreds.userId || '',
'userType': this.authService.retrieveUserType()?.toString() || ''
}
}
).pipe(
this.errorService.pipeHandler()
);
}

}