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

Feauture#405 #417

Merged
merged 15 commits into from
Oct 7, 2024
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to
[Semantic Versioning](https://semver.org/spec/v2.0.0.html).

### [ita-challenges-frontend-3.1.1-RELEASE] - 2024-09-26 (feauture#405)

- Refactoring: huge mocks must be externalized from tests.

### [ita-challenges-frontend-3.1.0-RELEASE]

- Added pipe to escape java characters to json.
Expand Down
2 changes: 1 addition & 1 deletion conf/.env.CI.dev
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
MICROSERVICE_DEPLOY=ita-challenges-frontend
MICROSERVICE_VERSION=3.1.0-RELEASE
MICROSERVICE_VERSION=3.1.1-RELEASE
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ita-challenges-frontend",
"version": "3.1.0-RELEASE",
"version": "3.1.1-RELEASE",
"sideEffects": [
"node_modules/@angular/localize"
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import { NgbModal, NgbModule } from '@ng-bootstrap/ng-bootstrap'
import { of } from 'rxjs'
import { RegisterModalComponent } from './register-modal.component'
import { AuthService } from '../../../services/auth.service'
import { type User } from '../../../models/user.model'
import { TranslateModule, TranslateService } from '@ngx-translate/core'
import { ChallengeService } from 'src/app/services/challenge.service'
import testUser from 'src/mocks/user/testUser.mock.json'

describe('RegisterModalComponent', () => {
let component: RegisterModalComponent
Expand Down Expand Up @@ -66,16 +66,6 @@ describe('RegisterModalComponent', () => {
legalTermsAccepted: true
})

const testUser: User = {
idUser: '',
dni: '12345678Z',
email: 'test@example.com',
name: 'testName',
itineraryId: 'testItinerary',
password: 'TestPassword1',
confirmPassword: 'TestPassword1'
}

component.register()

tick()
Expand All @@ -101,16 +91,6 @@ describe('RegisterModalComponent', () => {
legalTermsAccepted: true
})

const testUser: User = {
idUser: '',
dni: '12345678Z',
email: 'test@example.com',
name: 'testName',
itineraryId: 'testItinerary',
password: 'TestPassword1',
confirmPassword: 'TestPassword1'
}

component.register()

tick()
Expand Down
126 changes: 28 additions & 98 deletions src/app/services/auth.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@ import { User } from '../models/user.model'
import { type TokenService } from './token.service'
import { type Router } from '@angular/router'
import { type CookieService } from 'ngx-cookie-service'
// import { mockLoginResponse, mockRegisterResponse, mockLoginErrorResponse, mockRegisterErrorResponse, mockUnauthorizedErrorResponse } from 'src/mocks/auth/auth.mock'
import mockLoginResponse from '../../mocks/auth/loginResponse.mock.json'
import mockRegisterResponse from '../../mocks/auth/registerResponse.mock.json'
import mockLoginErrorResponse from '../../mocks/auth/loginErrorResponse.json'
import mockRegisterErrorResponse from '../../mocks/auth/registerErrorResponse.mock.json'
import mockUnauthorizedErrorResponse from '../../mocks/auth/unauthorizedErrorResponse.mock.json'
import mockRegisterUser from 'src/mocks/user/mockRegisterUser.mock.json'

describe('AuthService', () => {
let authService: AuthService
Expand Down Expand Up @@ -142,24 +149,18 @@ describe('AuthService', () => {
password: testPassword
}

const mockResponse = {
authToken: 'testAuthToken',
refreshToken: 'testRefreshToken',
id: 'testId'
}

authService.loginRequest(testUser)
.subscribe({
next: (res) => {
expect(res).toBeTruthy()
expect(res).toEqual(mockResponse)
expect(res).toEqual(mockLoginResponse)
}
})

const req = httpClientMock.expectOne(environment.BACKEND_ITA_SSO_BASE_URL.concat(environment.BACKEND_SSO_LOGIN_URL))
expect(req.request.method).toEqual('POST')

req.flush(mockResponse)
req.flush(mockLoginResponse)
done()
})

Expand All @@ -171,22 +172,19 @@ describe('AuthService', () => {
password: testPassword
}

const mockResponse = {
message: 'Invalid Credentials'
}

authService.loginRequest(testUser)
.subscribe({
error: (err) => {
expect(err).toBeTruthy()
expect(err).toEqual(mockResponse)
expect(err).toEqual(mockLoginErrorResponse)
}
})

const req = httpClientMock.expectOne(environment.BACKEND_ITA_SSO_BASE_URL.concat(environment.BACKEND_SSO_LOGIN_URL))
expect(req.request.method).toEqual('POST')

req.flush(mockResponse)
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
req.flush(mockLoginErrorResponse)
done()
})

Expand All @@ -198,14 +196,8 @@ describe('AuthService', () => {
password: testPassword
}

const mockResponse = { // response we expect from the loginRequest function.
authToken: 'testAuthToken',
refreshToken: 'testRefreshToken',
id: 'testId'
}

// spyOn function to mock the behavior of the loginRequest function.
spyOn(authService, 'loginRequest').and.returnValue(of(mockResponse)) // Import 'of' from 'rxjs' if not already imported
spyOn(authService, 'loginRequest').and.returnValue(of(mockLoginResponse)) // Import 'of' from 'rxjs' if not already imported

try {
void authService.login(mockUser).then((returnValue) => {
Expand All @@ -227,18 +219,13 @@ describe('AuthService', () => {
password: testPassword
}

const mockErrorMessage = 'Invalid Credentials'
const mockErrorResponse = { // response we expect from the loginRequest function.
message: mockErrorMessage
}

spyOn(authService, 'loginRequest').and.returnValue(
of({}).pipe(
tap(() => {
const error = new Error('Unauthorized')
error.name = 'HttpError'
// Agrega propiedades personalizadas al objeto de error
Object.assign(error, { status: 401, error: mockErrorResponse })
Object.assign(error, { status: 401, error: mockUnauthorizedErrorResponse })
throw error
})
)
Expand All @@ -248,122 +235,65 @@ describe('AuthService', () => {
await authService.login(mockUser)
fail('Login should have failed')
} catch (error: any) {
expect(error.error.message).toEqual(mockErrorMessage)
expect(error.error.message).toEqual(mockUnauthorizedErrorResponse.message)
}
})

it('should register request successfully', (done) => {
const mockUser = {
idUser: '',
dni: 'mockUserDni',
email: 'mockUserEmail',
name: 'mockUserName',
itineraryId: 'mockUserIteneraryId',
password: 'mockUserPassword',
confirmPassword: 'mockUserConfirmPassword'
}

const mockResponse = {
id: 'mockIdResponse'
}

authService.registerRequest(mockUser)
authService.registerRequest(mockRegisterUser)
.subscribe({
next: (res) => {
expect(res).toBeTruthy()
expect(res).toEqual(mockResponse)
expect(res).toEqual(mockRegisterResponse)
}
})

const req = httpClientMock.expectOne(environment.BACKEND_ITA_SSO_BASE_URL.concat(environment.BACKEND_SSO_REGISTER_URL))
expect(req.request.method).toEqual('POST')

req.flush(mockResponse)
req.flush(mockRegisterResponse)
done()
})

it('should register request UNsuccessful', (done) => {
const mockUser = {
idUser: 'mockIdResponse',
dni: 'mockUserDni',
email: 'mockUserEmail',
name: 'mockUserName',
itineraryId: 'mockUserIteneraryId',
password: 'mockUserPassword',
confirmPassword: 'mockUserConfirmPassword'
}

const mockResponse = {
id: 'mockIdResponse'
}

authService.loginRequest(mockUser)
authService.loginRequest(mockRegisterUser)
.subscribe({
error: (err) => {
expect(err).toBeTruthy()
expect(err).toEqual(mockResponse)
expect(err).toEqual(mockRegisterResponse)
}
})

const req = httpClientMock.expectOne(environment.BACKEND_ITA_SSO_BASE_URL.concat(environment.BACKEND_SSO_LOGIN_URL))
expect(req.request.method).toEqual('POST')

req.flush(mockResponse)
req.flush(mockRegisterResponse)
done()
})

it('should show success register modal', (done) => {
const mockUser = {
idUser: 'mockIdResponse',
dni: 'mockUserDni',
email: 'mockUserEmail',
name: 'mockUserName',
itineraryId: 'mockUserIteneraryId',
password: 'mockUserPassword',
confirmPassword: 'mockUserConfirmPassword'
}

const mockResponse = {
id: 'mockIdResponse'
}

spyOn(authService, 'registerRequest').and.returnValue(of(mockResponse))
spyOn(authService, 'registerRequest').and.returnValue(of(mockRegisterResponse))
spyOn(authService, 'modifyUserWithAdmin').and.returnValue(Promise.resolve())

authService.register(mockUser).then((returnValue) => {
authService.register(mockRegisterUser).then((returnValue) => {
expect(returnValue).toBeTruthy()
expect(returnValue).toEqual(mockResponse)
expect(authService.modifyUserWithAdmin).toHaveBeenCalledWith(mockResponse.id)
expect(returnValue).toEqual(mockRegisterResponse)
expect(authService.modifyUserWithAdmin).toHaveBeenCalledWith(mockRegisterResponse.id)
done()
}).catch((error) => {
done.fail('Promise should not be rejected: ' + error)
})
})

it('should show UNsuccessly register modal', (done) => {
const mockUser = {
idUser: 'mockIdResponse',
dni: 'mockUserDni',
email: 'mockUserEmail',
name: 'mockUserName',
itineraryId: 'mockUserIteneraryId',
password: 'mockUserPassword',
confirmPassword: 'mockUserConfirmPassword'
}

const mockErrorMessage = 'Invalid data'
const mockErrorResponse = { // response we expect from the registerRequest function.
message: mockErrorMessage
}

spyOn(authService, 'registerRequest').and.returnValue(
throwError({ status: 401, error: mockErrorResponse })
throwError({ status: 401, error: mockRegisterErrorResponse })
)

authService.register(mockUser).then(() => {
authService.register(mockRegisterUser).then(() => {
done.fail('Register should have failed')
}).catch((error) => {
expect(error).toEqual(mockErrorResponse.message)
expect(error).toEqual(mockRegisterErrorResponse.message)
done()
})
})
Expand Down
Loading
Loading