Compare commits

...

3 Commits

@ -1,6 +1,6 @@
# spring-rest-example
This project is a example of using jpa.
This project is a example of using Spring REST.
The project uses the [Spock Framework Example Project](https://github.com/spockframework/)

@ -6,7 +6,7 @@
<artifactId>spring_rest_example</artifactId>
<version>2.0.1</version>
<packaging>war</packaging>
<name>Example of using jpa for a hostpital.</name>
<name>Example of using Spring REST for a hostpital.</name>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

@ -0,0 +1,39 @@
package space.kklochko.spring_rest_example.db.repositories;
import jakarta.persistence.EntityManager;
import jakarta.persistence.NoResultException;
import jakarta.persistence.TypedQuery;
import org.springframework.stereotype.Component;
import space.kklochko.spring_rest_example.db.repositories.blocks.TransactionalInsert;
import space.kklochko.spring_rest_example.db.repositories.blocks.TransactionalRemove;
import space.kklochko.spring_rest_example.db.repositories.blocks.TransactionalUpdate;
import space.kklochko.spring_rest_example.models.Token;
import space.kklochko.spring_rest_example.models.User;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.UUID;
@Component
public class UserRepository {
public User read(String username) {
Optional<User> user = readAll()
.stream()
.filter(u -> u.getUsername().equals(username))
.findFirst();
return user.orElse(null);
}
public List<User> readAll() {
List<User> users = new ArrayList<>();
// password same as username
users.add(new User("admin", "ADMIN", "8c6976e5b5410415bde908bd4dee15dfb167a9c873fc4bb8a81f6f2ab448a918"));
users.add(new User("user", "USER", "04f8996da763b7a969b1028ee3007569eaf3a635486ddab211d512c85b9df8fb"));
return users;
}
}

@ -0,0 +1,19 @@
package space.kklochko.spring_rest_example.models;
import lombok.AllArgsConstructor;
import lombok.Data;
@AllArgsConstructor
@Data
public class User {
private String username;
private String role;
private String password;
public User() {
setUsername("");
setRole("");
setPassword("");
}
}

@ -0,0 +1,8 @@
package space.kklochko.spring_rest_example.security.hashes;
public class Sha256Hash {
public String hashOf(String input) {
return org.apache.commons.codec.digest.DigestUtils.sha256Hex(input);
}
}

@ -1,8 +1,10 @@
package space.kklochko.spring_rest_example.security.tokens;
import space.kklochko.spring_rest_example.security.hashes.Sha256Hash;
public class SimpleToken extends StringTokenFactory {
public String create(String input) {
return org.apache.commons.codec.digest.DigestUtils.sha256Hex(input);
return (new Sha256Hash()).hashOf(input);
}
}

@ -0,0 +1,59 @@
package space.kklochko.spring_rest_example.db.repositories
import groovy.sql.Sql
import jakarta.persistence.EntityManager
import space.kklochko.spring_rest_example.config.factories.LoadDataBaseConfigFromEnvFile
import space.kklochko.spring_rest_example.config.models.DataBaseConfig
import space.kklochko.spring_rest_example.db.factories.DataBaseConnection
import space.kklochko.spring_rest_example.db.factories.EntityManagerConnection
import space.kklochko.spring_rest_example.models.Token
import space.kklochko.spring_rest_example.models.User
import spock.lang.Shared
import spock.lang.Specification
import spock.lang.Stepwise
import spock.lang.Subject
@Stepwise
class UserRepositorySpec extends Specification {
def "Read one user"() {
given: "I have a repo"
@Subject
UserRepository repo = new UserRepository()
when: "reading the entry"
def result = repo.read(username)
then: "checking that the reading was successful"
result
and: "the user has a role"
with(result) {
getRole() == expectedRole
getPassword() == expectedPassword
}
where: "the expected data"
username || expectedRole | expectedPassword
"admin" || "ADMIN" | "8c6976e5b5410415bde908bd4dee15dfb167a9c873fc4bb8a81f6f2ab448a918"
"user" || "USER" | "04f8996da763b7a969b1028ee3007569eaf3a635486ddab211d512c85b9df8fb"
}
def "Read one user and check if exists"() {
given: "I have a repo"
@Subject
UserRepository repo = new UserRepository()
when: "reading the entry"
def result = repo.read(username)
then: "checking that the reading was successful"
isNull == (result == null)
where: "the expected data"
username || isNull
"admin" || false
"user" || false
"user2" || true
}
}

@ -0,0 +1,27 @@
package space.kklochko.spring_rest_example.security.hashes
import spock.lang.Specification;
import spock.lang.Subject;
public class Sha256HashSpec extends Specification {
def "Create a token that based on sha256"() {
given: "I have a hasher"
@Subject
def hasher = new Sha256Hash()
when: "I get a hash of a string"
String hash = hasher.hashOf(input)
then: "The token is the same if the same input data."
expectedHash == hash
where: "I have the expected data"
input || expectedHash
"abc" || "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad"
"random numbers232341" || "7f4b80b2b09ae4326f78b1a9782c807af0a7d0194bffb05602a6585466b4c43e"
"admin" || "8c6976e5b5410415bde908bd4dee15dfb167a9c873fc4bb8a81f6f2ab448a918"
"user" || "04f8996da763b7a969b1028ee3007569eaf3a635486ddab211d512c85b9df8fb"
}
}
Loading…
Cancel
Save