Skip to content

Commit 77b17f8

Browse files
Refactor user controller tests
1 parent a9f3ee1 commit 77b17f8

5 files changed

Lines changed: 129 additions & 102 deletions

File tree

src/main/java/hexlet/code/config/SecurityConfig.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323

2424
@Configuration
25+
//@EnableMethodSecurity(prePostEnabled = true)
2526
@EnableMethodSecurity
2627
@EnableWebSecurity
2728
public class SecurityConfig {

src/main/java/hexlet/code/controller/UserController.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@
2828
@RequestMapping("/api/users")
2929
@AllArgsConstructor
3030
public class UserController {
31-
private static final String ONLY_OWNER_BY_ID = """
32-
@userRepository.findById(#id).get().getEmail() == authentication.getName()
33-
""";
31+
// private static final String ONLY_OWNER_BY_ID = """
32+
// @userRepository.findById(#id).get().getEmail() == authentication.getName()
33+
// """;
3434

3535

3636
private final UserRepository userRepository;
@@ -72,7 +72,7 @@ public UserDTO create(@Valid @RequestBody UserCreateDTO dto) {
7272
* @param dto
7373
* @return UserDTO
7474
*/
75-
@PreAuthorize(ONLY_OWNER_BY_ID)
75+
@PreAuthorize("@userRepository.findById(#id).get().getEmail() == authentication.getName()")
7676
@PutMapping(path = "/{id}")
7777
@ResponseStatus(HttpStatus.OK)
7878
public UserDTO update(@PathVariable("id") Long id, @Valid @RequestBody UserUpdateDTO dto) {
@@ -82,7 +82,8 @@ public UserDTO update(@PathVariable("id") Long id, @Valid @RequestBody UserUpdat
8282
/**
8383
* @param id
8484
*/
85-
@PreAuthorize(ONLY_OWNER_BY_ID)
85+
// @PreAuthorize(ONLY_OWNER_BY_ID)
86+
@PreAuthorize("@userRepository.findById(#id).get().getEmail() == authentication.getName()")
8687
@DeleteMapping(path = "/{id}")
8788
@ResponseStatus(HttpStatus.NO_CONTENT)
8889
public void delete(@PathVariable long id) {

src/main/java/hexlet/code/dto/user/UserDTO.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,85 +12,85 @@ public class UserDTO {
1212
private String lastName;
1313

1414
@JsonFormat(pattern = "yyyy-MM-dd")
15+
// @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd")
1516
private LocalDateTime createdAt;
1617

1718
/**
18-
*
1919
* @return id
2020
*/
2121
public Long getId() {
2222
return id;
2323
}
2424

2525
/**
26-
*
2726
* @param id
2827
*/
2928
public void setId(Long id) {
3029
this.id = id;
3130
}
3231

3332
/**
34-
*
3533
* @return email
3634
*/
3735
public String getEmail() {
3836
return email;
3937
}
4038

4139
/**
42-
*
4340
* @param email
4441
*/
4542
public void setEmail(String email) {
4643
this.email = email;
4744
}
4845

4946
/**
50-
*
5147
* @return firstName
5248
*/
5349
public String getFirstName() {
5450
return firstName;
5551
}
5652

5753
/**
58-
*
5954
* @param firstName
6055
*/
6156
public void setFirstName(String firstName) {
6257
this.firstName = firstName;
6358
}
6459

6560
/**
66-
*
6761
* @return String
6862
*/
6963
public String getLastName() {
7064
return lastName;
7165
}
7266

7367
/**
74-
*
7568
* @param lastName
7669
*/
7770
public void setLastName(String lastName) {
7871
this.lastName = lastName;
7972
}
8073

8174
/**
82-
*
8375
* @return createdAt
8476
*/
8577
public LocalDateTime getCreatedAt() {
8678
return createdAt;
8779
}
8880

8981
/**
90-
*
9182
* @param createdAt
9283
*/
9384
public void setCreatedAt(LocalDateTime createdAt) {
9485
this.createdAt = createdAt;
9586
}
87+
88+
/**
89+
* For test, then createdAt is String Shape pattern = "yyyy-MM-dd".
90+
*
91+
* @param createdAt
92+
*/
93+
public void setCreatedAt(String createdAt) {
94+
this.createdAt = LocalDateTime.parse(createdAt + "T00:00");
95+
}
9696
}

src/main/java/hexlet/code/mapper/UserMapper.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public abstract class UserMapper {
3535
@Mapping(target = "passwordDigest", source = "password")
3636
public abstract User map(UserCreateDTO dto);
3737

38-
public abstract UserDTO map(User user);
38+
public abstract UserDTO map(User model);
3939

4040
@Mapping(target = "passwordDigest", source = "password")
4141
public abstract void update(UserUpdateDTO dto, @MappingTarget User user);

0 commit comments

Comments
 (0)