Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 32 additions & 55 deletions src/test/groovy/org/justserve/FulcrumCommandSpec.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,16 @@ package org.justserve

import io.micronaut.configuration.picocli.PicocliRunner
import io.micronaut.context.ApplicationContext
import io.micronaut.context.annotation.Value
import io.micronaut.context.env.Environment
import io.micronaut.test.extensions.spock.annotation.MicronautTest
import spock.lang.Shared
import spock.lang.Specification
import spock.lang.Unroll

import java.util.regex.Pattern

@MicronautTest()
class FulcrumCommandSpec extends Specification {
class FulcrumCommandSpec extends JustServeSpec {

@Shared
Pattern cliVersion

@Shared
String justServeUrl

@Shared
Pattern blankRegex = Pattern.compile "^\\s*\$"

Expand All @@ -35,68 +27,53 @@ class FulcrumCommandSpec extends Specification {


def setupSpec() {
justServeUrl = System.getenv("JUSTSERVE_URL") ?: "https://www.justserve.org"
def props = new Properties()
new File('gradle.properties').withInputStream { stream ->
props.load(stream)
}
cliVersion = Pattern.compile("^" + props.getProperty('justserveCliVersion') + "\\s*\$")
}

@Unroll("command with args: #args produces expected output when auth is set to #auth")
def "commands should behave correctly when JUSTSERVE_TOKEN is unset"() {
@Unroll("command with args: calling 'justserve #flag #email' works as expected")
def "commands to query temporary password should behave as expected with or without authentication"() {
when:
if (null != System.getenv("JUSTSERVE_TOKEN")) {
throw new IllegalStateException("JUSTSERVE_TOKEN is set. Do not define this variable in testing.")
}
ApplicationContext ctx
if (auth) {
ctx = ApplicationContext.builder()
.environments(Environment.CLI, Environment.TEST)
.properties([
"justserve.token": System.getenv("TEST_TOKEN"),
"micronaut.http.services.justserve.url" : justServeUrl
])
.build()
.start()
def (outputStream, errorStream) = executeCommand(context as ApplicationContext, [flag, email] as String[])

then:
if (context == noAuthCtx) {
verifyAll {
outputStream.toString().matches(blankRegex)
errorStream.toString().matches(tokenNotSetRegex)
}
} else if (userEmail.equalsIgnoreCase(email as String)) {
verifyAll {
outputStream.toString().matches(successRegex)
errorStream.toString().matches(blankRegex)
}
} else {
ctx = ApplicationContext
.builder()
.environments(Environment.CLI, Environment.TEST)
.environmentVariableExcludes("JUSTSERVE_TOKEN")
.properties(["micronaut.http.services.justserve.url": justServeUrl])
.build()
.start()
verifyAll {
outputStream.toString().matches(blankRegex)
errorStream.toString().matches(errorRegex)
}
}

and:
def (outputStream, errorStream) = executeCommand(ctx, args)
where:
[flag, email, context] << [['-e', '--email'], [userEmail, "notanemail@mail.moc"], [noAuthCtx, ctx]].combinations()
}

@Unroll
def "querying version returns current version, with or without authentication"() {
when:
def (outputStream, errorStream) = executeCommand(context as ApplicationContext, args as String[])

then:
verifyAll {
outputStream.toString().matches(expectedOutputValue)
errorStream.toString().matches(expectedErrorOutput)
outputStream.toString().matches(cliVersion)
errorStream.toString().matches(blankRegex)
}

and:
ctx.stop()

where:
args | expectedOutputValue | expectedErrorOutput | auth | _
new String[]{"-v"} | cliVersion | blankRegex | false | _
new String[]{"--version"} | cliVersion | blankRegex | false | _
new String[]{"version"} | cliVersion | blankRegex | false | _
new String[]{"-e", "jimmy@justserve.org"} | blankRegex | tokenNotSetRegex | false | _
new String[]{"--email", "jimmy@justserve.org"} | blankRegex | tokenNotSetRegex | false | _
new String[]{"-e", "notanemail@mail.moc"} | blankRegex | tokenNotSetRegex | false | _
new String[]{"--email", "notanemail@mail.moc"} | blankRegex | tokenNotSetRegex | false | _
new String[]{"-v"} | cliVersion | blankRegex | true | _
new String[]{"--version"} | cliVersion | blankRegex | true | _
new String[]{"version"} | cliVersion | blankRegex | true | _
new String[]{"-e", "jimmy@justserve.org"} | successRegex | blankRegex | true | _
new String[]{"--email", "jimmy@justserve.org"} | successRegex | blankRegex | true | _
new String[]{"-e", "notanemail@mail.moc"} | blankRegex | errorRegex | true | _
new String[]{"--email", "notanemail@mail.moc"} | blankRegex | errorRegex | true | _
[args, context] << [[['-v'], ['--version'], ['version']], [noAuthCtx, ctx]].combinations()
}

/**
Expand All @@ -108,7 +85,6 @@ class FulcrumCommandSpec extends Specification {
* @return An array containing the captured stdout (at index 0) and stderr (at index 1)
* as String representations of the output streams.
*/

String[] executeCommand(ApplicationContext ctx, String... args) {
OutputStream out = new ByteArrayOutputStream()
OutputStream err = new ByteArrayOutputStream()
Expand All @@ -117,4 +93,5 @@ class FulcrumCommandSpec extends Specification {
PicocliRunner.run(FulcrumCommand.class, ctx, args)
return new String[]{out, err}
}

}
49 changes: 49 additions & 0 deletions src/test/groovy/org/justserve/JustServeSpec.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package org.justserve

import io.micronaut.context.ApplicationContext
import io.micronaut.context.env.Environment
import io.micronaut.test.extensions.spock.annotation.MicronautTest
import spock.lang.Shared
import spock.lang.Specification

@MicronautTest()
class JustServeSpec extends Specification {

@Shared
ApplicationContext ctx, noAuthCtx

@Shared
String justServeUrl, userEmail

def setupSpec() {
justServeUrl = System.getenv("JUSTSERVE_URL") ?: "https://www.justserve.org"
if (justServeUrl.toLowerCase().contains("dev")) {
userEmail = "userc5f660c0-5c3a-42a8-bbe0-8dad54d00f42@fake.justserve.org"
} else {
userEmail = "jimmy@justserve.org"
}
if (null != System.getenv("JUSTSERVE_TOKEN")) {
throw new IllegalStateException("JUSTSERVE_TOKEN is set. Do not define this variable in testing.")
}
ctx = ApplicationContext.builder()
.environments(Environment.CLI, Environment.TEST)
.properties([
"justserve.token": System.getenv("TEST_TOKEN"),
"micronaut.http.services.justserve.url" : justServeUrl
])
.build()
.start()
noAuthCtx = ApplicationContext
.builder()
.environments(Environment.CLI, Environment.TEST)
.environmentVariableExcludes("JUSTSERVE_TOKEN")
.properties(["micronaut.http.services.justserve.url": justServeUrl])
.build()
.start()
}

void cleanupSpec() {
noAuthCtx.stop()
ctx.stop()
}
}
45 changes: 7 additions & 38 deletions src/test/groovy/org/justserve/client/UserClientSpec.groovy
Original file line number Diff line number Diff line change
@@ -1,55 +1,24 @@
package org.justserve.client

import io.micronaut.context.ApplicationContext

import io.micronaut.http.HttpResponse
import io.micronaut.http.HttpStatus
import io.micronaut.http.client.exceptions.HttpClientResponseException
import io.micronaut.runtime.server.EmbeddedServer
import org.justserve.JustServeSpec
import org.justserve.model.UserHashRequestByEmail
import spock.lang.Shared
import spock.lang.Specification

class UserClientSpec extends Specification {
class UserClientSpec extends JustServeSpec {

@Shared
ApplicationContext noAuthCtx
UserClient noAuthUserClient, userClient

@Shared
ApplicationContext ctx

@Shared
UserClient noAuthUserClient

@Shared
UserClient userClient

void setupSpec() {
def justServeUrl = System.getenv("JUSTSERVE_URL") ?: "https://www.justserve.org"
if (null != System.getenv("JUSTSERVE_TOKEN")) {
throw new IllegalStateException("JUSTSERVE_TOKEN is set. Do not define this variable in testing.")
}
noAuthCtx = ApplicationContext.builder(EmbeddedServer)
.environmentVariableExcludes("JUSTSERVE_TOKEN", "TEST_TOKEN")
.properties(["micronaut.http.services.justserve.url": justServeUrl])
.build()
.start()
def setupSpec() {
noAuthUserClient = noAuthCtx.getBean(UserClient)
ctx = ApplicationContext.builder(EmbeddedServer)
.environmentVariableExcludes("JUSTSERVE_TOKEN")
.properties([
"justserve.token" : System.getenv("TEST_TOKEN"),
"micronaut.http.services.justserve.url": justServeUrl
])
.build()
.start()
userClient = ctx.getBean(UserClient)
}

void cleanupSpec() {
noAuthCtx.stop()
ctx.stop()
}

def "get tempPassword for #email and "() {
when:
HttpResponse<String> response = null
Expand Down Expand Up @@ -77,9 +46,9 @@ class UserClientSpec extends Specification {

where:
expectedResponse | email | expectedException | expectedMessage | client | _
HttpStatus.OK | "jimmy@justserve.org" | null | null | userClient | _
HttpStatus.OK | userEmail | null | null | userClient | _
null | "notanemail@mail.moc" | HttpClientResponseException | "\"status\":500" | userClient | _
null | "jimmy@justserve.org" | HttpClientResponseException | "\"status\":401" | noAuthUserClient | _
null | userEmail | HttpClientResponseException | "\"status\":401" | noAuthUserClient | _
null | "notanemail@mail.moc" | HttpClientResponseException | "\"status\":401" | noAuthUserClient | _
}
}
Loading