Files
openide/platform/credential-store-impl/test/CredentialStoreHelpers.kt
Roman Shevchenko ddaee68015 [project] giving the credential store implementation module a better name
GitOrigin-RevId: 07a288ca290595c57c0f92fb2c62893a93554a42
2024-06-24 22:28:04 +00:00

50 lines
1.7 KiB
Kotlin

// Copyright 2000-2024 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
@file:JvmName("CredentialStoreHelpers")
package com.intellij.credentialStore
import com.intellij.ide.passwordSafe.impl.BasePasswordSafe
import com.intellij.openapi.util.SystemInfo
import com.intellij.testFramework.UsefulTestCase
import com.intellij.testFramework.assertions.Assertions
import org.junit.Assume.assumeTrue
import java.util.*
internal fun assumeLocalMac() =
assumeTrue("The test needs macOS on a local PC but got: "+SystemInfo.getOsNameAndVersion(), SystemInfo.isMac && !UsefulTestCase.IS_UNDER_TEAMCITY)
internal fun assumeLocalLinux() =
assumeTrue("The test needs Linux on a local PC but got: "+SystemInfo.getOsNameAndVersion(), SystemInfo.isLinux && (
!UsefulTestCase.IS_UNDER_TEAMCITY || System.getenv("FORCE_CREDENTIALS_TEST") != null)
)
internal fun randomString() = UUID.randomUUID().toString()
internal fun doNullUsername(ps: BasePasswordSafe) {
val attributes = CredentialAttributes(randomString())
try {
ps.set(attributes, Credentials(null, "password"))
val saved = ps.get(attributes)!!
Assertions.assertThat(saved.userName).isNullOrEmpty()
Assertions.assertThat(saved.password).isEqualTo("password")
}
finally {
ps.set(attributes, null)
}
}
internal fun doErasePassword(ps: BasePasswordSafe) {
val attributes = CredentialAttributes(randomString())
try {
ps.set(attributes, Credentials("a", "b"))
ps.set(attributes, Credentials("a", null as String?))
val saved = ps.get(attributes)!!
Assertions.assertThat(saved.userName).isEqualTo("a")
Assertions.assertThat(saved.password).isNullOrEmpty()
}
finally {
ps.set(attributes, null)
}
}