From 8257b97a1c39a802cf75e670ded8b2e67d1aec14 Mon Sep 17 00:00:00 2001 From: Marco Cetica Date: Wed, 24 Jan 2024 11:23:01 +0100 Subject: [PATCH] Added user signup toggle and added other tests --- README.md | 3 ++ docker-compose.yml | 1 + .../ceticamarco/bits/user/UserController.java | 18 ++++++++ .../ceticamarco/bits/JsonEmitterTests.java | 41 +++++++++++++++++++ 4 files changed, 63 insertions(+) create mode 100644 src/test/java/com/ceticamarco/bits/JsonEmitterTests.java diff --git a/README.md b/README.md index 0a7cdd5..9ded315 100644 --- a/README.md +++ b/README.md @@ -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**. diff --git a/docker-compose.yml b/docker-compose.yml index 3eee34e..acbb38c 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -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" diff --git a/src/main/java/com/ceticamarco/bits/user/UserController.java b/src/main/java/com/ceticamarco/bits/user/UserController.java index 8645ac2..0c0f46c 100644 --- a/src/main/java/com/ceticamarco/bits/user/UserController.java +++ b/src/main/java/com/ceticamarco/bits/user/UserController.java @@ -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 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"); diff --git a/src/test/java/com/ceticamarco/bits/JsonEmitterTests.java b/src/test/java/com/ceticamarco/bits/JsonEmitterTests.java new file mode 100644 index 0000000..85a619c --- /dev/null +++ b/src/test/java/com/ceticamarco/bits/JsonEmitterTests.java @@ -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); + } +}