A gateway/firewall task to be able to talk someone about the real job
Fibinger Ádám
2021-10-22 fd7692eae00cbb0db3e6b732f68357e3c64a8a8b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
<?php
 
namespace App\Tests;
 
use ApiPlatform\Core\Bridge\Symfony\Bundle\Test\ApiTestCase;
use App\Entity\Secret;
 
class SecretServerTest extends ApiTestCase
{
    private $secretValidDtoFixture = ['secret' => 'Something very secretly created stuff', 'expireAfterViews' => 10, 'expireAfter' => 10];
 
    public function testDefaultCollectionRoutes(): void
    {
        $client = static::createClient();
 
        $client->request('GET', 'v1/secrets');
        $this->assertResponseStatusCodeSame(404);
 
        $client->request('GET', 'v1/secret/asdf');
        $this->assertResponseStatusCodeSame(404);
    }
 
    public function testInvalidSecretFormats()
    {
        $client = static::createClient();
 
        $invalidFixture = $this->secretValidDtoFixture;
        $invalidFixture['expireAfterViews'] = 'sajt';
 
        $client->request(
            'POST',
            'v1/secret',
            [
                'json' => $invalidFixture
            ]
        );
        //missing field, invalid field, etc
        $this->assertResponseStatusCodeSame(405);
 
        unset($invalidFixture['expireAfterViews']);
 
        $client->request(
            'POST',
            'v1/secret',
            [
                'json' => $invalidFixture
            ]
        );
        //missing field, invalid field, etc
        $this->assertResponseStatusCodeSame(405);
    }
 
    public function testValidSecretCreation()
    {
        $client = static::createClient();
 
        $response = $client->request(
            'POST',
            'v1/secret',
            [
                'json' => $this->secretValidDtoFixture
            ]
        );
        //should be created
        $this->assertResponseStatusCodeSame(201);
 
        $content = $response->getContent();
        $this->assertMatchesJsonSchema(['hash', 'secretText', 'createdAt', 'expiresAt', 'remainingViews']);
        $objectAsArray = json_decode($content, true);
 
        $this->assertEquals($this->secretValidDtoFixture['secret'], $objectAsArray['secretText']);
        $this->assertEquals($this->secretValidDtoFixture['expireAfterViews'], $objectAsArray['remainingViews']);
 
        $response = $client->request('GET', 'v1/secret/' . $objectAsArray['hash']);
 
        $this->assertResponseStatusCodeSame(200);
        $content = $response->getContent();
        //we haven't saw this in the previous endpoint, we saw the creation response, so it worth to do the same check
        $this->assertMatchesJsonSchema(['hash', 'secretText', 'createdAt', 'expiresAt', 'remainingViews']);
        $objectAsArray = json_decode($content, true);
 
        $this->assertEquals($this->secretValidDtoFixture['expireAfterViews'] - 1, $objectAsArray['remainingViews']);
    }
}