Implemented Token Generation. Closes #6
parent
8f6fcf2578
commit
674e6f1447
|
@ -55,6 +55,7 @@ public class AccountCrudTest {
|
|||
.andExpect(content().contentType(APPLICATION_JSON))
|
||||
.andDo(MockMvcResultHandlers.print())
|
||||
.andExpect(jsonPath("$.name").value("my-company"))
|
||||
.andExpect(jsonPath("$.token").isNotEmpty())
|
||||
.andExpect(jsonPath("$.receivers", is(List.of("vip@my-company.com"))));
|
||||
}
|
||||
|
||||
|
@ -67,6 +68,7 @@ public class AccountCrudTest {
|
|||
.andExpect(status().is2xxSuccessful())
|
||||
.andExpect(content().contentType(APPLICATION_JSON))
|
||||
.andExpect(jsonPath("$.name").value("retrievable-account"))
|
||||
.andExpect(jsonPath("$.token").isNotEmpty())
|
||||
.andExpect(jsonPath("$.receivers", is(List.of("info@account.com"))));
|
||||
}
|
||||
|
||||
|
@ -81,6 +83,7 @@ public class AccountCrudTest {
|
|||
.andExpect(status().is2xxSuccessful())
|
||||
.andExpect(content().contentType(APPLICATION_JSON))
|
||||
.andExpect(jsonPath("$.name").value("updated-account-renamed"))
|
||||
.andExpect(jsonPath("$.token").isNotEmpty())
|
||||
.andExpect(jsonPath("$.receivers", is(List.of("vip@account.com"))));
|
||||
}
|
||||
|
||||
|
|
|
@ -6,8 +6,11 @@ import lombok.NonNull;
|
|||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.math.BigInteger;
|
||||
import java.security.SecureRandom;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.Random;
|
||||
import java.util.UUID;
|
||||
|
||||
@Data
|
||||
|
@ -15,6 +18,9 @@ import java.util.UUID;
|
|||
@Service
|
||||
public class Relay {
|
||||
|
||||
public static final int TOKEN_NUM_BITS = 256;
|
||||
private final Random RANDOM = new SecureRandom();
|
||||
|
||||
private final AccountRepository accountRepository;
|
||||
private final MessageRepository messageRepository;
|
||||
|
||||
|
@ -31,9 +37,17 @@ public class Relay {
|
|||
}
|
||||
|
||||
public Account saveAccount(@NonNull Account account) {
|
||||
if(StringUtils.isStringEmpty(account.getToken())) {
|
||||
String token = generateToken();
|
||||
account.setToken(token);
|
||||
}
|
||||
return this.accountRepository.save(account);
|
||||
}
|
||||
|
||||
private String generateToken() {
|
||||
return new BigInteger(TOKEN_NUM_BITS, RANDOM).toString(16);
|
||||
}
|
||||
|
||||
public List<Account> accounts() {
|
||||
return this.accountRepository.findAll();
|
||||
}
|
||||
|
|
|
@ -0,0 +1,14 @@
|
|||
package de.nclazz.service.mailrelay.domain;
|
||||
|
||||
public abstract class StringUtils {
|
||||
|
||||
private StringUtils() { /* no-op */ }
|
||||
|
||||
public static boolean isStringEmpty(String input) {
|
||||
if(input == null) {
|
||||
return true;
|
||||
}
|
||||
return input.trim().isEmpty();
|
||||
}
|
||||
|
||||
}
|
|
@ -27,6 +27,7 @@ class AccountRestControllerTest {
|
|||
);
|
||||
|
||||
assertThat(controller.addAccount(form))
|
||||
.extracting("body")
|
||||
.extracting("name", "receivers")
|
||||
.containsExactly("my-account", List.of("vip@my-company.com", "info@my-company.com"));
|
||||
}
|
||||
|
|
|
@ -26,4 +26,17 @@ class RelayTest {
|
|||
.hasSize(1)
|
||||
.containsExactly(account);
|
||||
}
|
||||
|
||||
@Test
|
||||
void saveAccountEnsuresTokenIsSetOnAccount() {
|
||||
Account account = Account.of("my-account", List.of("vip@me.com"));
|
||||
|
||||
FakeAccountRepository repository = new FakeAccountRepository();
|
||||
Relay relay = new Relay(repository, new FakeMessageRepository());
|
||||
|
||||
assertThat(relay.saveAccount(account))
|
||||
.isNotNull()
|
||||
.extracting("token")
|
||||
.isNotNull();
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue