Added basic API endpoints (#258)

This commit is contained in:
Kevin Papst
2018-08-08 23:10:45 +02:00
committed by GitHub
parent a7780dac9e
commit 2f84f3751a
59 changed files with 3207 additions and 297 deletions

View File

@@ -52,15 +52,13 @@ class UserControllerTest extends ControllerBaseTest
$this->assertIsRedirect($client, $this->createUrl('/profile/' . urlencode($username) . '/edit'));
$client->followRedirect();
$expectedTabs = ['#charts', '#settings', '#password', '#api-token', '#roles'];
$tabs = $client->getCrawler()->filter('div.nav-tabs-custom ul.nav-tabs li');
$this->assertEquals(4, $tabs->count());
$expectedTabs = ['#charts', '#settings', '#password', '#roles'];
$this->assertEquals(count($expectedTabs), $tabs->count());
$foundTabs = [];
foreach ($tabs->filter('a') as $tab) {
$name = $tab->getAttribute('href');
if (in_array($name, $expectedTabs)) {
$foundTabs[] = $name;
}
$foundTabs[] = $tab->getAttribute('href');
}
$this->assertEmpty(array_diff($expectedTabs, $foundTabs));

View File

@@ -72,5 +72,27 @@ class CalendarControllerTest extends ControllerBaseTest
$this->assertInternalType('array', $json);
$this->assertNotEmpty($json);
$this->assertEquals(10, count($json));
foreach ($json as $result) {
$this->assertInternalType('array', $result);
$this->assertCalendarStructure($result);
}
}
protected function assertCalendarStructure(array $result)
{
$this->assertArrayHasKey('id', $result);
$this->assertArrayHasKey('start', $result);
$this->assertArrayHasKey('title', $result);
$this->assertArrayHasKey('description', $result);
$this->assertArrayHasKey('customer', $result);
$this->assertArrayHasKey('project', $result);
$this->assertArrayHasKey('activity', $result);
$this->assertArrayHasKey('borderColor', $result);
$this->assertArrayHasKey('backgroundColor', $result);
if (isset($result['end'])) {
$this->assertNull($result['borderColor']);
$this->assertNull($result['backgroundColor']);
}
}
}

View File

@@ -96,18 +96,18 @@ abstract class ControllerBaseTest extends WebTestCase
*/
protected function assertRequestIsSecured(Client $client, string $url, $method = 'GET')
{
$client->request($method, $this->createUrl($url));
$this->request($client, $url, $method);
/* @var RedirectResponse $response */
$response = $client->getResponse();
$this->assertTrue(
$response->isRedirect(),
sprintf('The secure URL %s is not protected.', $url . $response->getContent())
sprintf('The secure URL %s is not protected.', $url)
);
$this->assertEquals(
'http://localhost' . $this->createUrl('/login'),
$this->assertStringEndsWith(
'/login',
$response->getTargetUrl(),
sprintf('The secure URL %s does not redirect to the login form.', $url)
);

View File

@@ -10,10 +10,13 @@
namespace App\Tests\Controller;
use App\DataFixtures\UserFixtures;
use App\Entity\User;
use Symfony\Component\Security\Core\Encoder\EncoderFactoryInterface;
/**
* @coversDefaultClass \App\Controller\InvoiceController
* @group integration
* @group legacy
*/
class ProfileControllerTest extends ControllerBaseTest
{
@@ -28,15 +31,13 @@ class ProfileControllerTest extends ControllerBaseTest
$this->request($client, '/profile/' . UserFixtures::USERNAME_USER);
$this->assertTrue($client->getResponse()->isSuccessful());
$expectedTabs = ['#charts', '#settings', '#password', '#api-token', '#preferences'];
$tabs = $client->getCrawler()->filter('div.nav-tabs-custom ul.nav-tabs li');
$this->assertEquals(4, $tabs->count());
$expectedTabs = ['#charts', '#settings', '#password', '#preferences'];
$this->assertEquals(count($expectedTabs), $tabs->count());
$foundTabs = [];
foreach ($tabs->filter('a') as $tab) {
$name = $tab->getAttribute('href');
if (in_array($name, $expectedTabs)) {
$foundTabs[] = $name;
}
$foundTabs[] = $tab->getAttribute('href');
}
$this->assertEmpty(array_diff($expectedTabs, $foundTabs));
}
@@ -47,4 +48,74 @@ class ProfileControllerTest extends ControllerBaseTest
$this->request($client, '/profile/' . UserFixtures::USERNAME_TEAMLEAD);
$this->assertFalse($client->getResponse()->isSuccessful());
}
public function testUpdateApiToken()
{
$client = $this->getClientForAuthenticatedUser(User::ROLE_USER);
$this->request($client, '/profile/' . UserFixtures::USERNAME_USER);
/** @var User $user */
$user = $client->getContainer()->get('security.token_storage')->getToken()->getUser();
/** @var EncoderFactoryInterface $passwordEncoder */
$passwordEncoder = $client->getContainer()->get('test.PasswordEncoder');
$this->assertTrue($passwordEncoder->getEncoder($user)->isPasswordValid($user->getApiToken(), UserFixtures::DEFAULT_API_TOKEN, $user->getSalt()));
$this->assertFalse($passwordEncoder->getEncoder($user)->isPasswordValid($user->getApiToken(), 'test123', $user->getSalt()));
$this->assertEquals(UserFixtures::USERNAME_USER, $user->getUsername());
$form = $client->getCrawler()->filter('form[name=user_api_token]')->form();
$client->submit($form, [
'user_api_token' => [
'plainApiToken' => [
'first' => 'test123',
'second' => 'test123',
]
]
]);
$this->assertIsRedirect($client, $this->createUrl('/profile/' . urlencode(UserFixtures::USERNAME_USER)));
$client->followRedirect();
$this->assertTrue($client->getResponse()->isSuccessful());
$this->assertHasFlashSuccess($client);
$user = $client->getContainer()->get('security.token_storage')->getToken()->getUser();
$this->assertFalse($passwordEncoder->getEncoder($user)->isPasswordValid($user->getApiToken(), UserFixtures::DEFAULT_API_TOKEN, $user->getSalt()));
$this->assertTrue($passwordEncoder->getEncoder($user)->isPasswordValid($user->getApiToken(), 'test123', $user->getSalt()));
}
public function testUpdatePassword()
{
$client = $this->getClientForAuthenticatedUser(User::ROLE_USER);
$this->request($client, '/profile/' . UserFixtures::USERNAME_USER);
/** @var User $user */
$user = $client->getContainer()->get('security.token_storage')->getToken()->getUser();
/** @var EncoderFactoryInterface $passwordEncoder */
$passwordEncoder = $client->getContainer()->get('test.PasswordEncoder');
$this->assertTrue($passwordEncoder->getEncoder($user)->isPasswordValid($user->getPassword(), UserFixtures::DEFAULT_PASSWORD, $user->getSalt()));
$this->assertFalse($passwordEncoder->getEncoder($user)->isPasswordValid($user->getPassword(), 'test123', $user->getSalt()));
$this->assertEquals(UserFixtures::USERNAME_USER, $user->getUsername());
$form = $client->getCrawler()->filter('form[name=user_password]')->form();
$client->submit($form, [
'user_password' => [
'plainPassword' => [
'first' => 'test123',
'second' => 'test123',
]
]
]);
$this->assertIsRedirect($client, $this->createUrl('/profile/' . urlencode(UserFixtures::USERNAME_USER)));
$client->followRedirect();
$this->assertTrue($client->getResponse()->isSuccessful());
$this->assertHasFlashSuccess($client);
$user = $client->getContainer()->get('security.token_storage')->getToken()->getUser();
$this->assertFalse($passwordEncoder->getEncoder($user)->isPasswordValid($user->getPassword(), UserFixtures::DEFAULT_PASSWORD, $user->getSalt()));
$this->assertTrue($passwordEncoder->getEncoder($user)->isPasswordValid($user->getPassword(), 'test123', $user->getSalt()));
}
}