Completed
Push — master ( 00092a...39103d )
by wiese
86:17 queued 21:06
created

newAuthorizerWithApplications()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 13
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 7
nc 2
nop 3
1
<?php
2
3
declare( strict_types = 1 );
4
5
namespace WMDE\Fundraising\Frontend\MembershipContext\Tests\Integration\DataAccess;
6
7
use Codeception\Specify;
8
use Doctrine\ORM\EntityManager;
9
use Doctrine\ORM\ORMException;
10
use WMDE\Fundraising\Entities\MembershipApplication;
11
use WMDE\Fundraising\Frontend\MembershipContext\Authorization\ApplicationAuthorizer;
12
use WMDE\Fundraising\Frontend\MembershipContext\DataAccess\DoctrineApplicationAuthorizer;
13
use WMDE\Fundraising\Frontend\Tests\TestEnvironment;
14
use WMDE\Fundraising\Store\MembershipApplicationData;
15
16
/**
17
 * @covers \WMDE\Fundraising\Frontend\MembershipContext\DataAccess\DoctrineApplicationAuthorizer
18
 *
19
 * @licence GNU GPL v2+
20
 * @author Jeroen De Dauw < [email protected] >
21
 */
22
class DoctrineMembershipApplicationAuthorizerTest extends \PHPUnit\Framework\TestCase {
23
	use Specify;
24
25
	const CORRECT_UPDATE_TOKEN = 'CorrectUpdateToken';
26
	const CORRECT_ACCESS_TOKEN = 'CorrectAccessToken';
27
	const WRONG__UPDATE_TOKEN = 'WrongUpdateToken';
28
	const WRONG_ACCESS_TOKEN = 'WrongAccessToken';
29
	const MEANINGLESS_APPLICATION_ID = 1337;
30
	const ID_OF_WRONG_APPLICATION = 42;
31
32
	private function newAuthorizerWithApplications( string $updateToken = null,
33
		string $accessToken = null, MembershipApplication ...$applications ): ApplicationAuthorizer {
34
35
		$entityManager = TestEnvironment::newInstance()->getFactory()->getEntityManager();
36
37
		foreach ( $applications as $application ) {
38
			$entityManager->persist( $application );
39
		}
40
41
		$entityManager->flush();
42
43
		return new DoctrineApplicationAuthorizer( $entityManager, $updateToken, $accessToken );
44
	}
45
46
	/**
47
	 * @slowThreshold 400
48
	 */
49
	public function testWhenNoMembershipApplications(): void {
50
		$this->specify( 'update authorization fails', function() {
51
			$authorizer = $this->newAuthorizerWithApplications( self::CORRECT_UPDATE_TOKEN );
52
			$this->assertFalse( $authorizer->canModifyApplication( self::MEANINGLESS_APPLICATION_ID ) );
53
		} );
54
55
		$this->specify( 'access authorization fails', function() {
56
			$authorizer = $this->newAuthorizerWithApplications( self::CORRECT_ACCESS_TOKEN );
57
			$this->assertFalse( $authorizer->canAccessApplication( self::MEANINGLESS_APPLICATION_ID ) );
58
		} );
59
	}
60
61
	/**
62
	 * @slowThreshold 1200
63
	 */
64
	public function testWhenApplicationWithTokenExists(): void {
65
		$application = new MembershipApplication();
66
67
		$application->modifyDataObject( function( MembershipApplicationData $data ) {
68
			$data->setUpdateToken( self::CORRECT_UPDATE_TOKEN );
69
			$data->setAccessToken( self::CORRECT_ACCESS_TOKEN );
70
		} );
71
72
		$this->specify(
73
			'given correct application id and correct token, update authorization succeeds',
74
			function() use ( $application ) {
75
				$authorizer = $this->newAuthorizerWithApplications( self::CORRECT_UPDATE_TOKEN, null, $application );
76
				$this->assertTrue( $authorizer->canModifyApplication( $application->getId() ) );
77
			}
78
		);
79
80
		$this->specify(
81
			'given wrong application id and correct token, update authorization fails',
82
			function() use ( $application ) {
83
				$authorizer = $this->newAuthorizerWithApplications( self::CORRECT_UPDATE_TOKEN, null, $application );
84
				$this->assertFalse( $authorizer->canModifyApplication( self::ID_OF_WRONG_APPLICATION ) );
85
			}
86
		);
87
88
		$this->specify(
89
			'given correct application id and wrong token, update authorization fails',
90
			function() use ( $application ) {
91
				$authorizer = $this->newAuthorizerWithApplications( self::WRONG__UPDATE_TOKEN, null, $application );
92
				$this->assertFalse( $authorizer->canModifyApplication( $application->getId() ) );
93
			}
94
		);
95
96
		$this->specify(
97
			'given correct application id and correct token, access authorization succeeds',
98
			function() use ( $application ) {
99
				$authorizer = $this->newAuthorizerWithApplications( null, self::CORRECT_ACCESS_TOKEN, $application );
100
				$this->assertTrue( $authorizer->canAccessApplication( $application->getId() ) );
101
			}
102
		);
103
104
		$this->specify(
105
			'given wrong application id and correct token, access authorization fails',
106
			function() use ( $application ) {
107
				$authorizer = $this->newAuthorizerWithApplications( null, self::CORRECT_ACCESS_TOKEN, $application );
108
				$this->assertFalse( $authorizer->canAccessApplication( self::ID_OF_WRONG_APPLICATION ) );
109
			}
110
		);
111
112
		$this->specify(
113
			'given correct application id and wrong token, access authorization fails',
114
			function() use ( $application ) {
115
				$authorizer = $this->newAuthorizerWithApplications( null, self::WRONG_ACCESS_TOKEN, $application );
116
				$this->assertFalse( $authorizer->canAccessApplication( $application->getId() ) );
117
			}
118
		);
119
	}
120
121
	/**
122
	 * @slowThreshold 400
123
	 */
124
	public function testWhenApplicationWithoutTokenExists(): void {
125
		$application = new MembershipApplication();
126
127
		$this->specify(
128
			'given correct application id and a token, update authorization fails',
129
			function() use ( $application ) {
130
				$authorizer = $this->newAuthorizerWithApplications( 'SomeToken', null, $application );
131
				$this->assertFalse( $authorizer->canModifyApplication( $application->getId() ) );
132
			}
133
		);
134
135
		$this->specify(
136
			'given correct application id and a token, access authorization fails',
137
			function() use ( $application ) {
138
				$authorizer = $this->newAuthorizerWithApplications( null, 'SomeToken', $application );
139
				$this->assertFalse( $authorizer->canAccessApplication( $application->getId() ) );
140
			}
141
		);
142
	}
143
144
	/**
145
	 * @slowThreshold 400
146
	 */
147
	public function testWhenDoctrineThrowsException(): void {
148
		$authorizer = new DoctrineApplicationAuthorizer(
149
			$this->getThrowingEntityManager(),
150
			self::CORRECT_UPDATE_TOKEN,
151
			self::CORRECT_ACCESS_TOKEN
152
		);
153
154
		$this->specify( 'update authorization fails', function() use ( $authorizer ) {
155
			$this->assertFalse( $authorizer->canModifyApplication( self::MEANINGLESS_APPLICATION_ID ) );
156
		} );
157
158
		$this->specify( 'access authorization fails', function() use ( $authorizer ) {
159
			$this->assertFalse( $authorizer->canAccessApplication( self::MEANINGLESS_APPLICATION_ID ) );
160
		} );
161
	}
162
163
	private function getThrowingEntityManager(): EntityManager {
164
		$entityManager = $this->getMockBuilder( EntityManager::class )
165
			->disableOriginalConstructor()->getMock();
166
167
		$entityManager->method( $this->anything() )
168
			->willThrowException( new ORMException() );
169
170
		return $entityManager;
171
	}
172
173
}
174