From 27a42346342ef54641641b75b456436c6c126741 Mon Sep 17 00:00:00 2001 From: Roland Gruber Date: Sat, 21 Dec 2019 14:18:03 +0100 Subject: [PATCH] added unit tests --- lam/lib/webauthn.inc | 2 +- lam/tests/lib/webauthnTest.php | 123 +++++++++++++++++++++++++++++++++ 2 files changed, 124 insertions(+), 1 deletion(-) create mode 100644 lam/tests/lib/webauthnTest.php diff --git a/lam/lib/webauthn.inc b/lam/lib/webauthn.inc index 1281c74f..f672a14f 100644 --- a/lam/lib/webauthn.inc +++ b/lam/lib/webauthn.inc @@ -370,7 +370,7 @@ class PublicKeyCredentialSourceRepositorySQLite implements PublicKeyCredentialSo * * @return string the PDO URL */ - protected function getPdoUrl() { + public function getPdoUrl() { $fileName = dirname(__FILE__) . '/../config/__lam.webauthn.sqlite'; if (!file_exists($fileName)) { $handle = fopen($fileName, 'w'); diff --git a/lam/tests/lib/webauthnTest.php b/lam/tests/lib/webauthnTest.php new file mode 100644 index 00000000..687f5f59 --- /dev/null +++ b/lam/tests/lib/webauthnTest.php @@ -0,0 +1,123 @@ +database = $this + ->getMockBuilder(PublicKeyCredentialSourceRepositorySQLite::class) + ->setMethods(array('getPdoUrl')) + ->getMock(); + $file = tmpfile(); + $filePath = stream_get_meta_data($file)['uri']; + $this->database->method('getPdoUrl')->willReturn('sqlite:' . $filePath); + } + + /** + * Empty DB test + */ + public function test_findOneByCredentialId_emptyDb() { + $result = $this->database->findOneByCredentialId("test"); + $this->assertNull($result); + } + + /** + * Empty DB test + */ + public function test_findAllForUserEntity_emptyDb() { + $entity = new PublicKeyCredentialUserEntity("cn=test,dc=example", "cn=test,dc=example", "test", null); + + $result = $this->database->findAllForUserEntity($entity); + $this->assertEmpty($result); + } + + /** + * Save multiple credentials and read them. + */ + public function test_saveCredentialSource() { + $source1 = new PublicKeyCredentialSource( + "id1", + PublicKeyCredentialDescriptor::CREDENTIAL_TYPE_PUBLIC_KEY, + array(), + "atype", + new CertificateTrustPath(array('x5c' => 'test')), + \Ramsey\Uuid\Uuid::uuid1(), + "p1", + "uh1", + 1); + $this->database->saveCredentialSource($source1); + $source2 = new PublicKeyCredentialSource( + "id2", + PublicKeyCredentialDescriptor::CREDENTIAL_TYPE_PUBLIC_KEY, + array(), + "atype", + new CertificateTrustPath(array('x5c' => 'test')), + \Ramsey\Uuid\Uuid::uuid1(), + "p2", + "uh1", + 1); + $this->database->saveCredentialSource($source2); + $source3 = new PublicKeyCredentialSource( + "id3", + PublicKeyCredentialDescriptor::CREDENTIAL_TYPE_PUBLIC_KEY, + array(), + "atype", + new CertificateTrustPath(array('x5c' => 'test')), + \Ramsey\Uuid\Uuid::uuid1(), + "p3", + "uh2", + 1); + $this->database->saveCredentialSource($source3); + + $this->assertNotNull($this->database->findOneByCredentialId("id1")); + $this->assertNotNull($this->database->findOneByCredentialId("id2")); + $this->assertNotNull($this->database->findOneByCredentialId("id3")); + $this->assertEquals(2, sizeof( + $this->database->findAllForUserEntity(new PublicKeyCredentialUserEntity("uh1", "uh1", "uh1", null)) + )); + $this->assertEquals(1, sizeof( + $this->database->findAllForUserEntity(new PublicKeyCredentialUserEntity("uh2", "uh2", "uh2", null)) + )); + } + +} +