This repository was archived by the owner on Jul 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow authentication with Google Accounts (#37)
* add flank auth task * service account credentials is not required anymore * update readme * run a spotlessApply to format code * credentials file convention must be declared in the extension * check that at least one of the two authentication methods are provided * Use InputFiles for credentials * set the environment variable only when the credentials file exists * restore code formatting not involving changed code * serviceAccountCredentials doesn't need to be optional * add another test case
- Loading branch information
1 parent
2d3c0ee
commit a4c4bdb
Showing
5 changed files
with
148 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
src/main/kotlin/io/github/flank/gradle/tasks/FlankAuthTask.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package io.github.flank.gradle.tasks | ||
|
||
import javax.inject.Inject | ||
import org.gradle.api.DefaultTask | ||
import org.gradle.api.file.ConfigurableFileCollection | ||
import org.gradle.api.tasks.Classpath | ||
import org.gradle.api.tasks.InputFiles | ||
import org.gradle.api.tasks.TaskAction | ||
import org.gradle.process.ExecOperations | ||
|
||
abstract class FlankAuthTask @Inject constructor(private val execOperations: ExecOperations) : | ||
DefaultTask() { | ||
@get:InputFiles @get:Classpath abstract val flankJarClasspath: ConfigurableFileCollection | ||
|
||
init { | ||
group = "flank" | ||
description = | ||
"Performs the authentication with a Google Account. https://flank.github.io/flank/#authenticate-with-a-google-account" | ||
} | ||
|
||
@TaskAction | ||
fun run() { | ||
execOperations | ||
.javaexec { | ||
classpath = flankJarClasspath | ||
mainClass.set("ftl.Main") | ||
args = listOf("auth", "login") | ||
} | ||
.assertNormalExitValue() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
import java.io.File | ||
import org.gradle.testkit.runner.TaskOutcome | ||
import org.junit.Rule | ||
import org.junit.Test | ||
import org.junit.rules.TemporaryFolder | ||
import strikt.api.expectThat | ||
import strikt.assertions.contains | ||
import strikt.gradle.testkit.output | ||
import strikt.gradle.testkit.taskPaths | ||
|
||
class CredentialsTest : GradleTest() { | ||
@get:Rule val userHomeDirectory = TemporaryFolder() | ||
|
||
@Test | ||
fun `When no service account or flank credentials are provided, build fails`() { | ||
projectFromResources("app") | ||
File(testProjectDir.root, "ftl-credentials.json").deleteRecursively() | ||
File(testProjectDir.root, "build.gradle.kts") | ||
.appendText("simpleFlank { projectId.set(\"my-project\") }") | ||
|
||
val build = gradleRunner("flankRun", "--stacktrace").forwardOutput().buildAndFail() | ||
|
||
expectThat(build) { | ||
output.contains( | ||
"Either a service account credential file should be provided or the flank authentication performed.") | ||
} | ||
expectThat(build) { taskPaths(TaskOutcome.FAILED).contains(":flankRunDebug") } | ||
} | ||
|
||
@Test | ||
fun `When service account is provided using the default file, build is successful`() { | ||
projectFromResources("app") | ||
|
||
val build = gradleRunner("flankRun", "--stacktrace").forwardOutput().build() | ||
|
||
expectThat(build) { | ||
output | ||
.not() | ||
.contains( | ||
"Either a service account credential file should be provided or the flank authentication performed.") | ||
taskPaths(TaskOutcome.SUCCESS).contains(":flankRunDebug") | ||
} | ||
} | ||
|
||
@Test | ||
fun `When service account is provided using the a custom file, build is successful`() { | ||
projectFromResources("app") | ||
|
||
val build = gradleRunner("flankRun", "--stacktrace").forwardOutput().build() | ||
File(testProjectDir.root, "ftl-credentials.json").deleteRecursively() | ||
File(testProjectDir.root, "custom-credentials.json") | ||
.appendText("{ \"project_id\": \"custom-project-id\" }") | ||
File(testProjectDir.root, "build.gradle.kts") | ||
.appendText("simpleFlank { credentialsFile.set(file(\"custom-credentials.json\")) }") | ||
|
||
expectThat(build) { | ||
output | ||
.not() | ||
.contains( | ||
"Either a service account credential file should be provided or the flank authentication performed.") | ||
taskPaths(TaskOutcome.SUCCESS).contains(":flankRunDebug") | ||
} | ||
} | ||
|
||
@Test | ||
fun `When flank Google Account authentication is provided, build is successful`() { | ||
projectFromResources("app") | ||
File(testProjectDir.root, "ftl-credentials.json").deleteRecursively() | ||
File(testProjectDir.root, "build.gradle.kts") | ||
.appendText("simpleFlank { projectId.set(\"my-project\") }") | ||
userHomeDirectory.root.resolve(".flank").mkdir() | ||
|
||
val build = | ||
gradleRunner("flankRun", "-Duser.home=${userHomeDirectory.root.path}", "--stacktrace") | ||
.forwardOutput() | ||
.build() | ||
|
||
expectThat(build) { | ||
output | ||
.not() | ||
.contains( | ||
"Either a service account credential file should be provided or the flank authentication performed.") | ||
taskPaths(TaskOutcome.SUCCESS).contains(":flankRunDebug") | ||
} | ||
} | ||
} |