Added user signup toggle and added other tests
This commit is contained in:
parent
843da9d81c
commit
8257b97a1c
|
@ -37,6 +37,9 @@ or delete an existing, non-anonymous post) you will need to provide your user cr
|
|||
7. Deleting an existing user, will result in a [cascade delete](https://learn.microsoft.com/en-us/ef/core/saving/cascade-delete#:~:text=Cascading%20deletes%20are%20needed%20when%20a%20dependent/child%20entity%20can%20no%20longer%20be%20associated%20with%20its%20current%20principal/parent.%20This%20can%20happen%20because%20the%20principal/parent%20is%20deleted%2C%20or%20it%20can%20happen%20when%20the%20principal/parent%20still%20exists%20but%20the%20dependent/child%20is%20no%20longer%20associated%20with%20it.)
|
||||
of any existing post associated with that user.
|
||||
|
||||
8. User signup can be disabled by setting the environment variable `BIT_DISABLE_SIGNUP` to `1`.
|
||||
By default, user registration is enabled(see `docker-compose.yml`).
|
||||
|
||||
## Database
|
||||
New posts are stored on a relational database(PostgreSQL) using the Spring ORM system(Hibernate).
|
||||
The architecture of the bit platform consists of two tables: **bt_users** and **bt_posts**.
|
||||
|
|
|
@ -15,6 +15,7 @@ services:
|
|||
SPRING_DATASOURCE_PASSWORD: qwerty1234
|
||||
SPRING_SECURITY_USER_NAME: admin
|
||||
SPRING_SECURITY_USER_PASSWORD: admin
|
||||
BIT_DISABLE_SIGNUP: 0
|
||||
restart: always
|
||||
ports:
|
||||
- "3000:3000"
|
||||
|
|
|
@ -10,11 +10,24 @@ import org.springframework.http.ResponseEntity;
|
|||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
|
||||
@RestController
|
||||
public class UserController {
|
||||
private final UserService userService;
|
||||
|
||||
/**
|
||||
* Check if user registration is disabled or not by reading the
|
||||
* 'BIT_DISABLE_SIGNUP' environment variable.
|
||||
*
|
||||
* @return true if 'BIT_DISABLE_SIGNUP' is equal to 1, false otherwise
|
||||
*/
|
||||
private boolean isSignupDisabled() {
|
||||
var env_var = System.getenv("BIT_DISABLE_SIGNUP");
|
||||
|
||||
return Objects.equals(env_var, "1");
|
||||
}
|
||||
|
||||
@Autowired
|
||||
public UserController(UserService userService) {
|
||||
this.userService = userService;
|
||||
|
@ -52,6 +65,11 @@ public class UserController {
|
|||
*/
|
||||
@PostMapping("/api/users/new")
|
||||
public ResponseEntity<String> submitUser(@Valid @RequestBody User user) {
|
||||
// Check if user registration is disabled
|
||||
if(isSignupDisabled()) {
|
||||
throw new GenericErrorException("Registration is disabled", "error");
|
||||
}
|
||||
|
||||
var res = userService.addNewUser(user);
|
||||
if(res.isLeft()) {
|
||||
throw new GenericErrorException(res.getLeft().getMessage(), "error");
|
||||
|
|
|
@ -0,0 +1,41 @@
|
|||
package com.ceticamarco.bits;
|
||||
|
||||
import com.ceticamarco.bits.json.JsonEmitter;
|
||||
import com.ceticamarco.bits.post.Post;
|
||||
import com.ceticamarco.bits.user.User;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.time.LocalDate;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
public class JsonEmitterTests {
|
||||
private Post post;
|
||||
|
||||
@BeforeEach
|
||||
public void tearUp() {
|
||||
this.post = new Post();
|
||||
var user = new User();
|
||||
|
||||
this.post.setId("ABCD");
|
||||
this.post.setTitle("Hello World");
|
||||
this.post.setContent("This is a test");
|
||||
this.post.setExpirationDate(LocalDate.of(1970, 1, 1));
|
||||
user.setId("afj45k");
|
||||
user.setUsername("john");
|
||||
user.setEmail("john@example.com");
|
||||
user.setPassword("qwerty");
|
||||
this.post.setUser(user);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEmitJsonWithoutKey() {
|
||||
var expected = "{\"id\":\"ABCD\",\"title\":\"Hello World\",\"content\":\"This is a test\",\"expirationDate\":\"1970-01-01\",\"user\":{\"id\":\"afj45k\",\"username\":\"john\",\"email\":\"john@example.com\",\"password\":\"qwerty\",\"role\":null}}";
|
||||
|
||||
// Convert object to JSON
|
||||
var get = new JsonEmitter<>(this.post).emitJsonKey();
|
||||
|
||||
assertEquals(expected, get);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue