Skip to content

Commit b99f3e1

Browse files
committed
Fix previous PGSQL test faillure
1 parent f0ec603 commit b99f3e1

2 files changed

Lines changed: 125 additions & 121 deletions

File tree

app/sprinkles/account/tests/Integration/Controller/AccountControllerTest.php

Lines changed: 125 additions & 119 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,131 @@ public function testControllerConstructor()
7878
return $controller;
7979
}
8080

81+
/**
82+
* N.B.: Must be first test, before any master user is created
83+
* @depends testControllerConstructor
84+
* @param AccountController $controller
85+
*/
86+
public function testRegisterWithNoMasterUser(AccountController $controller)
87+
{
88+
// Set POST
89+
$request = $this->getRequest()->withParsedBody([
90+
'spiderbro' => 'http://',
91+
]);
92+
93+
$result = $controller->register($request, $this->getResponse(), []);
94+
$this->assertInstanceOf(\Psr\Http\Message\ResponseInterface::class, $result);
95+
$this->assertSame($result->getStatusCode(), 403);
96+
$this->assertJson((string) $result->getBody());
97+
$this->assertSame('[]', (string) $result->getBody());
98+
99+
// Test message
100+
$ms = $this->ci->alerts;
101+
$messages = $ms->getAndClearMessages();
102+
$this->assertSame('danger', end($messages)['type']);
103+
}
104+
105+
/**
106+
* N.B.: Run this register second, as it's easier if no test user is present.
107+
* @depends testControllerConstructor
108+
* @see UserFrosting\Sprinkle\Account\Tests\Integration\RegistrationTest for complete registration exception (for example duplicate email) testing
109+
*/
110+
public function testRegister()
111+
{
112+
// Force locale config
113+
$this->ci->config['site.registration.user_defaults.locale'] = 'en_US';
114+
$this->ci->config['site.locales.available'] = [
115+
'en_US' => 'English',
116+
];
117+
118+
// Create fake mailer
119+
$mailer = m::mock(Mailer::class);
120+
$mailer->shouldReceive('send')->once()->with(\Mockery::type(TwigMailMessage::class));
121+
$this->ci->mailer = $mailer;
122+
123+
// Register will fail on PGSQL if a user is created with forced id
124+
// before registration occurs because the forced id mess the auto_increment
125+
// @see https://stackoverflow.com/questions/36157029/laravel-5-2-eloquent-save-auto-increment-pgsql-exception-on-same-id
126+
// So we create a dummy user and assign the master id config to it's id
127+
// to bypass the "no registration if no master user" security feature.
128+
// (Note the dummy should by default be id n°1, but we still assign the config
129+
// in case the default config does not return 1)
130+
$fm = $this->ci->factory;
131+
$dummyUser = $fm->create(User::class);
132+
$this->ci->config['reserved_user_ids.master'] = $dummyUser->id;
133+
134+
// Recreate controller to use fake config
135+
$controller = $this->getController();
136+
137+
// Perfrom common test code
138+
$this->performActualRegisterTest($controller);
139+
}
140+
141+
/**
142+
* @depends testControllerConstructor
143+
* @depends testRegister
144+
*/
145+
public function testRegisterWithNoEmailVerification()
146+
{
147+
// Delete previous attempt so we can reuse the same shared test code
148+
if ($user = User::where('email', '[email protected]')->first()) {
149+
$user->delete(true);
150+
}
151+
152+
// Force locale config, disable email verification
153+
$this->ci->config['site.registration.require_email_verification'] = false;
154+
$this->ci->config['site.registration.user_defaults.locale'] = 'en_US';
155+
$this->ci->config['site.locales.available'] = [
156+
'en_US' => 'English',
157+
];
158+
159+
// Recreate controller to use fake config
160+
$controller = $this->getController();
161+
162+
// Perfrom common test code
163+
$this->performActualRegisterTest($controller);
164+
}
165+
166+
/**
167+
* @param AccountController $controller
168+
*/
169+
protected function performActualRegisterTest(AccountController $controller)
170+
{
171+
// Genereate a captcha for next request.
172+
$captcha = new Captcha($this->ci->session, $this->ci->config['session.keys.captcha']);
173+
$captcha->generateRandomCode();
174+
175+
// Set POST
176+
$request = $this->getRequest()->withParsedBody([
177+
'spiderbro' => 'http://',
178+
'captcha' => $captcha->getCaptcha(),
179+
'user_name' => 'RegisteredUser',
180+
'first_name' => 'Testing',
181+
'last_name' => 'Register',
182+
'email' => '[email protected]',
183+
'password' => 'FooBarFooBar123',
184+
'passwordc' => 'FooBarFooBar123',
185+
'locale' => '',
186+
]);
187+
188+
$result = $controller->register($request, $this->getResponse(), []);
189+
$this->assertInstanceOf(\Psr\Http\Message\ResponseInterface::class, $result);
190+
$this->assertSame($result->getStatusCode(), 200);
191+
$this->assertJson((string) $result->getBody());
192+
$this->assertSame('[]', (string) $result->getBody());
193+
194+
// Make sure the user is added to the db by querying it
195+
$users = User::where('email', '[email protected]')->get();
196+
$this->assertCount(1, $users);
197+
$this->assertSame('RegisteredUser', $users->first()['user_name']);
198+
199+
// Test message
200+
$ms = $this->ci->alerts;
201+
$messages = $ms->getAndClearMessages();
202+
$this->assertSame('success', end($messages)['type']);
203+
}
204+
205+
81206
/**
82207
* @depends testControllerConstructor
83208
* @param AccountController $controller
@@ -1044,125 +1169,6 @@ public function testRegisterWithFailedValidation(AccountController $controller)
10441169
$this->assertSame('danger', end($messages)['type']);
10451170
}
10461171

1047-
/**
1048-
* @depends testControllerConstructor
1049-
* @depends testRegisterWithFailedValidation
1050-
* @see UserFrosting\Sprinkle\Account\Tests\Integration\RegistrationTest for complete registration exception (for example duplicate email) testing
1051-
*/
1052-
public function testRegister()
1053-
{
1054-
// Force config
1055-
$this->ci->config['site.registration.user_defaults.locale'] = 'en_US';
1056-
$this->ci->config['site.locales.available'] = [
1057-
'en_US' => 'English',
1058-
];
1059-
1060-
// Create fake mailer
1061-
$mailer = m::mock(Mailer::class);
1062-
$mailer->shouldReceive('send')->once()->with(\Mockery::type(TwigMailMessage::class));
1063-
$this->ci->mailer = $mailer;
1064-
1065-
// Recreate controller to use fake config
1066-
$controller = $this->getController();
1067-
1068-
// Perfrom common test code
1069-
$this->performActualRegisterTest($controller);
1070-
}
1071-
1072-
/**
1073-
* @depends testControllerConstructor
1074-
* @depends testRegister
1075-
*/
1076-
public function testRegisterWithNoEmailVerification()
1077-
{
1078-
// Delete previous attempt
1079-
if ($user = User::where('email', '[email protected]')->first()) {
1080-
$user->delete(true);
1081-
}
1082-
1083-
// Force config
1084-
$this->ci->config['site.registration.require_email_verification'] = false;
1085-
$this->ci->config['site.registration.user_defaults.locale'] = 'en_US';
1086-
$this->ci->config['site.locales.available'] = [
1087-
'en_US' => 'English',
1088-
];
1089-
1090-
1091-
// Recreate controller to use fake config
1092-
$controller = $this->getController();
1093-
1094-
// Perfrom common test code
1095-
$this->performActualRegisterTest($controller);
1096-
}
1097-
1098-
/**
1099-
* @param AccountController $controller
1100-
*/
1101-
protected function performActualRegisterTest(AccountController $controller)
1102-
{
1103-
// Genereate a captcha for next request.
1104-
$captcha = new Captcha($this->ci->session, $this->ci->config['session.keys.captcha']);
1105-
$captcha->generateRandomCode();
1106-
1107-
// Set POST
1108-
$request = $this->getRequest()->withParsedBody([
1109-
'spiderbro' => 'http://',
1110-
'captcha' => $captcha->getCaptcha(),
1111-
'user_name' => 'RegisteredUser',
1112-
'first_name' => 'Testing',
1113-
'last_name' => 'Register',
1114-
'email' => '[email protected]',
1115-
'password' => 'FooBarFooBar123',
1116-
'passwordc' => 'FooBarFooBar123',
1117-
'locale' => '',
1118-
]);
1119-
1120-
$result = $controller->register($request, $this->getResponse(), []);
1121-
$this->assertInstanceOf(\Psr\Http\Message\ResponseInterface::class, $result);
1122-
$this->assertSame($result->getStatusCode(), 200);
1123-
$this->assertJson((string) $result->getBody());
1124-
$this->assertSame('[]', (string) $result->getBody());
1125-
1126-
// Make sure the user is added to the db by querying it
1127-
$users = User::where('email', '[email protected]')->get();
1128-
$this->assertCount(1, $users);
1129-
$this->assertSame('RegisteredUser', $users->first()['user_name']);
1130-
1131-
// Test message
1132-
$ms = $this->ci->alerts;
1133-
$messages = $ms->getAndClearMessages();
1134-
$this->assertSame('success', end($messages)['type']);
1135-
}
1136-
1137-
/**
1138-
* @depends testControllerConstructor
1139-
* @param AccountController $controller
1140-
*/
1141-
public function testRegisterWithNoMasterUser(AccountController $controller)
1142-
{
1143-
// Delete any master user, just in case
1144-
if ($user = User::find($this->ci->config['reserved_user_ids.master'])) {
1145-
$user->delete(true);
1146-
}
1147-
1148-
// Set POST
1149-
$request = $this->getRequest()->withParsedBody([
1150-
'spiderbro' => 'http://',
1151-
]);
1152-
1153-
$result = $controller->register($request, $this->getResponse(), []);
1154-
$this->assertInstanceOf(\Psr\Http\Message\ResponseInterface::class, $result);
1155-
$this->assertSame($result->getStatusCode(), 403);
1156-
$this->assertJson((string) $result->getBody());
1157-
$this->assertSame('[]', (string) $result->getBody());
1158-
1159-
// Test message
1160-
$ms = $this->ci->alerts;
1161-
$messages = $ms->getAndClearMessages();
1162-
$this->assertSame('danger', end($messages)['type']);
1163-
}
1164-
1165-
11661172
/**
11671173
* @return AccountController
11681174
*/

app/sprinkles/admin/tests/Integration/Controller/UserControllerCreateTest.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,6 @@ public function testCreate()
106106

107107
/**
108108
* @depends testCreate
109-
* @param UserController $controller
110109
*/
111110
public function testCreateWithNoLocale()
112111
{
@@ -116,7 +115,6 @@ public function testCreateWithNoLocale()
116115

117116
/**
118117
* @depends testCreate
119-
* @param UserController $controller
120118
*/
121119
public function testCreateWithNoGroupId()
122120
{

0 commit comments

Comments
 (0)