<commit_before>// Copyright 2000-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package git4idea.remote

import com.intellij.openapi.components.service
import com.intellij.util.UriUtil
import com.intellij.util.io.URLUtil
import git4idea.checkout.GitCheckoutProvider
import git4idea.commands.GitHttpAuthService
import git4idea.commands.GitHttpAuthenticator
import git4idea.config.GitVersion
import git4idea.test.GitHttpAuthTestService
import git4idea.test.GitPlatformTest
import org.assertj.core.api.Assertions.assertThat
import java.util.concurrent.CountDownLatch
import java.util.concurrent.TimeUnit

class GitRemoteTest : GitPlatformTest() {

  private lateinit var authenticator: TestAuthenticator
  private lateinit var authTestService: GitHttpAuthTestService

  private lateinit var host: String
  private lateinit var token: String

  override fun setUp() {
    super.setUp()

    host = System.getenv("idea.test.github.host")
    token = System.getenv("idea.test.github.token1")

    authenticator = TestAuthenticator()
    authTestService = service<GitHttpAuthService>() as GitHttpAuthTestService
    authTestService.register(authenticator)
  }

  private fun makeUrl(): String {
    return "${host}/testmaster/$PROJECT_NAME.git"
  }

  private fun makeUrlWithUsername(): String {
    return UriUtil.splitScheme(host).let {
      val scheme = it.first
      if (scheme.isNotEmpty()) it.first + URLUtil.SCHEME_SEPARATOR + USERNAME + "@" + it.second + "/testmaster/" + PROJECT_NAME + ".git"
      else USERNAME + "@" + it.second + "/testmaster/" + PROJECT_NAME + ".git"
    }
  }

  override fun tearDown() {
    try {
      authTestService.cleanup()
    }
    finally {
      super.tearDown()
    }
  }

  override fun hasRemoteGitOperation() = true

  fun `test clone from http with username`() {
    val cloneWaiter = cloneOnPooledThread(makeUrlWithUsername())

    assertPasswordAsked()
    authenticator.supplyPassword(token)

    assertCloneSuccessful(cloneWaiter)
  }

  fun `test clone from http without username`() {
    val cloneWaiter = cloneOnPooledThread(makeUrl())

    assertUsernameAsked()
    authenticator.supplyUsername(USERNAME)
    assertPasswordAsked()
    authenticator.supplyPassword(token)

    assertCloneSuccessful(cloneWaiter)
  }

  fun `test clone fails if incorrect password`() {
    val url = makeUrlWithUsername()

    val cloneWaiter = cloneOnPooledThread(url)

    assertPasswordAsked()
    authenticator.supplyPassword("incorrect")

    assertTrue("Clone didn't complete during the reasonable period of time", cloneWaiter.await(30, TimeUnit.SECONDS))
    assertThat(testNioRoot.resolve(PROJECT_NAME)).describedAs("Repository directory shouldn't be created").doesNotExist()

    val gitVersion = vcs.version
    val expectedAuthFailureMessage = when {
      gitVersion.isLaterOrEqual(GitVersion(2, 22, 0, 0)) -> {
        "Authentication failed for '${makeUrl()}/'"
      }
      gitVersion.isLaterOrEqual(GitVersion(1, 8, 3, 0)) -> {
        "Authentication failed for '$url/'"
      }
      else -> {<commit_msg>git: fix tests after escaping changes in GitCheckoutProvider<commit_after>// Copyright 2000-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package git4idea.remote

import com.intellij.openapi.components.service
import com.intellij.openapi.util.text.StringUtil
import com.intellij.util.UriUtil
import com.intellij.util.io.URLUtil
import com.intellij.xml.util.XmlStringUtil
import git4idea.checkout.GitCheckoutProvider
import git4idea.commands.GitHttpAuthService
import git4idea.commands.GitHttpAuthenticator
import git4idea.config.GitVersion
import git4idea.test.GitHttpAuthTestService
import git4idea.test.GitPlatformTest
import org.assertj.core.api.Assertions.assertThat
import java.util.concurrent.CountDownLatch
import java.util.concurrent.TimeUnit

class GitRemoteTest : GitPlatformTest() {

  private lateinit var authenticator: TestAuthenticator
  private lateinit var authTestService: GitHttpAuthTestService

  private lateinit var host: String
  private lateinit var token: String

  override fun setUp() {
    super.setUp()

    host = System.getenv("idea.test.github.host")
    token = System.getenv("idea.test.github.token1")

    authenticator = TestAuthenticator()
    authTestService = service<GitHttpAuthService>() as GitHttpAuthTestService
    authTestService.register(authenticator)
  }

  private fun makeUrl(): String {
    return "${host}/testmaster/$PROJECT_NAME.git"
  }

  private fun makeUrlWithUsername(): String {
    return UriUtil.splitScheme(host).let {
      val scheme = it.first
      if (scheme.isNotEmpty()) it.first + URLUtil.SCHEME_SEPARATOR + USERNAME + "@" + it.second + "/testmaster/" + PROJECT_NAME + ".git"
      else USERNAME + "@" + it.second + "/testmaster/" + PROJECT_NAME + ".git"
    }
  }

  override fun tearDown() {
    try {
      authTestService.cleanup()
    }
    finally {
      super.tearDown()
    }
  }

  override fun hasRemoteGitOperation() = true

  fun `test clone from http with username`() {
    val cloneWaiter = cloneOnPooledThread(makeUrlWithUsername())

    assertPasswordAsked()
    authenticator.supplyPassword(token)

    assertCloneSuccessful(cloneWaiter)
  }

  fun `test clone from http without username`() {
    val cloneWaiter = cloneOnPooledThread(makeUrl())

    assertUsernameAsked()
    authenticator.supplyUsername(USERNAME)
    assertPasswordAsked()
    authenticator.supplyPassword(token)

    assertCloneSuccessful(cloneWaiter)
  }

  fun `test clone fails if incorrect password`() {
    val url = makeUrlWithUsername()

    val cloneWaiter = cloneOnPooledThread(url)

    assertPasswordAsked()
    authenticator.supplyPassword("incorrect")

    assertTrue("Clone didn't complete during the reasonable period of time", cloneWaiter.await(30, TimeUnit.SECONDS))
    assertThat(testNioRoot.resolve(PROJECT_NAME)).describedAs("Repository directory shouldn't be created").doesNotExist()

    val gitVersion = vcs.version
    val expectedAuthFailureMessage = when {
      gitVersion.isLaterOrEqual(GitVersion(2, 22, 0, 0)) -> {
        StringUtil.escapeXmlEntities("Authentication failed for '${makeUrl()}/'")
      }
      gitVersion.isLaterOrEqual(GitVersion(1, 8, 3, 0)) -> {
        StringUtil.escapeXmlEntities("Authentication failed for '$url/'")
      }
      else -> {
{'fraction_non_alphanumeric': 0.0971985760718155, 'fraction_numerical': 0.008976938554403343, 'mean_word_length': 4.16546762589928, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 1}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5932687', 'n_tokens_mistral': 1992, 'n_tokens_neox': 1884, 'n_words': 435}