Skip to content

Commit

Permalink
some formatting editing
Browse files Browse the repository at this point in the history
  • Loading branch information
karnilaev committed Jul 18, 2021
1 parent f8fdc12 commit 99790eb
Show file tree
Hide file tree
Showing 21 changed files with 64 additions and 44 deletions.
19 changes: 19 additions & 0 deletions .idea/saveactions_settings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion i18n/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
"otp": {
"message": "Your one-time password is {otp}"
},
"logout": "Log out"
"logout": "Log out",
"unauthorized": "You need to log in"
},
"pages": {
"home": {
Expand Down
3 changes: 1 addition & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions src/app/Routes.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package app
import auth.AuthController
import auth.FakeLoginForTestingController
import db.TransactionCoroutineContext
import domain.portfolios.PortfolioController
import io.jooby.Context
import io.jooby.Jooby
import io.jooby.Kooby
Expand All @@ -15,6 +16,7 @@ fun Kooby.registerRoutes() {

mvc<HealthRoutes>()
mvc<AuthController>()
mvc<PortfolioController>()
if (environment.isTest) mvc<FakeLoginForTestingController>()
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/auth/FakeLoginForTestingController.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import io.jooby.annotations.*
import io.jooby.exception.NotFoundException

@Path("/fake-login")
class FakeLoginForTestingController(val userRepository: UserRepository, val env: Environment): Before {
class FakeLoginForTestingController(private val userRepository: UserRepository, private val env: Environment): Before {
override fun before(ctx: Context) {
if (!env.isActive("test")) throw IllegalStateException("$javaClass should not be active while not testing")
}
Expand Down
1 change: 0 additions & 1 deletion src/auth/User.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package auth

import auth.Role
import db.BaseModel
import java.time.Instant
import java.util.*
Expand Down
2 changes: 1 addition & 1 deletion test/app/JoobyExtensionsTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import org.assertj.core.api.Assertions.assertThat
import org.junit.jupiter.api.Test

class JoobyExtensionsTest {
val ctx = mockk<Context>(relaxed = true)
private val ctx = mockk<Context>(relaxed = true)

@Test
fun isHttps() {
Expand Down
4 changes: 2 additions & 2 deletions test/app/LangTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import org.assertj.core.api.Assertions.assertThat
import org.junit.jupiter.api.Test

class LangTest {
val ctx = mockk<Context>(relaxed = true) {
private val ctx = mockk<Context>(relaxed = true) {
every { cookie(Lang.COOKIE).valueOrNull() } returns null
}

Expand All @@ -28,7 +28,7 @@ class LangTest {
fun `detect from header`() {
every { ctx.header("Accept-Language").valueOrNull() } returns "en-US,en;q=0.9,jp"
assertThat(Lang.detect(ctx)).isEqualTo("en")
verify { ctx.setResponseCookie(match { cookie -> cookie.name == Lang.COOKIE && cookie.value == "en" })}
verify { ctx.setResponseCookie(match { cookie -> cookie.name == Lang.COOKIE && cookie.value == "en" }) }
}

@Test
Expand Down
14 changes: 8 additions & 6 deletions test/app/RequestLoggerTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import org.slf4j.Logger
import org.slf4j.MDC

class RequestLoggerTest {
val ctx = mockk<Context>(relaxed = true) {
private val ctx = mockk<Context>(relaxed = true) {
every { remoteAddress } returns "127.0.0.13"
every { method } returns "GET"
every { requestPath } returns "/path"
Expand All @@ -28,8 +28,8 @@ class RequestLoggerTest {
every { getUser<User>() } returns TestData.user
}

val requestLog = mockk<Logger>(relaxed = true)
val handler = RequestLogger(requestLog)
private val requestLog = mockk<Logger>(relaxed = true)
private val handler = RequestLogger(requestLog)

@Test
fun `successful request log with proxy`() {
Expand All @@ -40,9 +40,11 @@ class RequestLoggerTest {

val user = TestData.user
verify {
requestLog.info(match { it.matches(
"""USER:${user.id} "GET /path\?q=hello" 202 12345 \d+ ms http://referrer "User-Agent"""".toRegex()
)})
requestLog.info(match {
it.matches(
"""USER:${user.id} "GET /path\?q=hello" 202 12345 \d+ ms http://referrer "User-Agent"""".toRegex()
)
})
}
assertThat(MDC.get("requestId")).isNull()
}
Expand Down
2 changes: 1 addition & 1 deletion test/app/RoutesTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import io.mockk.verify
import org.junit.jupiter.api.Test

class RoutesTest {
val app = mockk<Jooby>(relaxed = true)
private val app = mockk<Jooby>(relaxed = true)

@Test
fun `mvc routes without before handler`() {
Expand Down
2 changes: 2 additions & 0 deletions test/app/TestData.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ package app

import auth.Role.USER
import auth.User
import domain.portfolios.Portfolio

/** Reusable entities to use in tests to avoid specifying all params */
object TestData {
val user = User(login = "login", role = USER, lang = "en")
val portfolio = Portfolio(name = "portfolio name")
}
12 changes: 5 additions & 7 deletions test/auth/AuthControllerTest.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package auth

import app.Lang
import app.TestData
import io.jooby.Context
import io.jooby.Session
Expand All @@ -9,16 +8,15 @@ import io.mockk.*
import org.assertj.core.api.Assertions.assertThat
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.assertThrows
import auth.Role.USER

class AuthControllerTest {
val user = TestData.user
val session = mockk<Session>(relaxed = true)
val ctx = mockk<Context>(relaxed = true)
val userRepository = mockk<UserRepository>(relaxed = true) {
private val user = TestData.user
private val session = mockk<Session>(relaxed = true)
private val ctx = mockk<Context>(relaxed = true)
private val userRepository = mockk<UserRepository>(relaxed = true) {
every { create(any(), any(), any(), any()) } returns user
}
val controller = AuthController(userRepository)
private val controller = AuthController(userRepository)

@Test
fun `login with password`() {
Expand Down
8 changes: 4 additions & 4 deletions test/auth/AuthModuleTest.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package auth

import auth.Role.*
import io.jooby.AssetHandler
import io.jooby.Context
import io.jooby.exception.BadRequestException
Expand All @@ -9,15 +10,14 @@ import io.mockk.mockk
import io.mockk.verify
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.assertThrows
import auth.Role.*
import java.util.*

class AuthModuleTest {
val user = User(login = "login", role = USER, lang = "et")
val ctx = mockk<Context>(relaxed = true) {
private val user = User(login = "login", role = USER, lang = "et")
private val ctx = mockk<Context>(relaxed = true) {
every { requestPath } returns "/api/anything"
}
val auth = AuthModule().apply {
private val auth = AuthModule().apply {
userRepository = mockk(relaxed = true) {
every { get(user.id) } returns user
}
Expand Down
2 changes: 1 addition & 1 deletion test/auth/HashingServiceTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import org.assertj.core.api.Assertions.assertThat
import org.junit.jupiter.api.Test

class HashingServiceTest {
val hashingService = HashingService()
private val hashingService = HashingService()

@Test
fun hashPassword() {
Expand Down
7 changes: 4 additions & 3 deletions test/auth/UserRepositoryTest.kt
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
package auth

import auth.Role.ADMIN
import auth.Role.USER
import db.DBTest
import io.mockk.every
import io.mockk.mockk
import org.assertj.core.api.Assertions.assertThat
import org.junit.jupiter.api.Test
import auth.Role.*
import java.security.SecureRandom

class UserRepositoryTest: DBTest() {
val random = mockk<SecureRandom>(relaxed = true)
val repository = UserRepository(db, HashingService(), random)
private val random = mockk<SecureRandom>(relaxed = true)
private val repository = UserRepository(db, HashingService(), random)

@Test
fun create() {
Expand Down
2 changes: 1 addition & 1 deletion test/db/BaseRepositoryTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import org.assertj.core.api.Assertions.assertThat
import org.junit.jupiter.api.Test

class BaseRepositoryTest: DBTest() {
val repository = object: BaseRepository(db, "users") {}
private val repository = object: BaseRepository(db, "users") {}

@Test
fun count() {
Expand Down
3 changes: 1 addition & 2 deletions test/db/JdbcExtensionsTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,10 @@ import org.junit.jupiter.api.*
import org.junit.jupiter.api.TestInstance.Lifecycle.PER_CLASS
import java.util.*
import java.util.UUID.randomUUID
import kotlin.NoSuchElementException

@TestInstance(PER_CLASS)
class JdbcExtensionsTest: DBTest() {
val table = "temp"
private val table = "temp"

@BeforeAll fun before() {
db.exec("create table $table(id uuid primary key, hello varchar, world int)")
Expand Down
10 changes: 4 additions & 6 deletions test/db/RequestTransactionHandlerTest.kt
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package db

import com.zaxxer.hikari.util.DriverDataSource
import db.RequestTransactionHandler
import db.Transaction
import io.jooby.*
import io.mockk.*
import org.assertj.core.api.Assertions.assertThat
Expand All @@ -11,16 +9,16 @@ import java.sql.Connection
import javax.sql.DataSource

class RequestTransactionHandlerTest {
val ctx = mockk<Context>(relaxed = true) {
private val ctx = mockk<Context>(relaxed = true) {
every { requestPath } returns "/path"
every { queryString() } returns "?q=hello"
every { attribute<Transaction>("tx") } answers { Transaction.current()!! }
}
val conn = mockk<Connection>(relaxed = true)
val db = mockk<DriverDataSource>(relaxed = true) {
private val conn = mockk<Connection>(relaxed = true)
private val db = mockk<DriverDataSource>(relaxed = true) {
every { connection } returns conn
}
val handler = RequestTransactionHandler()
private val handler = RequestTransactionHandler()

@Test
fun `commit on success`() {
Expand Down
2 changes: 1 addition & 1 deletion test/db/TransactionTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import org.assertj.core.api.Assertions.assertThat
import org.junit.jupiter.api.Test

class TransactionTest {
val db = mockk<DriverDataSource>(relaxed = true)
private val db = mockk<DriverDataSource>(relaxed = true)

@Test
fun `transaction does not open connection at start`() {
Expand Down
4 changes: 2 additions & 2 deletions test/util/CacheTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import org.assertj.core.api.Assertions.assertThat
import org.junit.jupiter.api.Test

class CacheTest {
var count = 0
val provider = { ++count } // e.g. { http.get("/something") }
private var count = 0
private val provider = { ++count } // e.g. { http.get("/something") }

@Test
fun `cache does not expire`() {
Expand Down
4 changes: 2 additions & 2 deletions test/util/JsonHttpClientTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ import java.net.http.HttpResponse
import java.util.concurrent.CompletableFuture.completedFuture

class JsonHttpClientTest {
val httpClient = mockk<HttpClient>()
val http = JsonHttpClient("http://some.host/v1", { setHeader("X-Custom-API", "123") }, http = httpClient)
private val httpClient = mockk<HttpClient>()
private val http = JsonHttpClient("http://some.host/v1", { setHeader("X-Custom-API", "123") }, http = httpClient)

@Test
fun get() {
Expand Down

0 comments on commit 99790eb

Please sign in to comment.