Add edition of user name or user email
This commit is contained in:
7
pom.xml
7
pom.xml
@@ -70,6 +70,13 @@
|
|||||||
<artifactId>commons-lang3</artifactId>
|
<artifactId>commons-lang3</artifactId>
|
||||||
<version>3.7</version>
|
<version>3.7</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<!-- https://mvnrepository.com/artifact/org.mockito/mockito-all -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.mockito</groupId>
|
||||||
|
<artifactId>mockito-all</artifactId>
|
||||||
|
<version>1.9.5</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<build>
|
<build>
|
||||||
|
|||||||
@@ -87,4 +87,10 @@ public class AccountController {
|
|||||||
public UserDTO signin(@RequestBody final UserDTO pUser, final HttpServletResponse pResponse) throws IOException {
|
public UserDTO signin(@RequestBody final UserDTO pUser, final HttpServletResponse pResponse) throws IOException {
|
||||||
return accountService.signin(pUser, pResponse);
|
return accountService.signin(pUser, pResponse);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@PutMapping("/")
|
||||||
|
public void update(@RequestBody final UserDTO pUser, final HttpServletRequest pRequest,
|
||||||
|
final HttpServletResponse pResponse) throws IOException {
|
||||||
|
accountService.updateUser(pUser, pRequest, pResponse);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -140,4 +140,32 @@ public class AccountService {
|
|||||||
|
|
||||||
return new UserDTO(user);
|
return new UserDTO(user);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void updateUser(final UserDTO pUser, final HttpServletRequest pRequest,
|
||||||
|
final HttpServletResponse pResponse) throws IOException {
|
||||||
|
final Optional<User> connectedUserOpt = tokenService.getAuthenticatedUserByToken(pRequest);
|
||||||
|
if(connectedUserOpt.isPresent()) {
|
||||||
|
final User connectedUser = connectedUserOpt.get();
|
||||||
|
|
||||||
|
final Optional<User> userFromDb = userRepository.findByEmail(pUser.getEmail());
|
||||||
|
|
||||||
|
/*
|
||||||
|
* If a user is returned by the repository, that's the email adress is used, but
|
||||||
|
* if it is not the same as the connected user, that's the email adress
|
||||||
|
* corresponds to another user. So a 409 error is sent. Otherwise, if no user is
|
||||||
|
* returned by the repository, that's the email adress is free to be used. So,
|
||||||
|
* user can change him email adress.
|
||||||
|
*/
|
||||||
|
if(userFromDb.isPresent() && !connectedUser.getEmail().equals(userFromDb.get().getEmail())) {
|
||||||
|
pResponse.sendError(HttpServletResponse.SC_CONFLICT);
|
||||||
|
} else {
|
||||||
|
connectedUser.setName(pUser.getName());
|
||||||
|
connectedUser.setEmail(pUser.getEmail());
|
||||||
|
|
||||||
|
userRepository.save(connectedUser);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
pResponse.sendError(HttpServletResponse.SC_UNAUTHORIZED);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,7 +7,7 @@
|
|||||||
alt="Card image cap"
|
alt="Card image cap"
|
||||||
mdbTooltip="Modifier mon image de profil"
|
mdbTooltip="Modifier mon image de profil"
|
||||||
placement="bottom">
|
placement="bottom">
|
||||||
<div class="card-body">
|
<div id="form" class="card-body">
|
||||||
<form (ngSubmit)="onSubmit()" #profilEditionForm="ngForm">
|
<form (ngSubmit)="onSubmit()" #profilEditionForm="ngForm">
|
||||||
<div class="md-form">
|
<div class="md-form">
|
||||||
<input mdbActive
|
<input mdbActive
|
||||||
@@ -19,7 +19,7 @@
|
|||||||
#name="ngModel"
|
#name="ngModel"
|
||||||
data-error="Veuillez saisir votre nom d'utilisateur"
|
data-error="Veuillez saisir votre nom d'utilisateur"
|
||||||
data-sucess=""
|
data-sucess=""
|
||||||
required disabled />
|
required />
|
||||||
<label for="name">Nom d'utilisateur</label>
|
<label for="name">Nom d'utilisateur</label>
|
||||||
</div>
|
</div>
|
||||||
<div class="md-form">
|
<div class="md-form">
|
||||||
@@ -32,15 +32,25 @@
|
|||||||
#email="ngModel"
|
#email="ngModel"
|
||||||
data-error="Veuillez saisir votre adresse email"
|
data-error="Veuillez saisir votre adresse email"
|
||||||
data-sucess=""
|
data-sucess=""
|
||||||
required disabled />
|
required />
|
||||||
<label for="email">Email</label>
|
<label for="email">Email</label>
|
||||||
</div>
|
</div>
|
||||||
|
<div id="errorMsg" class="card red lighten-2 text-center z-depth-2">
|
||||||
|
<div class="card-body">
|
||||||
|
<p class="white-text mb-0">{{modelError}}</p>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<div id="resultMsg" class="card green lighten-2 text-center z-depth-2" >
|
||||||
|
<div class="card-body">
|
||||||
|
<p class="white-text mb-0">{{result}}</p>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
<div class="col submitFormArea">
|
<div class="col submitFormArea">
|
||||||
<a routerLink="/accountSettings" class="indigo-text">
|
<a routerLink="/accountSettings" class="indigo-text">
|
||||||
Annuler
|
Annuler
|
||||||
</a>
|
</a>
|
||||||
<button class="float-right waves-effect waves-light indigo btn"
|
<button class="float-right waves-effect waves-light indigo btn"
|
||||||
type="submit" [disabled]="!profilEditionForm.form.valid || true" disabled>
|
type="submit" [disabled]="!profilEditionForm.form.valid">
|
||||||
Suivant
|
Suivant
|
||||||
</button>
|
</button>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
@@ -10,7 +10,7 @@ import { environment } from '../../../environments/environment';
|
|||||||
selector: 'app-profil-edition',
|
selector: 'app-profil-edition',
|
||||||
templateUrl: './profil-edition.component.html',
|
templateUrl: './profil-edition.component.html',
|
||||||
styles: [`
|
styles: [`
|
||||||
.card-body {
|
#form {
|
||||||
padding-bottom: 10px;
|
padding-bottom: 10px;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -21,6 +21,13 @@ import { environment } from '../../../environments/environment';
|
|||||||
#profil-image {
|
#profil-image {
|
||||||
cursor: pointer;
|
cursor: pointer;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#resultMsg, #errorMsg {
|
||||||
|
max-height: 0;
|
||||||
|
overflow: hidden;
|
||||||
|
transition: max-height 0.5s ease-out;
|
||||||
|
margin: 0;
|
||||||
|
}
|
||||||
`]
|
`]
|
||||||
})
|
})
|
||||||
export class ProfilEditionComponent implements OnInit {
|
export class ProfilEditionComponent implements OnInit {
|
||||||
@@ -69,6 +76,24 @@ export class ProfilEditionComponent implements OnInit {
|
|||||||
}
|
}
|
||||||
|
|
||||||
onSubmit(): void {
|
onSubmit(): void {
|
||||||
// TODO
|
this.profilEditionService.updateUser(this.model).subscribe(() => {
|
||||||
|
this.setMessage('Modification enregistrée.', false);
|
||||||
|
}, error => {
|
||||||
|
this.setMessage('L\'adresse mail saisie n\'est pas disponible.', true);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
setMessage(message: string, error: boolean): void {
|
||||||
|
this[error ? 'modelError' : 'result'] = message;
|
||||||
|
|
||||||
|
const resultMsgDiv = document.getElementById(error ? 'errorMsg' : 'resultMsg');
|
||||||
|
resultMsgDiv.style.maxHeight = '64px';
|
||||||
|
|
||||||
|
setTimeout(() => {
|
||||||
|
resultMsgDiv.style.maxHeight = '0px';
|
||||||
|
setTimeout(() => {
|
||||||
|
this[error ? 'modelError' : 'result'] = undefined;
|
||||||
|
}, 550);
|
||||||
|
}, 3000);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,8 +2,10 @@ import { Injectable } from '@angular/core';
|
|||||||
import {HttpClient, HttpRequest, HttpEvent} from '@angular/common/http';
|
import {HttpClient, HttpRequest, HttpEvent} from '@angular/common/http';
|
||||||
import {Observable} from 'rxjs/Observable';
|
import {Observable} from 'rxjs/Observable';
|
||||||
import { environment } from '../../../environments/environment';
|
import { environment } from '../../../environments/environment';
|
||||||
|
import { User } from '../../core/entities';
|
||||||
|
|
||||||
const IMAGES_URL = environment.apiUrl + '/api/images';
|
const IMAGES_URL = environment.apiUrl + '/api/images';
|
||||||
|
const ACCOUNT_URL = environment.apiUrl + '/api/account';
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class ProfilEditionService {
|
export class ProfilEditionService {
|
||||||
@@ -21,4 +23,8 @@ export class ProfilEditionService {
|
|||||||
}
|
}
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
updateUser(user: User): Observable<any> {
|
||||||
|
return this.http.put<any>(`${ACCOUNT_URL}/`, user);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
125
src/test/java/org/codiki/account/AccountServiceTest.java
Normal file
125
src/test/java/org/codiki/account/AccountServiceTest.java
Normal file
@@ -0,0 +1,125 @@
|
|||||||
|
package org.codiki.account;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
|
import javax.servlet.http.HttpServletRequest;
|
||||||
|
import javax.servlet.http.HttpServletResponse;
|
||||||
|
|
||||||
|
import org.codiki.core.entities.dto.UserDTO;
|
||||||
|
import org.codiki.core.entities.persistence.User;
|
||||||
|
import org.codiki.core.repositories.UserRepository;
|
||||||
|
import org.codiki.core.security.TokenService;
|
||||||
|
import org.junit.Before;
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.junit.runner.RunWith;
|
||||||
|
import org.mockito.InjectMocks;
|
||||||
|
import org.mockito.Mock;
|
||||||
|
import org.mockito.Mockito;
|
||||||
|
import org.mockito.MockitoAnnotations;
|
||||||
|
import org.mockito.junit.MockitoJUnitRunner;
|
||||||
|
|
||||||
|
@RunWith(MockitoJUnitRunner.class)
|
||||||
|
public class AccountServiceTest {
|
||||||
|
|
||||||
|
@InjectMocks
|
||||||
|
private AccountService service;
|
||||||
|
|
||||||
|
@Mock
|
||||||
|
private UserRepository repository;
|
||||||
|
|
||||||
|
@Mock
|
||||||
|
private TokenService tokenService;
|
||||||
|
|
||||||
|
@Mock
|
||||||
|
private HttpServletRequest request;
|
||||||
|
|
||||||
|
@Mock
|
||||||
|
private HttpServletResponse response;
|
||||||
|
|
||||||
|
@Before
|
||||||
|
public void before() {
|
||||||
|
MockitoAnnotations.initMocks(AccountServiceTest.class);
|
||||||
|
}
|
||||||
|
|
||||||
|
private Optional<User> buildUser() {
|
||||||
|
final User u = new User();
|
||||||
|
u.setId(1L);
|
||||||
|
u.setName("name");
|
||||||
|
u.setEmail("test@test.te");
|
||||||
|
return Optional.of(u);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void test_updateUser_ok_diff_name() throws IOException {
|
||||||
|
final UserDTO userDto = new UserDTO();
|
||||||
|
userDto.setName("anotherName");
|
||||||
|
userDto.setEmail("test@test.te");
|
||||||
|
|
||||||
|
final Optional<User> connectedUser = buildUser();
|
||||||
|
final Optional<User> userFromDb = buildUser();
|
||||||
|
|
||||||
|
Mockito.when(tokenService.getAuthenticatedUserByToken(Mockito.any())).thenReturn(connectedUser);
|
||||||
|
Mockito.when(repository.findByEmail(Mockito.anyString())).thenReturn(userFromDb);
|
||||||
|
|
||||||
|
service.updateUser(userDto, request, response);
|
||||||
|
Mockito.verify(response, Mockito.times(0)).sendError(Mockito.anyInt());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void test_updateUser_ok_diff_email() throws IOException {
|
||||||
|
final UserDTO userDto = new UserDTO();
|
||||||
|
userDto.setName("name");
|
||||||
|
userDto.setEmail("autre_adresse@test.te");
|
||||||
|
|
||||||
|
final Optional<User> connectedUser = buildUser();
|
||||||
|
final Optional<User> userFromDb = buildUser();
|
||||||
|
|
||||||
|
Mockito.when(tokenService.getAuthenticatedUserByToken(Mockito.any())).thenReturn(connectedUser);
|
||||||
|
Mockito.when(repository.findByEmail(Mockito.anyString())).thenReturn(userFromDb);
|
||||||
|
|
||||||
|
service.updateUser(userDto, request, response);
|
||||||
|
Mockito.verify(response, Mockito.times(0)).sendError(Mockito.anyInt());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void test_updateUser_ok_diff_all() throws IOException {
|
||||||
|
final UserDTO userDto = new UserDTO();
|
||||||
|
userDto.setName("anotherName");
|
||||||
|
userDto.setEmail("another_adress@test.te");
|
||||||
|
|
||||||
|
final Optional<User> connectedUser = buildUser();
|
||||||
|
final Optional<User> userFromDb = buildUser();
|
||||||
|
|
||||||
|
Mockito.when(tokenService.getAuthenticatedUserByToken(Mockito.any())).thenReturn(connectedUser);
|
||||||
|
Mockito.when(repository.findByEmail(Mockito.anyString())).thenReturn(userFromDb);
|
||||||
|
|
||||||
|
service.updateUser(userDto, request, response);
|
||||||
|
Mockito.verify(response, Mockito.times(0)).sendError(Mockito.anyInt());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void test_updateUser_error_email() throws IOException {
|
||||||
|
final UserDTO userDto = new UserDTO();
|
||||||
|
userDto.setName("name");
|
||||||
|
userDto.setEmail("test@test.te");
|
||||||
|
|
||||||
|
final Optional<User> connectedUser = buildUser();
|
||||||
|
final Optional<User> userFromDb = buildUser();
|
||||||
|
userFromDb.get().setEmail("another_adress@test.te");
|
||||||
|
|
||||||
|
Mockito.when(tokenService.getAuthenticatedUserByToken(Mockito.any())).thenReturn(connectedUser);
|
||||||
|
Mockito.when(repository.findByEmail(Mockito.anyString())).thenReturn(userFromDb);
|
||||||
|
|
||||||
|
service.updateUser(userDto, request, response);
|
||||||
|
Mockito.verify(response, Mockito.times(1)).sendError(Mockito.anyInt());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void test_updateUser_error_disconnected() throws IOException {
|
||||||
|
Mockito.when(tokenService.getAuthenticatedUserByToken(Mockito.any())).thenReturn(Optional.empty());
|
||||||
|
|
||||||
|
service.updateUser(null, request, response);
|
||||||
|
Mockito.verify(response, Mockito.times(1)).sendError(Mockito.anyInt());
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user