Skip to content

Commit

Permalink
Update AuthenticatorAttachment tests
Browse files Browse the repository at this point in the history
  • Loading branch information
emlun committed Dec 16, 2022
1 parent c41bcdb commit 8c165d8
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonValue;
import java.util.Optional;
import java.util.stream.Stream;
import lombok.AllArgsConstructor;
import lombok.Getter;
Expand Down Expand Up @@ -73,18 +72,8 @@ public enum AuthenticatorAttachment {

@JsonValue @Getter @NonNull private final String value;

private static Optional<AuthenticatorAttachment> fromString(@NonNull String value) {
return Stream.of(values()).filter(v -> v.value.equals(value)).findAny();
}

@JsonCreator
private static AuthenticatorAttachment fromJsonString(@NonNull String value) {
return fromString(value)
.orElseThrow(
() ->
new IllegalArgumentException(
String.format(
"Unknown %s value: %s",
AuthenticatorAttachment.class.getSimpleName(), value)));
return Stream.of(values()).filter(v -> v.value.equals(value)).findAny().orElse(null);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,10 @@ class EnumsSpec

describe("AuthenticatorAttachment") {
describe("can be parsed from JSON") {
it("but throws IllegalArgumentException for unknown values.") {
val result = Try(
it("and ignores for unknown values.") {
val result =
json.readValue("\"foo\"", classOf[AuthenticatorAttachment])
)
result.failed.get.getCause shouldBe an[IllegalArgumentException]
result should be(null)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,13 @@ import com.yubico.webauthn.extension.appid.Generators._
import org.junit.runner.RunWith
import org.scalacheck.Arbitrary
import org.scalacheck.Arbitrary.arbitrary
import org.scalacheck.Gen
import org.scalatest.funspec.AnyFunSpec
import org.scalatest.matchers.should.Matchers
import org.scalatestplus.junit.JUnitRunner
import org.scalatestplus.scalacheck.ScalaCheckDrivenPropertyChecks

import scala.jdk.OptionConverters.RichOptional

@RunWith(classOf[JUnitRunner])
class JsonIoSpec
extends AnyFunSpec
Expand Down Expand Up @@ -351,15 +352,16 @@ class JsonIoSpec
)
}

it("allows and ignores an authenticatorAttachment attribute.") {
it(
"allows an authenticatorAttachment attribute, but ignores unknown values."
) {
def test[P <: PublicKeyCredential[_, _]](tpe: TypeReference[P])(implicit
a: Arbitrary[P]
): Unit = {
forAll(
a.arbitrary,
Gen.oneOf(
arbitrary[AuthenticatorAttachment].map(_.getValue),
arbitrary[String],
arbitrary[String].suchThat(s =>
!AuthenticatorAttachment.values.map(_.getValue).contains(s)
),
) { (value: P, authenticatorAttachment: String) =>
val tree: ObjectNode = json.valueToTree(value)
Expand All @@ -370,8 +372,37 @@ class JsonIoSpec
val encoded = json.writeValueAsString(tree)
println(authenticatorAttachment)
val decoded = json.readValue(encoded, tpe)
decoded.getAuthenticatorAttachment.asScala should be(None)
}

forAll(
a.arbitrary,
arbitrary[AuthenticatorAttachment],
) { (value: P, authenticatorAttachment: AuthenticatorAttachment) =>
val tree: ObjectNode = json.valueToTree(value)
tree.set(
"authenticatorAttachment",
new TextNode(authenticatorAttachment.getValue),
)
val encoded = json.writeValueAsString(tree)
println(authenticatorAttachment)
val decoded = json.readValue(encoded, tpe)

decoded.getAuthenticatorAttachment.asScala should equal(
Some(authenticatorAttachment)
)
}

forAll(
a.arbitrary
) { (value: P) =>
val tree: ObjectNode = json.valueToTree(
value.toBuilder.authenticatorAttachment(null).build()
)
val encoded = json.writeValueAsString(tree)
val decoded = json.readValue(encoded, tpe)

decoded should equal(value)
decoded.getAuthenticatorAttachment.asScala should be(None)
}
}

Expand Down

0 comments on commit 8c165d8

Please sign in to comment.