|
| 1 | +<?php |
| 2 | + |
| 3 | +use Dacastro4\LaravelGmail\GmailConnection; |
| 4 | +use Illuminate\Support\Facades\Storage; |
| 5 | +use Tests\TestCase; |
| 6 | + |
| 7 | +class TokenRepositoryTest extends TestCase |
| 8 | +{ |
| 9 | + private array $config; |
| 10 | + |
| 11 | + protected function setUp(): void |
| 12 | + { |
| 13 | + parent::setUp(); |
| 14 | + |
| 15 | + Storage::fake('local'); |
| 16 | + |
| 17 | + config(['app.key' => 'base64:'.base64_encode('testing_app_key_1234567890123456')]); |
| 18 | + |
| 19 | + $this->config = [ |
| 20 | + 'gmail.client_secret' => 'secret', |
| 21 | + 'gmail.client_id' => 'id', |
| 22 | + 'gmail.redirect_url' => '/callback', |
| 23 | + 'gmail.state' => null, |
| 24 | + 'gmail.scopes' => [], |
| 25 | + 'gmail.additional_scopes' => [], |
| 26 | + 'gmail.access_type' => 'offline', |
| 27 | + 'gmail.approval_prompt' => 'force', |
| 28 | + 'gmail.credentials_file_name' => 'gmail-json', |
| 29 | + 'gmail.allow_multiple_credentials' => false, |
| 30 | + 'gmail.allow_json_encrypt' => true, |
| 31 | + ]; |
| 32 | + } |
| 33 | + |
| 34 | + /** @test */ |
| 35 | + public function has_token_with_encryption(): void |
| 36 | + { |
| 37 | + $repository = new GmailConnection($this->config); |
| 38 | + |
| 39 | + $this->assertFalse($repository->checkPreviouslyLoggedIn()); |
| 40 | + |
| 41 | + $repository->saveAccessToken([ |
| 42 | + 'access_token' => 'token', |
| 43 | + 'refresh_token' => 'refresh', |
| 44 | + ]); |
| 45 | + |
| 46 | + $this->assertTrue($repository->checkPreviouslyLoggedIn()); |
| 47 | + } |
| 48 | + |
| 49 | + /** @test */ |
| 50 | + public function save_stores_encrypted_token(): void |
| 51 | + { |
| 52 | + $repository = new GmailConnection($this->config); |
| 53 | + |
| 54 | + $token = [ |
| 55 | + 'access_token' => 'token', |
| 56 | + 'refresh_token' => 'refresh', |
| 57 | + ]; |
| 58 | + |
| 59 | + $repository->saveAccessToken($token); |
| 60 | + |
| 61 | + $path = 'gmail/tokens/gmail-json.json'; |
| 62 | + $contents = Storage::disk('local')->get($path); |
| 63 | + |
| 64 | + $this->assertNotSame(json_encode($token + ['email' => null]), $contents); |
| 65 | + $this->assertSame($token + ['email' => null], json_decode(decrypt($contents), true)); |
| 66 | + } |
| 67 | + |
| 68 | + /** @test */ |
| 69 | + public function delete_removes_encrypted_token(): void |
| 70 | + { |
| 71 | + $repository = new GmailConnection($this->config); |
| 72 | + |
| 73 | + $repository->saveAccessToken([ |
| 74 | + 'access_token' => 'token', |
| 75 | + 'refresh_token' => 'refresh', |
| 76 | + ]); |
| 77 | + $this->assertTrue($repository->checkPreviouslyLoggedIn()); |
| 78 | + |
| 79 | + $repository->deleteAccessToken(); |
| 80 | + |
| 81 | + $this->assertFalse($repository->checkPreviouslyLoggedIn()); |
| 82 | + |
| 83 | + $path = 'gmail/tokens/gmail-json.json'; |
| 84 | + $this->assertSame([], json_decode(decrypt(Storage::disk('local')->get($path)), true)); |
| 85 | + } |
| 86 | +} |
0 commit comments