@@ -15,6 +15,7 @@ import io.github.nefilim.kjwt.JWSHMAC512Algorithm
1515import io.github.nefilim.kjwt.JWT
1616import io.github.nefilim.kjwt.KJWTVerificationError
1717import io.github.nomisrev.articles.Article
18+ import io.github.nomisrev.articles.ArticleService
1819import io.github.nomisrev.articles.CreateArticle
1920import io.github.nomisrev.env.Dependencies
2021import io.github.nomisrev.env.kotlinXSerializersModule
@@ -29,84 +30,104 @@ import kotlinx.serialization.json.Json
2930
3031@TestRegistering
3132fun TestSuite.testService (
32- @TestElementName name : String ,
33- testConfig : TestConfig = TestConfig ,
34- test : suspend context(Dependencies ) Test .ExecutionScope .() -> Unit ,
33+ @TestElementName name : String ,
34+ testConfig : TestConfig = TestConfig ,
35+ test : suspend context(Dependencies ) Test .ExecutionScope .() -> Unit ,
3536) =
36- test(name, testConfig) {
37- withTestDependencies { dependencies ->
38- test.invoke(dependencies, this @test)
39- }
37+ test(name, testConfig) {
38+ withTestDependencies { dependencies ->
39+ test.invoke(dependencies, this @test)
4040 }
41+ }
42+
43+ @TestRegistering
44+ fun TestSuite.testDependencies (
45+ @TestElementName name : String ,
46+ testConfig : TestConfig = TestConfig ,
47+ test :
48+ suspend context(Dependencies , Raise <DomainError >) Test .ExecutionScope .(
49+ ) -> Unit ,
50+ ) = test(name, testConfig) {
51+ withTestDependencies { dependencies ->
52+ either<DomainError , Unit > {
53+ test.invoke(
54+ dependencies,
55+ contextOf<Raise <DomainError >>(),
56+ this @test,
57+ )
58+ }
59+ .shouldBeRight()
60+ }
61+ }
4162
4263@TestRegistering
4364fun TestSuite.testServer (
44- @TestElementName name : String ,
45- testConfig : TestConfig = TestConfig ,
46- test :
47- suspend context(Dependencies , HttpClient , Raise <DomainError >) Test .ExecutionScope .(
48- ) -> Unit ,
65+ @TestElementName name : String ,
66+ testConfig : TestConfig = TestConfig ,
67+ test :
68+ suspend context(Dependencies , HttpClient , Raise <DomainError >) Test .ExecutionScope .(
69+ ) -> Unit ,
4970) =
50- test(name, testConfig) {
51- withTestDependencies { dependencies ->
52- testApplication {
53- application { app(dependencies) }
54- createClient {
55- expectSuccess = false
56- install(ContentNegotiation ) {
57- json(Json { serializersModule = kotlinXSerializersModule })
58- }
59- }
60- .use { client ->
61- either<DomainError , Unit > {
62- test.invoke(
63- dependencies,
64- client,
65- contextOf<Raise <DomainError >>(),
66- this @test,
67- )
68- }
69- .shouldBeRight()
70- }
71- }
71+ test(name, testConfig) {
72+ withTestDependencies { dependencies ->
73+ testApplication {
74+ application { app(dependencies) }
75+ createClient {
76+ expectSuccess = false
77+ install(ContentNegotiation ) {
78+ json(Json { serializersModule = kotlinXSerializersModule })
79+ }
7280 }
81+ .use { client ->
82+ either<DomainError , Unit > {
83+ test.invoke(
84+ dependencies,
85+ client,
86+ contextOf<Raise <DomainError >>(),
87+ this @test,
88+ )
89+ }
90+ .shouldBeRight()
91+ }
92+ }
7393 }
94+ }
7495
7596context(client: HttpClient )
7697val client: HttpClient
77- get() = client
98+ get() = client
7899
79100context(dependencies: Dependencies )
80101val dependencies: Dependencies
81- get() = dependencies
102+ get() = dependencies
82103
83- context(dependencies: Dependencies , _: Raise <DomainError >)
84- suspend fun createArticle (userId : UserId , article : ArticleFixture = articleFixture()): Article =
85- dependencies.articleService.createArticle(
86- CreateArticle (
87- userId,
88- article.title,
89- article.description,
90- article.body,
91- article.tags,
92- )
104+ context(_: Raise <DomainError >)
105+ suspend fun ArticleService.createArticle (userId : UserId , article : ArticleFixture = articleFixture()): Article =
106+ createArticle(
107+ CreateArticle (
108+ userId,
109+ article.title,
110+ article.description,
111+ article.body,
112+ article.tags,
93113 )
114+ )
94115
95116context(dependencies: Dependencies , _: Raise <DomainError >)
96117fun registerUser (fixture : UserFixture = userFixture()): RegisteredUser {
97- val token =
98- dependencies.userService.register(
99- RegisterUser (fixture.username, fixture.email, fixture.password)
100- )
101- val jwt =
102- withError({ JwtInvalid (it.toString()) }) {
103- JWT .decodeT(token.value, JWSHMAC512Algorithm )
104- .bind<KJWTVerificationError , DecodedJWT <JWSHMAC512Algorithm >>()
105- }
106- val id =
107- ensureNotNull(jwt.claimValueAsLong(" id" ).getOrNull()) {
108- JwtInvalid (" id missing from JWT Token" )
109- }
118+ val token =
119+ dependencies.userService.register(
120+ RegisterUser (fixture.username, fixture.email, fixture.password)
121+ )
122+ val jwt =
123+ withError({ JwtInvalid (it.toString()) }) {
124+ JWT .decodeT(token.value, JWSHMAC512Algorithm )
125+ .bind<KJWTVerificationError , DecodedJWT <JWSHMAC512Algorithm >>()
126+ }
127+ val id =
128+ ensureNotNull(jwt.claimValueAsLong(" id" ).getOrNull()) {
129+ JwtInvalid (" id missing from JWT Token" )
130+ }
110131
111- return RegisteredUser (fixture, token, UserId (id))
132+ return RegisteredUser (fixture, token, UserId (id))
112133}
0 commit comments