Skip to content

Commit 8c165d8

Browse files
committed
Update AuthenticatorAttachment tests
1 parent c41bcdb commit 8c165d8

File tree

3 files changed

+41
-22
lines changed

3 files changed

+41
-22
lines changed

webauthn-server-core/src/main/java/com/yubico/webauthn/data/AuthenticatorAttachment.java

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626

2727
import com.fasterxml.jackson.annotation.JsonCreator;
2828
import com.fasterxml.jackson.annotation.JsonValue;
29-
import java.util.Optional;
3029
import java.util.stream.Stream;
3130
import lombok.AllArgsConstructor;
3231
import lombok.Getter;
@@ -73,18 +72,8 @@ public enum AuthenticatorAttachment {
7372

7473
@JsonValue @Getter @NonNull private final String value;
7574

76-
private static Optional<AuthenticatorAttachment> fromString(@NonNull String value) {
77-
return Stream.of(values()).filter(v -> v.value.equals(value)).findAny();
78-
}
79-
8075
@JsonCreator
8176
private static AuthenticatorAttachment fromJsonString(@NonNull String value) {
82-
return fromString(value)
83-
.orElseThrow(
84-
() ->
85-
new IllegalArgumentException(
86-
String.format(
87-
"Unknown %s value: %s",
88-
AuthenticatorAttachment.class.getSimpleName(), value)));
77+
return Stream.of(values()).filter(v -> v.value.equals(value)).findAny().orElse(null);
8978
}
9079
}

webauthn-server-core/src/test/scala/com/yubico/webauthn/data/EnumsSpec.scala

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,10 @@ class EnumsSpec
6161

6262
describe("AuthenticatorAttachment") {
6363
describe("can be parsed from JSON") {
64-
it("but throws IllegalArgumentException for unknown values.") {
65-
val result = Try(
64+
it("and ignores for unknown values.") {
65+
val result =
6666
json.readValue("\"foo\"", classOf[AuthenticatorAttachment])
67-
)
68-
result.failed.get.getCause shouldBe an[IllegalArgumentException]
67+
result should be(null)
6968
}
7069
}
7170
}

webauthn-server-core/src/test/scala/com/yubico/webauthn/data/JsonIoSpec.scala

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,13 @@ import com.yubico.webauthn.extension.appid.Generators._
4141
import org.junit.runner.RunWith
4242
import org.scalacheck.Arbitrary
4343
import org.scalacheck.Arbitrary.arbitrary
44-
import org.scalacheck.Gen
4544
import org.scalatest.funspec.AnyFunSpec
4645
import org.scalatest.matchers.should.Matchers
4746
import org.scalatestplus.junit.JUnitRunner
4847
import org.scalatestplus.scalacheck.ScalaCheckDrivenPropertyChecks
4948

49+
import scala.jdk.OptionConverters.RichOptional
50+
5051
@RunWith(classOf[JUnitRunner])
5152
class JsonIoSpec
5253
extends AnyFunSpec
@@ -351,15 +352,16 @@ class JsonIoSpec
351352
)
352353
}
353354

354-
it("allows and ignores an authenticatorAttachment attribute.") {
355+
it(
356+
"allows an authenticatorAttachment attribute, but ignores unknown values."
357+
) {
355358
def test[P <: PublicKeyCredential[_, _]](tpe: TypeReference[P])(implicit
356359
a: Arbitrary[P]
357360
): Unit = {
358361
forAll(
359362
a.arbitrary,
360-
Gen.oneOf(
361-
arbitrary[AuthenticatorAttachment].map(_.getValue),
362-
arbitrary[String],
363+
arbitrary[String].suchThat(s =>
364+
!AuthenticatorAttachment.values.map(_.getValue).contains(s)
363365
),
364366
) { (value: P, authenticatorAttachment: String) =>
365367
val tree: ObjectNode = json.valueToTree(value)
@@ -370,8 +372,37 @@ class JsonIoSpec
370372
val encoded = json.writeValueAsString(tree)
371373
println(authenticatorAttachment)
372374
val decoded = json.readValue(encoded, tpe)
375+
decoded.getAuthenticatorAttachment.asScala should be(None)
376+
}
377+
378+
forAll(
379+
a.arbitrary,
380+
arbitrary[AuthenticatorAttachment],
381+
) { (value: P, authenticatorAttachment: AuthenticatorAttachment) =>
382+
val tree: ObjectNode = json.valueToTree(value)
383+
tree.set(
384+
"authenticatorAttachment",
385+
new TextNode(authenticatorAttachment.getValue),
386+
)
387+
val encoded = json.writeValueAsString(tree)
388+
println(authenticatorAttachment)
389+
val decoded = json.readValue(encoded, tpe)
390+
391+
decoded.getAuthenticatorAttachment.asScala should equal(
392+
Some(authenticatorAttachment)
393+
)
394+
}
395+
396+
forAll(
397+
a.arbitrary
398+
) { (value: P) =>
399+
val tree: ObjectNode = json.valueToTree(
400+
value.toBuilder.authenticatorAttachment(null).build()
401+
)
402+
val encoded = json.writeValueAsString(tree)
403+
val decoded = json.readValue(encoded, tpe)
373404

374-
decoded should equal(value)
405+
decoded.getAuthenticatorAttachment.asScala should be(None)
375406
}
376407
}
377408

0 commit comments

Comments
 (0)