Passed
Push — main ( 1fbbbe...fd4d73 )
by Michiel
16:27 queued 12:04
created

test_select_raa_authorization()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 4
rs 10
1
<?php
2
3
/**
4
 * Copyright 2014 SURFnet bv
5
 *
6
 * Licensed under the Apache License, Version 2.0 (the "License");
7
 * you may not use this file except in compliance with the License.
8
 * You may obtain a copy of the License at
9
 *
10
 *     http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing, software
13
 * distributed under the License is distributed on an "AS IS" BASIS,
14
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
 * See the License for the specific language governing permissions and
16
 * limitations under the License.
17
 */
18
19
namespace Surfnet\StepupMiddleware\Test\Database;
20
21
use Surfnet\Stepup\Identity\Collection\InstitutionCollection;
22
use Surfnet\Stepup\Identity\Value\IdentityId;
23
use Surfnet\Stepup\Identity\Value\Institution;
24
use Surfnet\Stepup\Identity\Value\RegistrationAuthorityRole;
25
use Surfnet\StepupMiddleware\ApiBundle\Identity\Repository\AuthorizationRepository;
26
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
27
28
/**
29
 * Test the AuthorizationRepository.
30
 *
31
 * This repo is responsible for determining great portions of the FGA authorizations.
32
 * Having that repository code (DQL) under test will greatly decrease the chance of
33
 * regressions in that area.
34
 *
35
 * Tests in this file are based of a database dump from test2, a quite representative
36
 * data set was captured there with many FGA scenarios covered.
37
 */
38
class AuthorizationRepositoryMatrixTest extends KernelTestCase
39
{
40
    /**
41
     * @var AuthorizationRepository
42
     */
43
    private $authzRepository;
44
45
    public function authorizationMatrix()
46
    {
47
        $ra = RegistrationAuthorityRole::ra();
48
        $raa = RegistrationAuthorityRole::raa();
49
        // The uuids match those of the `Fixtures/test2.sql` data
50
        $aRa = new IdentityId('eff4d3bc-bbe9-45d4-b80d-080bc7e06615'); // pieter-a-ra
51
        $aRaa = new IdentityId('947da709-185b-4d9a-ba49-0a22d99dceb3'); // michiel-a-raa (only raa in institution-a)
52
        $avRaa = new IdentityId('cccfece4-e5e5-40b7-9aa4-a800d7cd3633'); // pieter-a-raa (raa in inst-a and inst-v)
53
        return [
54
            'RA from inst-a should have RA rights in inst-a+f' => [$ra, $aRa, ['institution-a.nl', 'institution-f.nl']],
55
            'RA from inst-a should not have RAA rights in inst-a+f' => [$raa, $aRa, []],
56
            'RAA from inst-a should have RA rights in inst-a+f' => [$ra, $aRaa, ['institution-a.nl', 'institution-f.nl']],
57
            'RAA from inst-a should have RAA rights in inst-a+f' => [$raa, $aRaa, ['institution-a.nl', 'institution-f.nl']],
58
            'RAA from inst-a+v should have RA rights in inst-a+f+i' => [$ra, $avRaa, ['institution-a.nl', 'institution-f.nl', 'institution-i.nl']],
59
            'RAA from inst-a+v should have RAA rights in inst-a+f+i' => [$raa, $avRaa, ['institution-a.nl', 'institution-f.nl', 'institution-i.nl']],
60
        ];
61
    }
62
63
    public function selectRaaMatrix()
64
    {
65
        $aRaa = new IdentityId('cccfece4-e5e5-40b7-9aa4-a800d7cd3633'); // Raa @ institution A
66
        $ghRaa = new IdentityId('02b70719-243f-4c7d-8649-48952a816ddf'); // RAA @ institution H
67
68
        return [
69
            'RAA inst-a => select_raa @ inst-a' => [$aRaa, ['institution-a.nl']],
70
            'RAA inst-h => select_raa @ inst-h+g' => [$ghRaa, ['institution-g.nl', 'institution-h.nl']],
71
        ];
72
    }
73
74
    protected function setUp(): void
75
    {
76
        $kernel = self::bootKernel();
77
        $manager = $kernel->getContainer()
78
            ->get('doctrine')
79
            ->getManager();
80
81
        $this->authzRepository = $kernel->getContainer()->get(AuthorizationRepository::class);
82
        $fixture = file_get_contents(__DIR__ . '/Fixture/test2.sql');
83
        $manager->getConnection()->exec($fixture);
84
        $manager->flush();
85
    }
86
87
    /**
88
     * A test matrix to verify the correct institutions are selected for a given identity for a
89
     * specific institution role.
90
     * @dataProvider authorizationMatrix
91
     */
92
    public function test_get_institutions_for_role_matrix(
93
        RegistrationAuthorityRole $requiredRole,
94
        IdentityId $identity,
95
        array $expectedInstitutions
96
    ) {
97
        $institutions = $this->authzRepository->getInstitutionsForRole($requiredRole, $identity);
98
        $results = $this->flattenInstitutionResults($institutions);
99
100
        $this->assertEquals(
101
            $results,
102
            $expectedInstitutions,
103
            sprintf(
104
                'The results do not match the expected results. Actual "%s" versus expected: "%s"',
105
                implode($results, ','),
0 ignored issues
show
Bug introduced by
',' of type string is incompatible with the type array expected by parameter $pieces of implode(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

105
                implode($results, /** @scrutinizer ignore-type */ ','),
Loading history...
106
                implode($expectedInstitutions, ',')
0 ignored issues
show
Unused Code introduced by
The call to implode() has too many arguments starting with ','. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

106
                /** @scrutinizer ignore-call */ 
107
                implode($expectedInstitutions, ',')

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
107
            )
108
        );
109
    }
110
111
    /**
112
     * @dataProvider selectRaaMatrix
113
     */
114
    public function test_select_raa_authorization(IdentityId $identityId, array $expected)
115
    {
116
        $institutions = $this->authzRepository->getInstitutionsForSelectRaaRole($identityId);
117
        $this->assertEquals($expected, $this->flattenInstitutionResults($institutions));
118
119
    }
120
121
    private function flattenInstitutionResults(InstitutionCollection $collection)
122
    {
123
        $institutions = [];
124
        /** @var Institution $institution */
125
        foreach($collection->jsonSerialize()['institutions'] as $institution)
126
        {
127
            $institutions[] = $institution->getInstitution();
128
        }
129
        return $institutions;
130
    }
131
}
132