package com.securityc4po.api.pentest

import com.github.tomakehurst.wiremock.common.Json
import com.securityc4po.api.BaseIntTest
import com.securityc4po.api.configuration.NP_NONNULL_FIELD_NOT_INITIALIZED_IN_CONSTRUCTOR
import com.securityc4po.api.configuration.RCN_REDUNDANT_NULLCHECK_OF_NONNULL_VALUE
import com.securityc4po.api.configuration.SIC_INNER_SHOULD_BE_STATIC
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings
import org.junit.jupiter.api.AfterEach
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Nested
import org.junit.jupiter.api.Test
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.web.server.LocalServerPort
import org.springframework.data.mongodb.core.MongoTemplate
import org.springframework.data.mongodb.core.query.Query
import org.springframework.test.web.reactive.server.WebTestClient
import java.time.Duration

@SuppressFBWarnings(
    SIC_INNER_SHOULD_BE_STATIC,
    NP_NONNULL_FIELD_NOT_INITIALIZED_IN_CONSTRUCTOR,
    RCN_REDUNDANT_NULLCHECK_OF_NONNULL_VALUE
)
class PentestControllerIntTest : BaseIntTest() {

    @LocalServerPort
    private var port = 0

    @Autowired
    lateinit var mongoTemplate: MongoTemplate

    @Autowired
    private lateinit var webTestClient: WebTestClient

    @BeforeEach
    fun setupWebClient() {
        webTestClient = WebTestClient.bindToServer()
            .baseUrl("http://localhost:$port")
            .responseTimeout(Duration.ofMillis(10000))
            .build()
    }

    @BeforeEach
    fun init() {
        configureAdminToken()
        persistBasicTestScenario()
    }

    @AfterEach
    fun destroy() {
        cleanUp()
    }

    @Nested
    inner class GetPentests {
        @Test
        fun `requesting pentests by projectId and category successfully`() {
            webTestClient.get()
                .uri("/pentests?projectId=d2e126ba-f608-11ec-b939-0242ac120002&category=INFORMATION_GATHERING")
                .header("Authorization", "Bearer $tokenAdmin")
                .exchange()
                .expectStatus().isOk
                .expectHeader().valueEquals("Application-Name", "SecurityC4PO")
                .expectBody().json(Json.write(getPentests()))
        }

        private val pentestOne = Pentest(
            id = "9c8af320-f608-11ec-b939-0242ac120002",
            projectId = "d2e126ba-f608-11ec-b939-0242ac120002",
            category = PentestCategory.INFORMATION_GATHERING,
            title = "Search engine discovery/reconnaissance",
            refNumber = "OTG-INFO-001",
            status = PentestStatus.NOT_STARTED,
            findingIds = emptyList(),
            commentIds = emptyList()
        )
        private val pentestTwo = Pentest(
            id = "43fbc63c-f624-11ec-b939-0242ac120002",
            projectId = "d2e126ba-f608-11ec-b939-0242ac120002",
            category = PentestCategory.INFORMATION_GATHERING,
            title = "Fingerprint Web Server",
            refNumber = "OTG-INFO-002",
            status = PentestStatus.REPORTED,
            findingIds = emptyList(),
            commentIds = emptyList()
        )

        private fun getPentests() = listOf(
            pentestOne.toPentestResponseBody(),
            pentestTwo.toPentestResponseBody()
        )
    }

    private fun persistBasicTestScenario() {
        // setup test data
        val pentestOne = Pentest(
            id = "9c8af320-f608-11ec-b939-0242ac120002",
            projectId = "d2e126ba-f608-11ec-b939-0242ac120002",
            category = PentestCategory.INFORMATION_GATHERING,
            title = "Search engine discovery/reconnaissance",
            refNumber = "OTG-INFO-001",
            status = PentestStatus.NOT_STARTED,
            findingIds = emptyList(),
            commentIds = emptyList()
        )
        val pentestTwo = Pentest(
            id = "43fbc63c-f624-11ec-b939-0242ac120002",
            projectId = "d2e126ba-f608-11ec-b939-0242ac120002",
            category = PentestCategory.INFORMATION_GATHERING,
            title = "Fingerprint Web Server",
            refNumber = "OTG-INFO-002",
            status = PentestStatus.REPORTED,
            findingIds = emptyList(),
            commentIds = emptyList()
        )
        val pentestThree = Pentest(
            id = "74eae112-f62c-11ec-b939-0242ac120002",
            projectId = "6fad3474-fc29-49f9-bd37-e039e9e60c18",
            category = PentestCategory.AUTHENTICATION_TESTING,
            title = "Testing for Credentials Transported over an Encrypted Channel",
            refNumber = "OTG-AUTHN-001",
            status = PentestStatus.CHECKED,
            findingIds = emptyList(),
            commentIds = emptyList()
        )
        // persist test data in database
        mongoTemplate.save(PentestEntity(pentestOne))
        mongoTemplate.save(PentestEntity(pentestTwo))
        mongoTemplate.save(PentestEntity(pentestThree))
    }

    private fun configureAdminToken() {
        tokenAdmin = getAccessToken("test_admin", "test", "c4po_local", "c4po_realm_local")
    }

    private fun cleanUp() {
        mongoTemplate.findAllAndRemove(Query(), PentestEntity::class.java)

        tokenAdmin = "n/a"
    }
}