Add the Token repository.
continuous-integration/drone/push Build is passing

This commit is contained in:
2023-12-01 14:59:58 +02:00
parent 91c95de67e
commit 356013b149
3 changed files with 258 additions and 0 deletions
@@ -0,0 +1,187 @@
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 spock.lang.Shared
import spock.lang.Specification
import spock.lang.Stepwise
import spock.lang.Subject
@Stepwise
class TokenRepositorySpec extends Specification {
@Shared
Sql sql
@Shared
EntityManager manager
@Shared
DataBaseConfig db
@Shared
DataBaseConnection dbc
def setupSpec() {
db = (new LoadDataBaseConfigFromEnvFile()).load("db.testing")
dbc = new DataBaseConnection(db)
manager = (new EntityManagerConnection("testing")).connect()
sql = Sql.newInstance(db.getUrl(), db.getProperties().get("user"), db.getProperties().get("password"))
sql.execute("insert into tokens (id, username, role, token) values " +
"('bcbbcdb4-702c-11ee-9113-a0e4349366cb', 'admin', 'ADMIN', '70BA33708CBFB103F1A8E34AFEF333BA7DC021022B2D9AAA583AABB8058D8D67')," +
"('becbafac-702c-11ee-9113-a0e4349366cb', 'user', 'USER', '04F8996DA763B7A969B1028EE3007569EAF3A635486DDAB211D512C85B9DF8FB')")
}
def cleanupSpec() {
sql.execute("delete from tokens")
}
def "Read one token"() {
given: "I have an existing token"
UUID id = UUID.fromString("becbafac-702c-11ee-9113-a0e4349366cb")
and: "I have a repo"
@Subject
TokenRepository repo = new TokenRepository(manager)
when: "reading the entry"
def result = repo.read(id)
then: "checking that the reading was successful"
result
and: "checking the entry with the expected data"
def data = sql.firstRow("SELECT username, role, token FROM tokens WHERE id = ?", id)
with(data) {
get("username") == 'user'
get("role") == 'USER'
get("token") == '04F8996DA763B7A969B1028EE3007569EAF3A635486DDAB211D512C85B9DF8FB'
}
}
def "Read one token by a token value"() {
given: "I have an existing token"
String token = '04F8996DA763B7A969B1028EE3007569EAF3A635486DDAB211D512C85B9DF8FB'
and: "I have a repo"
@Subject
TokenRepository repo = new TokenRepository(manager)
when: "reading the entry"
def result = repo.read(token)
then: "checking that the reading was successful"
result
and: "checking the entry with the expected data"
def data = sql.firstRow("SELECT username, role, token FROM tokens WHERE token = ?", token)
with(data) {
get("username") == 'user'
get("role") == 'USER'
get("token") == '04F8996DA763B7A969B1028EE3007569EAF3A635486DDAB211D512C85B9DF8FB'
}
}
def "Read one nonexistent token by a token value"() {
given: "I have an existing token"
String token = '04F8996DA763B7A969B1028EE3007569EAF3A635486DDAB211D512C85B9DF8FA'
and: "I have a repo"
@Subject
TokenRepository repo = new TokenRepository(manager)
when: "reading the entry"
def result = repo.read(token)
then: "checking that the reading was successful"
!result
}
def "Read all indicators"() {
given: "I have a repo"
@Subject
TokenRepository repo = new TokenRepository(manager)
when: "reading the entries"
def result = repo.readAll()
then: "checking that the reading was successful"
result
and: "checking the entries with the expected data"
def data = sql.rows("SELECT username, role, token FROM tokens")
data.collect{it.get("username")} == ['admin', 'user']
}
def "Add one token"() {
given: "I create an token"
Token token = new Token("user", "USER", "2CF24DBA5FB0A30E26E83B2AC5B9E29E1B161E5C1FA7425E73043362938B9824")
and: "I have a repo"
@Subject
TokenRepository repo = new TokenRepository(manager)
when: "inserting the entry"
def result = repo.create(token)
then: "checking that the insert was successful"
result
token.id
and: "checking that the entry is added"
def isExist = sql.firstRow("SELECT username, role, token FROM tokens WHERE id = ?", token.id)
isExist
}
def "Update one token"() {
given: "I have an existing token"
UUID id = UUID.fromString("becbafac-702c-11ee-9113-a0e4349366cb")
Token token = new Token(id, "user", "USER", "B49F425A7E1F9CFF3856329ADA223F2F9D368F15A00CF48DF16CA95986137FE8")
and: "I have a repo"
@Subject
TokenRepository repo = new TokenRepository(manager)
when: "updating the entry"
def result = repo.update(token)
then: "checking that the update was successful"
result
and: "checking that the entry is updated"
def data = sql.firstRow("SELECT username, role, token FROM tokens WHERE id = ?", token.id)
with(data) {
get("username") == token.username
get("role") == token.role
get("token") == token.token
}
}
def "Remove one token"() {
given: "I have the id of an existing token"
UUID id = UUID.fromString("bcbbcdb4-702c-11ee-9113-a0e4349366cb")
and: "I have a repo"
@Subject
TokenRepository repo = new TokenRepository(manager)
when: "deleting the entry"
def result = repo.delete(id)
then: "checking that the delete was successful"
result
and: "checking that the entry was deleted"
def isExist = sql.firstRow("SELECT username, role, token FROM tokens WHERE id = ?", id)
!isExist
}
}