Add the factory for Token model.
continuous-integration/drone/push Build is passing
Details
continuous-integration/drone/push Build is passing
Details
parent
e99a0bc771
commit
91c95de67e
@ -0,0 +1,33 @@
|
||||
package space.kklochko.spring_rest_example.models.factories;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Getter;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import space.kklochko.spring_rest_example.models.Token;
|
||||
import space.kklochko.spring_rest_example.security.tokens.StringTokenFactory;
|
||||
|
||||
@AllArgsConstructor
|
||||
@Getter
|
||||
public class TokenFactory {
|
||||
public StringTokenFactory factory;
|
||||
|
||||
public TokenFactory() {
|
||||
setFactory(null);
|
||||
}
|
||||
|
||||
public Token create(String username, String role) {
|
||||
Token token = new Token();
|
||||
|
||||
token.setUsername(username);
|
||||
token.setRole(role);
|
||||
token.setToken(getFactory().create(username));
|
||||
|
||||
return token;
|
||||
}
|
||||
|
||||
@Autowired
|
||||
public void setFactory(StringTokenFactory factory) {
|
||||
this.factory = factory;
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,32 @@
|
||||
package space.kklochko.spring_rest_example.models.factories
|
||||
|
||||
import space.kklochko.spring_rest_example.security.tokens.StringTokenFactory
|
||||
import spock.lang.Specification
|
||||
import spock.lang.Subject
|
||||
|
||||
class TokenFactorySpec extends Specification {
|
||||
def "Create a token for a user"() {
|
||||
given: "I have a string token factory"
|
||||
def stringTokenFactory = Mock(StringTokenFactory)
|
||||
stringTokenFactory.create(_) >> "token"
|
||||
|
||||
and: "I have a token factory"
|
||||
@Subject
|
||||
def factory = new TokenFactory(stringTokenFactory)
|
||||
|
||||
and: "I have a username and a role"
|
||||
String username = "user"
|
||||
String role = "USER"
|
||||
|
||||
when: "I create a token"
|
||||
def token = factory.create(username, role)
|
||||
|
||||
then: "The token is the same if the same input data."
|
||||
with(token) {
|
||||
getUsername() == username
|
||||
getRole() == role
|
||||
getToken() == "token"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in new issue