Browse Source

create PasswordEncoder for AccountServiceTest mocked

feature-pr-AccountService
Sona Markosyan 3 years ago
parent
commit
4661e9c4f6
  1. 22
      src/main/java/hs/fulda/de/ci/exam/project/Account.java
  2. 5
      src/main/java/hs/fulda/de/ci/exam/project/AccountRepository.java
  3. 24
      src/main/java/hs/fulda/de/ci/exam/project/AccountService.java
  4. 35
      src/test/java/hs/fulda/de/ci/exam/project/AccountServiceTest.java

22
src/main/java/hs/fulda/de/ci/exam/project/Account.java

@ -33,6 +33,15 @@ public class Account {
this.status = status;
}
public boolean isEnabled() {
if(status == AccountStatus.ACTIVE) return true;
return false;
}
public String getPasswordHash() {
return new PasswordEncoder().encode(password);
}
public enum AccountStatus {
ACTIVE,
CLOSED,
@ -67,4 +76,17 @@ public class Account {
}
}
class PasswordEncoder{
String encode(String password){
int p = 31;
int m = (int) Math.pow(10, 9) + 9;
int hash_value = 0;
int p_pow = 1;
for (char c : password.toCharArray()) {
hash_value = (hash_value + (c - 'a' + 1) * p_pow) % m;
p_pow = (p_pow * p) % m;
}
return Integer.toString(hash_value);
}
}

5
src/main/java/hs/fulda/de/ci/exam/project/AccountRepository.java

@ -14,6 +14,11 @@ public class AccountRepository {
}
return false;
}
Account findById(String id){
Account account = accountList.get(id);
return account;
}
private String generateKey(Account account) {
return String.format("%s", account.getId());
}

24
src/main/java/hs/fulda/de/ci/exam/project/AccountService.java

@ -0,0 +1,24 @@
package hs.fulda.de.ci.exam.project;
public class AccountService {
private final AccountRepository accountRepository;
private final PasswordEncoder passwordEncoder;
public AccountService(AccountRepository accountRepository, PasswordEncoder passwordEncoder) {
this.accountRepository = accountRepository;
this.passwordEncoder = passwordEncoder;
}
public boolean isValidAccount(String id, String password){
Account account = accountRepository.findById(id);
return isEnabledAccount(account) && isValidPassword(account, password);
}
private boolean isEnabledAccount(Account account) {
return account!= null && account.isEnabled();
}
private boolean isValidPassword(Account account, String password) {
String encodedPassword = passwordEncoder.encode(password);
return encodedPassword.equals(account.getPasswordHash());
}
}

35
src/test/java/hs/fulda/de/ci/exam/project/AccountServiceTest.java

@ -0,0 +1,35 @@
package hs.fulda.de.ci.exam.project;
import org.junit.Before;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
public class AccountServiceTest {
private static final String PASSWORD = "password";
private static final Account ENABLED_USER =
new Account("user id", "hash", Account.AccountStatus.ACTIVE);
private static final Account DISABLED_USER =
new Account("disabled user id", "disabled user password hash", Account.AccountStatus.CLOSED);
private AccountRepository accountRepository;
private PasswordEncoder passwordEncoder;
private AccountService accountService;
@Before
public void setup() {
passwordEncoder = createPasswordEncoder();
accountService = new AccountService(accountRepository, passwordEncoder);
}
private PasswordEncoder createPasswordEncoder() {
PasswordEncoder mock = mock(PasswordEncoder.class);
when(mock.encode(anyString())).thenReturn("any password hash");
when(mock.encode(PASSWORD)).thenReturn(ENABLED_USER.getPasswordHash());
return mock;
}
}
Loading…
Cancel
Save