From 55ec97e33223c86176102edc15a2e6fd224c1ff0 Mon Sep 17 00:00:00 2001 From: Bubka <858858+Bubka@users.noreply.github.com> Date: Tue, 17 Nov 2020 15:47:15 +0100 Subject: [PATCH] Fix TwoFAccount model unit tests to match its new definition --- tests/Unit/TwoFAccountTest.php | 196 +++++++++++++++++++++++++++------ 1 file changed, 162 insertions(+), 34 deletions(-) diff --git a/tests/Unit/TwoFAccountTest.php b/tests/Unit/TwoFAccountTest.php index b5e1c189..cf223a43 100644 --- a/tests/Unit/TwoFAccountTest.php +++ b/tests/Unit/TwoFAccountTest.php @@ -38,10 +38,9 @@ class TwoFAccountTest extends TestCase 'service' => 'testTOTP', 'account' => 'test@test.com', 'uri' => 'otpauth://totp/test@test.com?secret=A4GRFHVVRBGY7UIW&issuer=test', - 'icon' => 'test.png', + 'icon' => 'test.png' ]); - $response = $this->actingAs($this->user, 'api') ->json('GET', '/api/twofaccounts/' . $twofaccount->id) ->assertStatus(200) @@ -49,9 +48,43 @@ class TwoFAccountTest extends TestCase 'service' => 'testTOTP', 'account' => 'test@test.com', 'icon' => 'test.png', + 'group_id' => null, + 'isConsistent' => true, + 'otpType' => 'totp', + 'digits' => 6, + 'totpPeriod' => 30, + 'hotpCounter' => null, + 'imageLink' => null, ]) ->assertJsonMissing([ 'uri' => 'otpauth://totp/test@test.com?secret=A4GRFHVVRBGY7UIW&issuer=test', + 'secret' => 'A4GRFHVVRBGY7UIW', + 'algorithm' => 'sha1', + ]); + } + + + /** + * test TwoFAccount display via API + * + * @test + */ + public function testTwoFAccountDisplayWithSensitive() + { + $twofaccount = factory(TwoFAccount::class)->create([ + 'service' => 'testTOTP', + 'account' => 'test@test.com', + 'uri' => 'otpauth://totp/test@test.com?secret=A4GRFHVVRBGY7UIW', + ]); + + + $response = $this->actingAs($this->user, 'api') + ->json('GET', '/api/twofaccounts/' . $twofaccount->id . '/withSensitive') + ->assertStatus(200) + ->assertJsonFragment([ + 'uri' => 'otpauth://totp/test@test.com?secret=A4GRFHVVRBGY7UIW', + 'secret' => 'A4GRFHVVRBGY7UIW', + 'algorithm' => 'sha1', ]); } @@ -132,11 +165,11 @@ class TwoFAccountTest extends TestCase /** - * test otpType is null in case of invalid uri via API + * test show account when uri field remains encrypted via API * * @test */ - public function testOtpTypeIsNullForAccountWithInvalidUri() + public function testShowAccountWithUndecipheredUri() { $response = $this->actingAs($this->user, 'api') ->json('POST', '/api/twofaccounts', [ @@ -150,54 +183,88 @@ class TwoFAccountTest extends TestCase DB::table('twofaccounts') ->where('id', 1) ->update([ - 'uri' => 'iCanHasCheeseBurger', + 'uri' => '**encrypted**', ]); $response = $this->actingAs($this->user, 'api') ->json('GET', '/api/twofaccounts/1') - ->assertStatus(200) - ->assertJsonFragment([ - 'otpType' => null, - ]); + ->assertStatus(422); } /** - * test TOTP generation for a given existing account via API + * test token generation for a given existing account via API * * @test */ - public function testTOTPgenerationWithProvidedAccountId() + public function testTokenGenerationWithAccountId() { $twofaccount = factory(TwoFAccount::class)->create([ - 'service' => 'testTOTP', - 'account' => 'test@test.com', - 'uri' => 'otpauth://totp/test@test.com?secret=A4GRFHVVRBGY7UIW&issuer=test' + 'service' => 'testService', + 'account' => 'testAccount', + 'uri' => 'otpauth://totp/testService:testAccount?secret=A4GRFHVVRBGY7UIW&issuer=testService' ]); $response = $this->actingAs($this->user, 'api') - ->json('POST', '/api/twofaccounts/otp', ['data' => $twofaccount->id]) + ->json('POST', '/api/twofaccounts/otp', ['id' => $twofaccount->id]) ->assertStatus(200) ->assertJsonStructure([ - 'otp', + 'token', + 'totpTimestamp' ]); } /** - * test TOTP generation as preview via API + * test token generation by providing an URI via API * * @test */ - public function testTOTPgenerationPreview() + public function testTokenGenerationWithUri() { - $uri = 'otpauth://totp/test@test.com?secret=A4GRFHVVRBGY7UIW&issuer=test'; + $uri = 'otpauth://totp/service:account?secret=A4GRFHVVRBGY7UIW&issuer=service'; $response = $this->actingAs($this->user, 'api') - ->json('POST', '/api/twofaccounts/otp', ['data' => $uri]) + ->json('POST', '/api/twofaccounts/otp', ['otp' => ['uri' => $uri]]) ->assertStatus(200) + ->assertJsonFragment([ + 'service' => 'service', + 'account' => 'account', + ]) ->assertJsonStructure([ - 'otp', + 'token', + 'totpTimestamp' + ]); + } + + + /** + * test token generation by providing an array of otp attributes without URI via API + * + * @test + */ + public function testTokenGenerationWithAttributesArray() + { + $response = $this->actingAs($this->user, 'api') + ->json('POST', '/api/twofaccounts/otp', ['otp' => [ + 'service' => 'service', + 'account' => 'account', + 'otpType' => 'totp', + 'secret' => 'A4GRFHVVRBGY7UIW', + 'secretIsBase32Encoded' => 1, + 'digits' => 6, + 'totpPeriod' => 30, + 'algorithm' => 'sha1', + 'uri' => '' + ]]) + ->assertStatus(200) + ->assertJsonFragment([ + 'service' => 'service', + 'account' => 'account', + ]) + ->assertJsonStructure([ + 'token', + 'totpTimestamp' ]); } @@ -213,19 +280,44 @@ class TwoFAccountTest extends TestCase $response = $this->actingAs($this->user, 'api') ->json('PUT', '/api/twofaccounts/' . $twofaccount->id, [ - 'service' => 'testUpdate', - 'account' => 'testUpdate@test.com', + 'service' => 'service', + 'account' => 'account', 'icon' => 'testUpdate.png', + 'otpType' => 'totp', + 'secret' => 'A4GRFHVVRBGY7UIW', + 'secretIsBase32Encoded' => 1, + 'digits' => 8, + 'totpPeriod' => 40, + 'algorithm' => 'sha256', + 'uri' => '', + 'imageLink' => 'http://www.image.net/file.png' ]) ->assertStatus(200) ->assertJsonFragment([ 'id' => 1, - 'service' => 'testUpdate', - 'account' => 'testUpdate@test.com', + 'service' => 'service', + 'account' => 'account', 'icon' => 'testUpdate.png', + 'otpType' => 'totp', + 'digits' => 8, + 'totpPeriod' => 40, + 'imageLink' => 'http://www.image.net/file.png' ]) ->assertJsonMissing([ 'uri' => $twofaccount->uri, + 'secret' => 'A4GRFHVVRBGY7UIW', + 'algorithm' => 'sha256', + ]); + + $response = $this->actingAs($this->user, 'api') + ->json('GET', '/api/twofaccounts/' . $twofaccount->id . '/withSensitive') + ->assertStatus(200) + ->assertJsonFragment([ + 'secret' => 'A4GRFHVVRBGY7UIW', + 'algorithm' => 'sha256', + ]) + ->assertJsonStructure([ + 'uri', ]); } @@ -238,9 +330,9 @@ class TwoFAccountTest extends TestCase public function testTwoFAccountHOTPUpdate() { $twofaccount = factory(TwoFAccount::class)->create([ - 'service' => 'test.com', - 'account' => 'test', - 'uri' => 'otpauth://hotp/service?counter=1&secret=A4GRFHVVRBGY7UIW' + 'service' => 'service', + 'account' => 'account', + 'uri' => 'otpauth://hotp/service:account?counter=1&secret=A4GRFHVVRBGY7UIW' ]); $response = $this->actingAs($this->user, 'api') @@ -248,7 +340,14 @@ class TwoFAccountTest extends TestCase 'service' => 'testUpdate.com', 'account' => 'testUpdate', 'icon' => 'testUpdate.png', - 'counter' => 5 + 'otpType' => 'hotp', + 'secret' => 'BBBBFFFFEEEEAAAA', + 'secretIsBase32Encoded' => 1, + 'digits' => 8, + 'hotpCounter' => 5, + 'algorithm' => 'sha256', + 'uri' => '', + 'imageLink' => 'http://www.image.net/file.png' ]) ->assertStatus(200) ->assertJsonFragment([ @@ -256,7 +355,26 @@ class TwoFAccountTest extends TestCase 'service' => 'testUpdate.com', 'account' => 'testUpdate', 'icon' => 'testUpdate.png', - 'counter' => 5, + 'otpType' => 'hotp', + 'digits' => 8, + 'hotpCounter' => 5, + 'imageLink' => 'http://www.image.net/file.png' + ]) + ->assertJsonMissing([ + 'uri' => $twofaccount->uri, + 'secret' => 'BBBBFFFFEEEEAAAA', + 'algorithm' => 'sha256', + ]); + + $response = $this->actingAs($this->user, 'api') + ->json('GET', '/api/twofaccounts/' . $twofaccount->id . '/withSensitive') + ->assertStatus(200) + ->assertJsonFragment([ + 'secret' => 'BBBBFFFFEEEEAAAA', + 'algorithm' => 'sha256', + ]) + ->assertJsonStructure([ + 'uri', ]); } @@ -274,8 +392,17 @@ class TwoFAccountTest extends TestCase $response = $this->actingAs($this->user, 'api') ->json('PUT', '/api/twofaccounts/' . $id, [ - 'service' => 'testUpdate', - 'icon' => 'name.png' + 'service' => 'testUpdate.com', + 'account' => 'testUpdate', + 'icon' => 'testUpdate.png', + 'otpType' => 'hotp', + 'secret' => 'BBBBFFFFEEEEAAAA', + 'secretIsBase32Encoded' => 1, + 'digits' => 8, + 'hotpCounter' => 5, + 'algorithm' => 'sha256', + 'uri' => '', + 'imageLink' => 'http://www.image.net/file.png' ]) ->assertStatus(404); } @@ -288,7 +415,7 @@ class TwoFAccountTest extends TestCase */ public function testTwoFAccountIndexListing() { - factory(TwoFAccount::class, 3)->create(); + $twofaccount = factory(TwoFAccount::class, 3)->create(); $response = $this->actingAs($this->user, 'api') ->json('GET', '/api/twofaccounts') @@ -301,7 +428,8 @@ class TwoFAccountTest extends TestCase 'account', 'icon', 'created_at', - 'updated_at' + 'updated_at', + 'isConsistent' ] ] );