aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorJeremy Benoist <jeremy.benoist@gmail.com>2017-06-02 10:19:33 +0200
committerJeremy Benoist <jeremy.benoist@gmail.com>2017-06-02 11:32:38 +0200
commit426bb453d295900fb3e35dce2f9081a42639cf27 (patch)
tree77fc6aedf17640a9d13a817e25fbd06ab89b471f
parenta687c8d915276eee0c0494156700f7d0c0606735 (diff)
downloadwallabag-426bb453d295900fb3e35dce2f9081a42639cf27.tar.gz
wallabag-426bb453d295900fb3e35dce2f9081a42639cf27.tar.zst
wallabag-426bb453d295900fb3e35dce2f9081a42639cf27.zip
API user creation behing a toggle
I've added a toggle feature (in internal settings) so that user api creation can be disabled while form registration still can be enabled. Also, the /api/user endpoint shouldn't require authentication. Even if we check the authentication when sending a GET request, to retrieve current user information. I've moved all the internal settings definition to config to avoid duplicated place to define them. I don't know why we didn't did that earlier.
-rw-r--r--app/DoctrineMigrations/Version20170602075214.php52
-rw-r--r--app/config/config.yml129
-rw-r--r--app/config/security.yml1
-rw-r--r--src/Wallabag/ApiBundle/Controller/UserRestController.php6
-rw-r--r--src/Wallabag/CoreBundle/Command/InstallCommand.php160
-rw-r--r--src/Wallabag/CoreBundle/DataFixtures/ORM/LoadSettingData.php174
-rw-r--r--src/Wallabag/CoreBundle/DependencyInjection/Configuration.php11
-rw-r--r--src/Wallabag/CoreBundle/DependencyInjection/WallabagCoreExtension.php1
-rw-r--r--tests/Wallabag/ApiBundle/Controller/UserRestControllerTest.php96
-rw-r--r--tests/Wallabag/ApiBundle/Controller/WallabagRestControllerTest.php10
10 files changed, 297 insertions, 343 deletions
diff --git a/app/DoctrineMigrations/Version20170602075214.php b/app/DoctrineMigrations/Version20170602075214.php
new file mode 100644
index 00000000..451d16ba
--- /dev/null
+++ b/app/DoctrineMigrations/Version20170602075214.php
@@ -0,0 +1,52 @@
1<?php
2
3namespace Application\Migrations;
4
5use Doctrine\DBAL\Migrations\AbstractMigration;
6use Doctrine\DBAL\Schema\Schema;
7use Symfony\Component\DependencyInjection\ContainerAwareInterface;
8use Symfony\Component\DependencyInjection\ContainerInterface;
9
10/**
11 * Add api_user_registration in craue_config_setting.
12 */
13class Version20170602075214 extends AbstractMigration implements ContainerAwareInterface
14{
15 /**
16 * @var ContainerInterface
17 */
18 private $container;
19
20 public function setContainer(ContainerInterface $container = null)
21 {
22 $this->container = $container;
23 }
24
25 private function getTable($tableName)
26 {
27 return $this->container->getParameter('database_table_prefix').$tableName;
28 }
29
30 /**
31 * @param Schema $schema
32 */
33 public function up(Schema $schema)
34 {
35 $apiUserRegistration = $this->container
36 ->get('doctrine.orm.default_entity_manager')
37 ->getConnection()
38 ->fetchArray('SELECT * FROM '.$this->getTable('craue_config_setting')." WHERE name = 'api_user_registration'");
39
40 $this->skipIf(false !== $apiUserRegistration, 'It seems that you already played this migration.');
41
42 $this->addSql('INSERT INTO '.$this->getTable('craue_config_setting')." (name, value, section) VALUES ('api_user_registration', '0', 'api')");
43 }
44
45 /**
46 * @param Schema $schema
47 */
48 public function down(Schema $schema)
49 {
50 $this->addSql('DELETE FROM '.$this->getTable('craue_config_setting')." WHERE name = 'api_user_registration';");
51 }
52}
diff --git a/app/config/config.yml b/app/config/config.yml
index 04f8547d..b0d330ab 100644
--- a/app/config/config.yml
+++ b/app/config/config.yml
@@ -62,6 +62,135 @@ wallabag_core:
62 fetching_error_message: | 62 fetching_error_message: |
63 wallabag can't retrieve contents for this article. Please <a href="http://doc.wallabag.org/en/user/errors_during_fetching.html#how-can-i-help-to-fix-that">troubleshoot this issue</a>. 63 wallabag can't retrieve contents for this article. Please <a href="http://doc.wallabag.org/en/user/errors_during_fetching.html#how-can-i-help-to-fix-that">troubleshoot this issue</a>.
64 api_limit_mass_actions: 10 64 api_limit_mass_actions: 10
65 default_internal_settings:
66 -
67 name: share_public
68 value: 1
69 section: entry
70 -
71 name: carrot
72 value: 1
73 section: entry
74 -
75 name: share_diaspora
76 value: 1
77 section: entry
78 -
79 name: diaspora_url
80 value: http://diasporapod.com
81 section: entry
82 -
83 name: share_unmark
84 value: 1
85 section: entry
86 -
87 name: unmark_url
88 value: https://unmark.it
89 section: entry
90 -
91 name: share_shaarli
92 value: 1
93 section: entry
94 -
95 name: share_scuttle
96 value: 1
97 section: entry
98 -
99 name: shaarli_url
100 value: http://myshaarli.com
101 section: entry
102 -
103 name: scuttle_url
104 value: http://scuttle.org
105 section: entry
106 -
107 name: share_mail
108 value: 1
109 section: entry
110 -
111 name: share_twitter
112 value: 1
113 section: entry
114 -
115 name: show_printlink
116 value: 1
117 section: entry
118 -
119 name: restricted_access
120 value: 0
121 section: entry
122 -
123 name: export_epub
124 value: 1
125 section: export
126 -
127 name: export_mobi
128 value: 1
129 section: export
130 -
131 name: export_pdf
132 value: 1
133 section: export
134 -
135 name: export_csv
136 value: 1
137 section: export
138 -
139 name: export_json
140 value: 1
141 section: export
142 -
143 name: export_txt
144 value: 1
145 section: export
146 -
147 name: export_xml
148 value: 1
149 section: export
150 -
151 name: import_with_redis
152 value: 0
153 section: import
154 -
155 name: import_with_rabbitmq
156 value: 0
157 section: import
158 -
159 name: piwik_enabled
160 value: 0
161 section: analytics
162 -
163 name: piwik_host
164 value: v2.wallabag.org
165 section: analytics
166 -
167 name: piwik_site_id
168 value: 1
169 section: analytics
170 -
171 name: demo_mode_enabled
172 value: 0
173 section: misc
174 -
175 name: demo_mode_username
176 value: wallabag
177 section: misc
178 -
179 name: download_images_enabled
180 value: 0
181 section: misc
182 -
183 name: wallabag_support_url
184 value: https://www.wallabag.org/pages/support.html
185 section: misc
186 -
187 name: wallabag_url
188 value: http://v2.wallabag.org
189 section: misc
190 -
191 name: api_user_registration
192 value: 0
193 section: api
65 194
66wallabag_user: 195wallabag_user:
67 registration_enabled: "%fosuser_registration%" 196 registration_enabled: "%fosuser_registration%"
diff --git a/app/config/security.yml b/app/config/security.yml
index efb00a53..ffb1d356 100644
--- a/app/config/security.yml
+++ b/app/config/security.yml
@@ -56,6 +56,7 @@ security:
56 access_control: 56 access_control:
57 - { path: ^/api/doc, roles: IS_AUTHENTICATED_ANONYMOUSLY } 57 - { path: ^/api/doc, roles: IS_AUTHENTICATED_ANONYMOUSLY }
58 - { path: ^/api/version, roles: IS_AUTHENTICATED_ANONYMOUSLY } 58 - { path: ^/api/version, roles: IS_AUTHENTICATED_ANONYMOUSLY }
59 - { path: ^/api/user, roles: IS_AUTHENTICATED_ANONYMOUSLY }
59 - { path: ^/login, roles: IS_AUTHENTICATED_ANONYMOUSLY } 60 - { path: ^/login, roles: IS_AUTHENTICATED_ANONYMOUSLY }
60 - { path: ^/register, role: IS_AUTHENTICATED_ANONYMOUSLY } 61 - { path: ^/register, role: IS_AUTHENTICATED_ANONYMOUSLY }
61 - { path: ^/resetting, role: IS_AUTHENTICATED_ANONYMOUSLY } 62 - { path: ^/resetting, role: IS_AUTHENTICATED_ANONYMOUSLY }
diff --git a/src/Wallabag/ApiBundle/Controller/UserRestController.php b/src/Wallabag/ApiBundle/Controller/UserRestController.php
index a1b78e3f..1fc67d00 100644
--- a/src/Wallabag/ApiBundle/Controller/UserRestController.php
+++ b/src/Wallabag/ApiBundle/Controller/UserRestController.php
@@ -43,7 +43,7 @@ class UserRestController extends WallabagRestController
43 */ 43 */
44 public function putUserAction(Request $request) 44 public function putUserAction(Request $request)
45 { 45 {
46 if (!$this->container->getParameter('fosuser_registration')) { 46 if (!$this->getParameter('fosuser_registration') || !$this->get('craue_config')->get('api_user_registration')) {
47 $json = $this->get('serializer')->serialize(['error' => "Server doesn't allow registrations"], 'json'); 47 $json = $this->get('serializer')->serialize(['error' => "Server doesn't allow registrations"], 'json');
48 48
49 return (new JsonResponse())->setJson($json)->setStatusCode(403); 49 return (new JsonResponse())->setJson($json)->setStatusCode(403);
@@ -51,8 +51,8 @@ class UserRestController extends WallabagRestController
51 51
52 $userManager = $this->get('fos_user.user_manager'); 52 $userManager = $this->get('fos_user.user_manager');
53 $user = $userManager->createUser(); 53 $user = $userManager->createUser();
54 // enable created user by default 54 // user will be disabled BY DEFAULT to avoid spamming account to be created
55 $user->setEnabled(true); 55 $user->setEnabled(false);
56 56
57 $form = $this->createForm('Wallabag\UserBundle\Form\NewUserType', $user, [ 57 $form = $this->createForm('Wallabag\UserBundle\Form\NewUserType', $user, [
58 'csrf_protection' => false, 58 'csrf_protection' => false,
diff --git a/src/Wallabag/CoreBundle/Command/InstallCommand.php b/src/Wallabag/CoreBundle/Command/InstallCommand.php
index d9608246..0f119377 100644
--- a/src/Wallabag/CoreBundle/Command/InstallCommand.php
+++ b/src/Wallabag/CoreBundle/Command/InstallCommand.php
@@ -292,165 +292,7 @@ class InstallCommand extends ContainerAwareCommand
292 // cleanup before insert new stuff 292 // cleanup before insert new stuff
293 $em->createQuery('DELETE FROM CraueConfigBundle:Setting')->execute(); 293 $em->createQuery('DELETE FROM CraueConfigBundle:Setting')->execute();
294 294
295 $settings = [ 295 foreach ($this->getContainer()->getParameter('wallabag_core.default_internal_settings') as $setting) {
296 [
297 'name' => 'share_public',
298 'value' => '1',
299 'section' => 'entry',
300 ],
301 [
302 'name' => 'carrot',
303 'value' => '1',
304 'section' => 'entry',
305 ],
306 [
307 'name' => 'share_diaspora',
308 'value' => '1',
309 'section' => 'entry',
310 ],
311 [
312 'name' => 'diaspora_url',
313 'value' => 'http://diasporapod.com',
314 'section' => 'entry',
315 ],
316 [
317 'name' => 'share_unmark',
318 'value' => '1',
319 'section' => 'entry',
320 ],
321 [
322 'name' => 'unmark_url',
323 'value' => 'https://unmark.it',
324 'section' => 'entry',
325 ],
326 [
327 'name' => 'share_shaarli',
328 'value' => '1',
329 'section' => 'entry',
330 ],
331 [
332 'name' => 'shaarli_url',
333 'value' => 'http://myshaarli.com',
334 'section' => 'entry',
335 ],
336 [
337 'name' => 'share_scuttle',
338 'value' => '1',
339 'section' => 'entry',
340 ],
341 [
342 'name' => 'scuttle_url',
343 'value' => 'http://scuttle.org',
344 'section' => 'entry',
345 ],
346 [
347 'name' => 'share_mail',
348 'value' => '1',
349 'section' => 'entry',
350 ],
351 [
352 'name' => 'share_twitter',
353 'value' => '1',
354 'section' => 'entry',
355 ],
356 [
357 'name' => 'export_epub',
358 'value' => '1',
359 'section' => 'export',
360 ],
361 [
362 'name' => 'export_mobi',
363 'value' => '1',
364 'section' => 'export',
365 ],
366 [
367 'name' => 'export_pdf',
368 'value' => '1',
369 'section' => 'export',
370 ],
371 [
372 'name' => 'export_csv',
373 'value' => '1',
374 'section' => 'export',
375 ],
376 [
377 'name' => 'export_json',
378 'value' => '1',
379 'section' => 'export',
380 ],
381 [
382 'name' => 'export_txt',
383 'value' => '1',
384 'section' => 'export',
385 ],
386 [
387 'name' => 'export_xml',
388 'value' => '1',
389 'section' => 'export',
390 ],
391 [
392 'name' => 'import_with_redis',
393 'value' => '0',
394 'section' => 'import',
395 ],
396 [
397 'name' => 'import_with_rabbitmq',
398 'value' => '0',
399 'section' => 'import',
400 ],
401 [
402 'name' => 'show_printlink',
403 'value' => '1',
404 'section' => 'entry',
405 ],
406 [
407 'name' => 'wallabag_support_url',
408 'value' => 'https://www.wallabag.org/pages/support.html',
409 'section' => 'misc',
410 ],
411 [
412 'name' => 'wallabag_url',
413 'value' => '',
414 'section' => 'misc',
415 ],
416 [
417 'name' => 'piwik_enabled',
418 'value' => '0',
419 'section' => 'analytics',
420 ],
421 [
422 'name' => 'piwik_host',
423 'value' => 'v2.wallabag.org',
424 'section' => 'analytics',
425 ],
426 [
427 'name' => 'piwik_site_id',
428 'value' => '1',
429 'section' => 'analytics',
430 ],
431 [
432 'name' => 'demo_mode_enabled',
433 'value' => '0',
434 'section' => 'misc',
435 ],
436 [
437 'name' => 'demo_mode_username',
438 'value' => 'wallabag',
439 'section' => 'misc',
440 ],
441 [
442 'name' => 'download_images_enabled',
443 'value' => '0',
444 'section' => 'misc',
445 ],
446 [
447 'name' => 'restricted_access',
448 'value' => '0',
449 'section' => 'entry',
450 ],
451 ];
452
453 foreach ($settings as $setting) {
454 $newSetting = new Setting(); 296 $newSetting = new Setting();
455 $newSetting->setName($setting['name']); 297 $newSetting->setName($setting['name']);
456 $newSetting->setValue($setting['value']); 298 $newSetting->setValue($setting['value']);
diff --git a/src/Wallabag/CoreBundle/DataFixtures/ORM/LoadSettingData.php b/src/Wallabag/CoreBundle/DataFixtures/ORM/LoadSettingData.php
index aaeb9ee9..a52288e6 100644
--- a/src/Wallabag/CoreBundle/DataFixtures/ORM/LoadSettingData.php
+++ b/src/Wallabag/CoreBundle/DataFixtures/ORM/LoadSettingData.php
@@ -6,173 +6,27 @@ use Doctrine\Common\DataFixtures\AbstractFixture;
6use Doctrine\Common\DataFixtures\OrderedFixtureInterface; 6use Doctrine\Common\DataFixtures\OrderedFixtureInterface;
7use Doctrine\Common\Persistence\ObjectManager; 7use Doctrine\Common\Persistence\ObjectManager;
8use Craue\ConfigBundle\Entity\Setting; 8use Craue\ConfigBundle\Entity\Setting;
9use Symfony\Component\DependencyInjection\ContainerAwareInterface;
10use Symfony\Component\DependencyInjection\ContainerInterface;
9 11
10class LoadSettingData extends AbstractFixture implements OrderedFixtureInterface 12class LoadSettingData extends AbstractFixture implements OrderedFixtureInterface, ContainerAwareInterface
11{ 13{
12 /** 14 /**
15 * @var ContainerInterface
16 */
17 private $container;
18
19 public function setContainer(ContainerInterface $container = null)
20 {
21 $this->container = $container;
22 }
23
24 /**
13 * {@inheritdoc} 25 * {@inheritdoc}
14 */ 26 */
15 public function load(ObjectManager $manager) 27 public function load(ObjectManager $manager)
16 { 28 {
17 $settings = [ 29 foreach ($this->container->getParameter('wallabag_core.default_internal_settings') as $setting) {
18 [
19 'name' => 'share_public',
20 'value' => '1',
21 'section' => 'entry',
22 ],
23 [
24 'name' => 'carrot',
25 'value' => '1',
26 'section' => 'entry',
27 ],
28 [
29 'name' => 'share_diaspora',
30 'value' => '1',
31 'section' => 'entry',
32 ],
33 [
34 'name' => 'diaspora_url',
35 'value' => 'http://diasporapod.com',
36 'section' => 'entry',
37 ],
38 [
39 'name' => 'share_unmark',
40 'value' => '1',
41 'section' => 'entry',
42 ],
43 [
44 'name' => 'unmark_url',
45 'value' => 'https://unmark.it',
46 'section' => 'entry',
47 ],
48 [
49 'name' => 'share_shaarli',
50 'value' => '1',
51 'section' => 'entry',
52 ],
53 [
54 'name' => 'share_scuttle',
55 'value' => '1',
56 'section' => 'entry',
57 ],
58 [
59 'name' => 'shaarli_url',
60 'value' => 'http://myshaarli.com',
61 'section' => 'entry',
62 ],
63 [
64 'name' => 'scuttle_url',
65 'value' => 'http://scuttle.org',
66 'section' => 'entry',
67 ],
68 [
69 'name' => 'share_mail',
70 'value' => '1',
71 'section' => 'entry',
72 ],
73 [
74 'name' => 'share_twitter',
75 'value' => '1',
76 'section' => 'entry',
77 ],
78 [
79 'name' => 'export_epub',
80 'value' => '1',
81 'section' => 'export',
82 ],
83 [
84 'name' => 'export_mobi',
85 'value' => '1',
86 'section' => 'export',
87 ],
88 [
89 'name' => 'export_pdf',
90 'value' => '1',
91 'section' => 'export',
92 ],
93 [
94 'name' => 'export_csv',
95 'value' => '1',
96 'section' => 'export',
97 ],
98 [
99 'name' => 'export_json',
100 'value' => '1',
101 'section' => 'export',
102 ],
103 [
104 'name' => 'export_txt',
105 'value' => '1',
106 'section' => 'export',
107 ],
108 [
109 'name' => 'export_xml',
110 'value' => '1',
111 'section' => 'export',
112 ],
113 [
114 'name' => 'import_with_redis',
115 'value' => '0',
116 'section' => 'import',
117 ],
118 [
119 'name' => 'import_with_rabbitmq',
120 'value' => '0',
121 'section' => 'import',
122 ],
123 [
124 'name' => 'show_printlink',
125 'value' => '1',
126 'section' => 'entry',
127 ],
128 [
129 'name' => 'wallabag_support_url',
130 'value' => 'https://www.wallabag.org/pages/support.html',
131 'section' => 'misc',
132 ],
133 [
134 'name' => 'wallabag_url',
135 'value' => 'http://v2.wallabag.org',
136 'section' => 'misc',
137 ],
138 [
139 'name' => 'piwik_enabled',
140 'value' => '0',
141 'section' => 'analytics',
142 ],
143 [
144 'name' => 'piwik_host',
145 'value' => 'v2.wallabag.org',
146 'section' => 'analytics',
147 ],
148 [
149 'name' => 'piwik_site_id',
150 'value' => '1',
151 'section' => 'analytics',
152 ],
153 [
154 'name' => 'demo_mode_enabled',
155 'value' => '0',
156 'section' => 'misc',
157 ],
158 [
159 'name' => 'demo_mode_username',
160 'value' => 'wallabag',
161 'section' => 'misc',
162 ],
163 [
164 'name' => 'download_images_enabled',
165 'value' => '0',
166 'section' => 'misc',
167 ],
168 [
169 'name' => 'restricted_access',
170 'value' => '0',
171 'section' => 'entry',
172 ],
173 ];
174
175 foreach ($settings as $setting) {
176 $newSetting = new Setting(); 30 $newSetting = new Setting();
177 $newSetting->setName($setting['name']); 31 $newSetting->setName($setting['name']);
178 $newSetting->setValue($setting['value']); 32 $newSetting->setValue($setting['value']);
diff --git a/src/Wallabag/CoreBundle/DependencyInjection/Configuration.php b/src/Wallabag/CoreBundle/DependencyInjection/Configuration.php
index 8b5b5744..33df92d3 100644
--- a/src/Wallabag/CoreBundle/DependencyInjection/Configuration.php
+++ b/src/Wallabag/CoreBundle/DependencyInjection/Configuration.php
@@ -52,6 +52,17 @@ class Configuration implements ConfigurationInterface
52 ->scalarNode('api_limit_mass_actions') 52 ->scalarNode('api_limit_mass_actions')
53 ->defaultValue(10) 53 ->defaultValue(10)
54 ->end() 54 ->end()
55 ->arrayNode('default_internal_settings')
56 ->prototype('array')
57 ->children()
58 ->scalarNode('name')->end()
59 ->scalarNode('value')->end()
60 ->enumNode('section')
61 ->values(['entry', 'misc', 'api', 'analytics', 'export', 'import'])
62 ->end()
63 ->end()
64 ->end()
65 ->end()
55 ->end() 66 ->end()
56 ; 67 ;
57 68
diff --git a/src/Wallabag/CoreBundle/DependencyInjection/WallabagCoreExtension.php b/src/Wallabag/CoreBundle/DependencyInjection/WallabagCoreExtension.php
index a2a703cb..b4d8a386 100644
--- a/src/Wallabag/CoreBundle/DependencyInjection/WallabagCoreExtension.php
+++ b/src/Wallabag/CoreBundle/DependencyInjection/WallabagCoreExtension.php
@@ -28,6 +28,7 @@ class WallabagCoreExtension extends Extension
28 $container->setParameter('wallabag_core.fetching_error_message', $config['fetching_error_message']); 28 $container->setParameter('wallabag_core.fetching_error_message', $config['fetching_error_message']);
29 $container->setParameter('wallabag_core.fetching_error_message_title', $config['fetching_error_message_title']); 29 $container->setParameter('wallabag_core.fetching_error_message_title', $config['fetching_error_message_title']);
30 $container->setParameter('wallabag_core.api_limit_mass_actions', $config['api_limit_mass_actions']); 30 $container->setParameter('wallabag_core.api_limit_mass_actions', $config['api_limit_mass_actions']);
31 $container->setParameter('wallabag_core.default_internal_settings', $config['default_internal_settings']);
31 32
32 $loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config')); 33 $loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
33 $loader->load('services.yml'); 34 $loader->load('services.yml');
diff --git a/tests/Wallabag/ApiBundle/Controller/UserRestControllerTest.php b/tests/Wallabag/ApiBundle/Controller/UserRestControllerTest.php
index 3f4969a5..c1095da8 100644
--- a/tests/Wallabag/ApiBundle/Controller/UserRestControllerTest.php
+++ b/tests/Wallabag/ApiBundle/Controller/UserRestControllerTest.php
@@ -27,8 +27,25 @@ class UserRestControllerTest extends WallabagApiTestCase
27 $this->assertEquals('application/json', $this->client->getResponse()->headers->get('Content-Type')); 27 $this->assertEquals('application/json', $this->client->getResponse()->headers->get('Content-Type'));
28 } 28 }
29 29
30 public function testGetUserWithoutAuthentication()
31 {
32 $client = static::createClient();
33 $client->request('GET', '/api/user.json');
34 $this->assertEquals(401, $client->getResponse()->getStatusCode());
35
36 $content = json_decode($client->getResponse()->getContent(), true);
37
38 $this->assertArrayHasKey('error', $content);
39 $this->assertArrayHasKey('error_description', $content);
40
41 $this->assertEquals('access_denied', $content['error']);
42
43 $this->assertEquals('application/json', $client->getResponse()->headers->get('Content-Type'));
44 }
45
30 public function testCreateNewUser() 46 public function testCreateNewUser()
31 { 47 {
48 $this->client->getContainer()->get('craue_config')->set('api_user_registration', 1);
32 $this->client->request('PUT', '/api/user.json', [ 49 $this->client->request('PUT', '/api/user.json', [
33 'username' => 'google', 50 'username' => 'google',
34 'password' => 'googlegoogle', 51 'password' => 'googlegoogle',
@@ -50,30 +67,51 @@ class UserRestControllerTest extends WallabagApiTestCase
50 67
51 $this->assertEquals('application/json', $this->client->getResponse()->headers->get('Content-Type')); 68 $this->assertEquals('application/json', $this->client->getResponse()->headers->get('Content-Type'));
52 69
53 // remove the created user to avoid side effect on other tests 70 $this->client->getContainer()->get('craue_config')->set('api_user_registration', 0);
54 // @todo remove these lines when test will be isolated 71 }
55 $em = $this->client->getContainer()->get('doctrine.orm.entity_manager'); 72
73 public function testCreateNewUserWithoutAuthentication()
74 {
75 // create a new client instead of using $this->client to be sure client isn't authenticated
76 $client = static::createClient();
77 $client->getContainer()->get('craue_config')->set('api_user_registration', 1);
78 $client->request('PUT', '/api/user.json', [
79 'username' => 'google',
80 'password' => 'googlegoogle',
81 'email' => 'wallabag@google.com',
82 ]);
83
84 $this->assertEquals(200, $client->getResponse()->getStatusCode());
85
86 $content = json_decode($client->getResponse()->getContent(), true);
87
88 $this->assertArrayHasKey('id', $content);
89 $this->assertArrayHasKey('email', $content);
90 $this->assertArrayHasKey('username', $content);
91 $this->assertArrayHasKey('created_at', $content);
92 $this->assertArrayHasKey('updated_at', $content);
93
94 $this->assertEquals('wallabag@google.com', $content['email']);
95 $this->assertEquals('google', $content['username']);
56 96
57 $query = $em->createQuery('DELETE FROM Wallabag\CoreBundle\Entity\Config c WHERE c.user = :user_id'); 97 $this->assertEquals('application/json', $client->getResponse()->headers->get('Content-Type'));
58 $query->setParameter('user_id', $content['id']);
59 $query->execute();
60 98
61 $query = $em->createQuery('DELETE FROM Wallabag\UserBundle\Entity\User u WHERE u.id = :id'); 99 $client->getContainer()->get('craue_config')->set('api_user_registration', 0);
62 $query->setParameter('id', $content['id']);
63 $query->execute();
64 } 100 }
65 101
66 public function testCreateNewUserWithExistingEmail() 102 public function testCreateNewUserWithExistingEmail()
67 { 103 {
68 $this->client->request('PUT', '/api/user.json', [ 104 $client = static::createClient();
105 $client->getContainer()->get('craue_config')->set('api_user_registration', 1);
106 $client->request('PUT', '/api/user.json', [
69 'username' => 'admin', 107 'username' => 'admin',
70 'password' => 'googlegoogle', 108 'password' => 'googlegoogle',
71 'email' => 'bigboss@wallabag.org', 109 'email' => 'bigboss@wallabag.org',
72 ]); 110 ]);
73 111
74 $this->assertEquals(400, $this->client->getResponse()->getStatusCode()); 112 $this->assertEquals(400, $client->getResponse()->getStatusCode());
75 113
76 $content = json_decode($this->client->getResponse()->getContent(), true); 114 $content = json_decode($client->getResponse()->getContent(), true);
77 115
78 $this->assertArrayHasKey('error', $content); 116 $this->assertArrayHasKey('error', $content);
79 $this->assertArrayHasKey('username', $content['error']); 117 $this->assertArrayHasKey('username', $content['error']);
@@ -85,26 +123,50 @@ class UserRestControllerTest extends WallabagApiTestCase
85 $this->assertEquals('This value is already used.', $content['error']['username'][0]); 123 $this->assertEquals('This value is already used.', $content['error']['username'][0]);
86 $this->assertEquals('This value is already used.', $content['error']['email'][0]); 124 $this->assertEquals('This value is already used.', $content['error']['email'][0]);
87 125
88 $this->assertEquals('application/json', $this->client->getResponse()->headers->get('Content-Type')); 126 $this->assertEquals('application/json', $client->getResponse()->headers->get('Content-Type'));
127
128 $client->getContainer()->get('craue_config')->set('api_user_registration', 0);
89 } 129 }
90 130
91 public function testCreateNewUserWithTooShortPassword() 131 public function testCreateNewUserWithTooShortPassword()
92 { 132 {
93 $this->client->request('PUT', '/api/user.json', [ 133 $client = static::createClient();
134 $client->getContainer()->get('craue_config')->set('api_user_registration', 1);
135 $client->request('PUT', '/api/user.json', [
94 'username' => 'facebook', 136 'username' => 'facebook',
95 'password' => 'face', 137 'password' => 'face',
96 'email' => 'facebook@wallabag.org', 138 'email' => 'facebook@wallabag.org',
97 ]); 139 ]);
98 140
99 $this->assertEquals(400, $this->client->getResponse()->getStatusCode()); 141 $this->assertEquals(400, $client->getResponse()->getStatusCode());
100 142
101 $content = json_decode($this->client->getResponse()->getContent(), true); 143 $content = json_decode($client->getResponse()->getContent(), true);
102 144
103 $this->assertArrayHasKey('error', $content); 145 $this->assertArrayHasKey('error', $content);
104 $this->assertArrayHasKey('password', $content['error']); 146 $this->assertArrayHasKey('password', $content['error']);
105 147
106 $this->assertEquals('validator.password_too_short', $content['error']['password'][0]); 148 $this->assertEquals('validator.password_too_short', $content['error']['password'][0]);
107 149
108 $this->assertEquals('application/json', $this->client->getResponse()->headers->get('Content-Type')); 150 $this->assertEquals('application/json', $client->getResponse()->headers->get('Content-Type'));
151
152 $client->getContainer()->get('craue_config')->set('api_user_registration', 0);
153 }
154
155 public function testCreateNewUserWhenRegistrationIsDisabled()
156 {
157 $client = static::createClient();
158 $client->request('PUT', '/api/user.json', [
159 'username' => 'facebook',
160 'password' => 'face',
161 'email' => 'facebook@wallabag.org',
162 ]);
163
164 $this->assertEquals(403, $client->getResponse()->getStatusCode());
165
166 $content = json_decode($client->getResponse()->getContent(), true);
167
168 $this->assertArrayHasKey('error', $content);
169
170 $this->assertEquals('application/json', $client->getResponse()->headers->get('Content-Type'));
109 } 171 }
110} 172}
diff --git a/tests/Wallabag/ApiBundle/Controller/WallabagRestControllerTest.php b/tests/Wallabag/ApiBundle/Controller/WallabagRestControllerTest.php
index c87e58de..df638e8f 100644
--- a/tests/Wallabag/ApiBundle/Controller/WallabagRestControllerTest.php
+++ b/tests/Wallabag/ApiBundle/Controller/WallabagRestControllerTest.php
@@ -8,12 +8,14 @@ class WallabagRestControllerTest extends WallabagApiTestCase
8{ 8{
9 public function testGetVersion() 9 public function testGetVersion()
10 { 10 {
11 $this->client->request('GET', '/api/version'); 11 // create a new client instead of using $this->client to be sure client isn't authenticated
12 $client = static::createClient();
13 $client->request('GET', '/api/version');
12 14
13 $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); 15 $this->assertEquals(200, $client->getResponse()->getStatusCode());
14 16
15 $content = json_decode($this->client->getResponse()->getContent(), true); 17 $content = json_decode($client->getResponse()->getContent(), true);
16 18
17 $this->assertEquals($this->client->getContainer()->getParameter('wallabag_core.version'), $content); 19 $this->assertEquals($client->getContainer()->getParameter('wallabag_core.version'), $content);
18 } 20 }
19} 21}