|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * UserFrosting (http://www.userfrosting.com) |
| 5 | + * |
| 6 | + * @link https://github.com/userfrosting/UserFrosting |
| 7 | + * @copyright Copyright (c) 2019 Alexander Weissman |
| 8 | + * @license https://github.com/userfrosting/UserFrosting/blob/master/LICENSE.md (MIT License) |
| 9 | + */ |
| 10 | + |
| 11 | +namespace UserFrosting\Sprinkle\Account\Tests\Integration\Controller; |
| 12 | + |
| 13 | +use Mockery as m; |
| 14 | +use UserFrosting\Sprinkle\Account\Controller\AccountController; |
| 15 | +use UserFrosting\Sprinkle\Account\Repository\PasswordResetRepository; |
| 16 | +use UserFrosting\Sprinkle\Account\Tests\withTestUser; |
| 17 | +use UserFrosting\Sprinkle\Core\Tests\RefreshDatabase; |
| 18 | +use UserFrosting\Sprinkle\Core\Tests\TestDatabase; |
| 19 | +use UserFrosting\Sprinkle\Core\Tests\withController; |
| 20 | +use UserFrosting\Sprinkle\Core\Throttle\Throttler; |
| 21 | +use UserFrosting\Support\Exception\BadRequestException; |
| 22 | +use UserFrosting\Support\Exception\NotFoundException; |
| 23 | +use UserFrosting\Tests\TestCase; |
| 24 | + |
| 25 | +/** |
| 26 | + * Tests AccountController |
| 27 | + */ |
| 28 | +class AccountControllerTest extends TestCase |
| 29 | +{ |
| 30 | + use TestDatabase; |
| 31 | + use RefreshDatabase; |
| 32 | + use withTestUser; |
| 33 | + use withController; |
| 34 | + |
| 35 | + /** |
| 36 | + * @var bool DB is initialized for normal db |
| 37 | + */ |
| 38 | + protected static $initialized = false; |
| 39 | + |
| 40 | + /** |
| 41 | + * Setup test database for controller tests |
| 42 | + */ |
| 43 | + public function setUp() |
| 44 | + { |
| 45 | + parent::setUp(); |
| 46 | + $this->setupTestDatabase(); |
| 47 | + |
| 48 | + if ($this->usingInMemoryDatabase() || !static::$initialized) { |
| 49 | + |
| 50 | + // Setup database, then setup User & default role |
| 51 | + $this->refreshDatabase(); |
| 52 | + static::$initialized = true; |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + public function tearDown() |
| 57 | + { |
| 58 | + parent::tearDown(); |
| 59 | + m::close(); |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * @return AccountController |
| 64 | + */ |
| 65 | + public function testControllerConstructor() |
| 66 | + { |
| 67 | + $controller = $this->getController(); |
| 68 | + $this->assertInstanceOf(AccountController::class, $controller); |
| 69 | + |
| 70 | + return $controller; |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * @depends testControllerConstructor |
| 75 | + * @param AccountController $controller |
| 76 | + */ |
| 77 | + public function testcheckUsername(AccountController $controller) |
| 78 | + { |
| 79 | + $request = $this->getRequest()->withQueryParams([ |
| 80 | + 'user_name' => 'potato' |
| 81 | + ]); |
| 82 | + |
| 83 | + $result = $controller->checkUsername($request, $this->getResponse(), []); |
| 84 | + $this->assertSame($result->getStatusCode(), 200); |
| 85 | + $this->assertSame('true', (string) $result->getBody()); |
| 86 | + } |
| 87 | + |
| 88 | + /** |
| 89 | + * @depends testControllerConstructor |
| 90 | + * @depends testcheckUsername |
| 91 | + * @param AccountController $controller |
| 92 | + */ |
| 93 | + public function testcheckUsernameWithNoData(AccountController $controller) |
| 94 | + { |
| 95 | + $this->expectException(BadRequestException::class); |
| 96 | + $controller->checkUsername($this->getRequest(), $this->getResponse(), []); |
| 97 | + } |
| 98 | + |
| 99 | + /** |
| 100 | + * @depends testControllerConstructor |
| 101 | + * @depends testcheckUsername |
| 102 | + * @param AccountController $controller |
| 103 | + */ |
| 104 | + public function testcheckUsernameWithUsernameNotAvailable(AccountController $controller) |
| 105 | + { |
| 106 | + // Create test user |
| 107 | + $this->createTestUser(false, false, [ |
| 108 | + 'user_name' => 'userfoo' |
| 109 | + ]); |
| 110 | + |
| 111 | + $request = $this->getRequest()->withQueryParams([ |
| 112 | + 'user_name' => 'userfoo' |
| 113 | + ]); |
| 114 | + |
| 115 | + $result = $controller->checkUsername($request, $this->getResponse(), []); |
| 116 | + $this->assertSame($result->getStatusCode(), 200); |
| 117 | + $this->assertNotSame('', (string) $result->getBody()); |
| 118 | + $this->assertNotSame('true', (string) $result->getBody()); |
| 119 | + } |
| 120 | + |
| 121 | + /** |
| 122 | + * @depends testControllerConstructor |
| 123 | + * @depends testcheckUsername |
| 124 | + * @param AccountController $controller |
| 125 | + */ |
| 126 | + public function testcheckUsernameWithThrottler(AccountController $controller) |
| 127 | + { |
| 128 | + // Create fake mailer |
| 129 | + $throttler = m::mock(Throttler::class); |
| 130 | + $throttler->shouldReceive('getDelay')->once()->with('check_username_request')->andReturn(90); |
| 131 | + $this->ci->throttler = $throttler; |
| 132 | + |
| 133 | + // Recreate controller to use fake throttler |
| 134 | + $controller = $this->getController(); |
| 135 | + |
| 136 | + $request = $this->getRequest()->withQueryParams([ |
| 137 | + 'user_name' => 'potato' |
| 138 | + ]); |
| 139 | + |
| 140 | + $result = $controller->checkUsername($request, $this->getResponse(), []); |
| 141 | + $this->assertSame($result->getStatusCode(), 429); |
| 142 | + $this->assertJson((string) $result->getBody()); |
| 143 | + $this->assertSame('[]', (string) $result->getBody()); |
| 144 | + } |
| 145 | + |
| 146 | + /** |
| 147 | + * @depends testControllerConstructor |
| 148 | + * @param AccountController $controller |
| 149 | + */ |
| 150 | + public function testdenyResetPassword(AccountController $controller) |
| 151 | + { |
| 152 | + // Create fake mailer |
| 153 | + $repoPasswordReset = m::mock(PasswordResetRepository::class); |
| 154 | + $repoPasswordReset->shouldReceive('cancel')->once()->with('potato')->andReturn(true); |
| 155 | + $this->ci->repoPasswordReset = $repoPasswordReset; |
| 156 | + |
| 157 | + // Recreate controller to use fake throttler |
| 158 | + $controller = $this->getController(); |
| 159 | + |
| 160 | + $request = $this->getRequest()->withQueryParams([ |
| 161 | + 'token' => 'potato' |
| 162 | + ]); |
| 163 | + |
| 164 | + $result = $controller->denyResetPassword($request, $this->getResponse(), []); |
| 165 | + $this->assertSame($result->getStatusCode(), 302); |
| 166 | + $this->assertEquals($this->ci->router->pathFor('login'), $result->getHeaderLine('Location')); |
| 167 | + |
| 168 | + // Test message |
| 169 | + $ms = $this->ci->alerts; |
| 170 | + $messages = $ms->getAndClearMessages(); |
| 171 | + $this->assertSame('success', end($messages)['type']); |
| 172 | + } |
| 173 | + |
| 174 | + /** |
| 175 | + * @depends testControllerConstructor |
| 176 | + * @depends testdenyResetPassword |
| 177 | + * @param AccountController $controller |
| 178 | + */ |
| 179 | + public function testdenyResetPasswordWithFailedPasswordReset(AccountController $controller) |
| 180 | + { |
| 181 | + // Create fake mailer |
| 182 | + $repoPasswordReset = m::mock(PasswordResetRepository::class); |
| 183 | + $repoPasswordReset->shouldReceive('cancel')->once()->with('potato')->andReturn(false); |
| 184 | + $this->ci->repoPasswordReset = $repoPasswordReset; |
| 185 | + |
| 186 | + // Recreate controller to use fake throttler |
| 187 | + $controller = $this->getController(); |
| 188 | + |
| 189 | + $request = $this->getRequest()->withQueryParams([ |
| 190 | + 'token' => 'potato' |
| 191 | + ]); |
| 192 | + |
| 193 | + $result = $controller->denyResetPassword($request, $this->getResponse(), []); |
| 194 | + $this->assertSame($result->getStatusCode(), 302); |
| 195 | + $this->assertEquals($this->ci->router->pathFor('login'), $result->getHeaderLine('Location')); |
| 196 | + |
| 197 | + // Test message |
| 198 | + $ms = $this->ci->alerts; |
| 199 | + $messages = $ms->getAndClearMessages(); |
| 200 | + $this->assertSame('danger', end($messages)['type']); |
| 201 | + } |
| 202 | + |
| 203 | + /** |
| 204 | + * @depends testControllerConstructor |
| 205 | + * @depends testdenyResetPassword |
| 206 | + * @param AccountController $controller |
| 207 | + */ |
| 208 | + public function testdenyResetPasswordWithFailedValidation(AccountController $controller) |
| 209 | + { |
| 210 | + $result = $controller->denyResetPassword($this->getRequest(), $this->getResponse(), []); |
| 211 | + $this->assertSame($result->getStatusCode(), 302); |
| 212 | + $this->assertEquals($this->ci->router->pathFor('login'), $result->getHeaderLine('Location')); |
| 213 | + |
| 214 | + // Test message |
| 215 | + $ms = $this->ci->alerts; |
| 216 | + $messages = $ms->getAndClearMessages(); |
| 217 | + $this->assertSame('danger', end($messages)['type']); |
| 218 | + } |
| 219 | + |
| 220 | + /** |
| 221 | + * @return AccountController |
| 222 | + */ |
| 223 | + private function getController() |
| 224 | + { |
| 225 | + return new AccountController($this->ci); |
| 226 | + } |
| 227 | +} |
0 commit comments