Skip to content

Commit 0c4923d

Browse files
committed
Fix Travis tests
1 parent b59b955 commit 0c4923d

File tree

14 files changed

+121
-18
lines changed

14 files changed

+121
-18
lines changed

.travis.yml

+9-5
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,20 @@
11
language: php
22

33
php:
4-
- 5.4
5-
- 5.5
6-
- 5.6
7-
- 7.0
4+
- 7.2
5+
- 7.3
6+
7+
services:
8+
- postgresql
89

910
before_script:
11+
- psql -c 'create database symfony_test;' -U postgres
1012
- phpenv config-rm xdebug.ini
1113
- cd back-end
1214
- composer self-update
1315
- composer install
16+
- APP_ENV=test bin/console doctrine:schema:create -q
17+
- APP_ENV=test bin/console doctrine:fixtures:load -q --purge-with-truncate
1418

1519
script:
16-
- phpunit -c app
20+
- phpunit

README.md

+6-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
angular-symfony
1+
angular-symfony [![Build Status](https://travis-ci.org/FlyersWeb/angular-symfony.svg?branch=upgrade)](https://travis-ci.org/FlyersWeb/angular-symfony)
22
===============
33

44
Project Bootstrap for an Angular 2+ and Symfony 4+ webservices project.
@@ -13,7 +13,7 @@ Buy me a coffee
1313

1414
[![Buy me a coffee](https://raw.githubusercontent.com/FlyersWeb/angular-symfony/upgrade/buy-me-a-coffee.png)](https://paypal.me/nac1dbois)
1515

16-
I'm working on this project in my free time and offering it free of charges. To help me work more on this you can help me by sending me a tip.
16+
I'm working on this project in my free time and offering it free of charges. To help me work more on this you send me tips to buy more coffee :)
1717

1818
Installation
1919
------------
@@ -32,6 +32,10 @@ Log in application docker image :
3232

3333
docker-compose exec application bash
3434

35+
Install dependencies :
36+
37+
composer install
38+
3539
Update schemas (FOSUserBundle) :
3640

3741
php bin/console doctrine:schema:create

back-end/.env

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ APP_SECRET=abb685fe97081c338ed39e8b1cb4bd69
2525
# For an SQLite database, use: "sqlite:///%kernel.project_dir%/var/data.db"
2626
# For a PostgreSQL database, use: "postgresql://db_user:[email protected]:5432/db_name?serverVersion=11"
2727
# IMPORTANT: You MUST configure your db driver and server version, either here or in config/packages/doctrine.yaml
28-
DATABASE_URL=postgresql://postgres:postgres@database:5432/symfony_test?serverVersion=10.10
28+
DATABASE_URL=postgresql://postgres:postgres@database:5432/symfony_dev?serverVersion=10.10
2929
###< doctrine/doctrine-bundle ###
3030

3131
###> symfony/swiftmailer-bundle ###

back-end/.env.test

+1
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ KERNEL_CLASS='App\Kernel'
33
APP_SECRET='$ecretf0rt3st'
44
SYMFONY_DEPRECATIONS_HELPER=999999
55
PANTHER_APP_ENV=panther
6+
DATABASE_URL=postgresql://postgres@localhost:5432/symfony_test?serverVersion=9.2

back-end/composer.json

+2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"ext-ctype": "*",
77
"ext-iconv": "*",
88
"friendsofsymfony/rest-bundle": "^2.6",
9+
"friendsofsymfony/user-bundle": "^2.0",
910
"nelmio/cors-bundle": "^2.0",
1011
"sensio/framework-extra-bundle": "^5.1",
1112
"symfony/asset": "4.3.*",
@@ -34,6 +35,7 @@
3435
"doctrine/doctrine-fixtures-bundle": "^3.3",
3536
"symfony/debug-pack": "*",
3637
"symfony/maker-bundle": "^1.0",
38+
"symfony/phpunit-bridge": "^4.3",
3739
"symfony/profiler-pack": "*",
3840
"symfony/test-pack": "*",
3941
"symfony/web-server-bundle": "4.3.*"

back-end/src/Controller/DefaultController.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,6 @@ public function loginAction(Request $request, UserManagerInterface $userManager,
4242
throw new HttpException(403, "Error User Bad Password");
4343
}
4444

45-
return $this->json(['secret' => $user->getPassword()]);
45+
return $this->json(['secret' => $user->getSalt()]);
4646
}
4747
}

back-end/src/Security/Authentication/Provider/WsseProvider.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public function authenticate(TokenInterface $token)
2727
$user = $this->userProvider->loadUserByUsername($token->getUsername());
2828

2929
// Check digest and replay attempts and return token with user roles
30-
if ($user && $this->validateDigest($token->digest, $token->nonce, $token->created, $user->getPassword())) {
30+
if ($user && $this->validateDigest($token->digest, $token->nonce, $token->created, $user->getSalt())) {
3131
$authenticatedToken = new WsseUserToken($user->getRoles());
3232
$authenticatedToken->setUser($user);
3333

@@ -49,15 +49,15 @@ protected function validateDigest($digest, $nonce, $created, $secret)
4949
if (strtotime($created) > time()) {
5050
return false;
5151
}
52-
52+
5353
// Expire timestamp after 5 minutes
5454
if (time() - strtotime($created) > self::TOKEN_DURATION) {
5555
return false;
5656
}
57-
57+
5858
// Try to fetch the cache item from pool
5959
$cacheItem = $this->cachePool->getItem(md5($nonce));
60-
60+
6161
// Validate that the nonce is *not* in cache
6262
// if it is, this could be a replay attack
6363
if ($cacheItem->isHit()) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
3+
namespace App\Test\Controller;
4+
5+
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
6+
7+
class ApiControllerTest extends WebTestCase
8+
{
9+
public function testGetHelloWhithoutToken()
10+
{
11+
$client = static::createClient();
12+
13+
$client->request('GET', '/api/hello');
14+
15+
$this->assertEquals(401, $client->getResponse()->getStatusCode());
16+
}
17+
18+
public function testGetHelloWhithToken()
19+
{
20+
$client = static::createClient();
21+
// User created by running doctrine fixtures
22+
$client->request('POST', '/login', ['username' => 'bob', 'password' => 'Abc123']);
23+
// Get user secret
24+
$content = json_decode($client->getResponse()->getContent(), true);
25+
26+
// User created by running doctrine fixtures
27+
$username = 'bob';
28+
// User secret returned after login
29+
$secret = $content['secret'];
30+
// Generate a random string to prevent replay attacks
31+
$nonce = base64_encode(substr(md5(rand()), 0, 10));
32+
// Token work for 5 minutes
33+
$created = date("Y-m-d\TH:i:s\Z", strtotime('now -2 minute'));
34+
// Generate the shared secret digest
35+
$digest = base64_encode(sha1(base64_decode($nonce) . $created . $secret, true));
36+
// X-WSSE header sent
37+
$userToken = 'UsernameToken Username="' . $username . '", PasswordDigest="' . $digest . '", Nonce="' . $nonce . '", Created="' . $created . '"';
38+
39+
$client = static::createClient();
40+
$client->request('GET', '/api/hello', [], [], [
41+
'HTTP_X-WSSE' => $userToken
42+
]);
43+
44+
$this->assertEquals(200, $client->getResponse()->getStatusCode());
45+
$content = json_decode($client->getResponse()->getContent(), true);
46+
$this->assertEquals('world', $content['hello']);
47+
}
48+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
namespace App\Test\Controller;
4+
5+
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
6+
7+
class DefaultControllerTest extends WebTestCase
8+
{
9+
public function testGetIndex()
10+
{
11+
$client = static::createClient();
12+
13+
$client->request('GET', '/');
14+
15+
$this->assertEquals(200, $client->getResponse()->getStatusCode());
16+
}
17+
18+
public function testPostLogin()
19+
{
20+
$client = static::createClient();
21+
// User created by running doctrine fixtures
22+
$client->request('POST', '/login', ['username' => 'bob', 'password' => 'Abc123']);
23+
24+
$this->assertEquals(200, $client->getResponse()->getStatusCode());
25+
// Get the user secret (we use the salt because it can be disclosed)
26+
$content = json_decode($client->getResponse()->getContent(), true);
27+
$this->assertEquals("e4TNCCgLvPbDRh7ih+pK58pab0NToFzdZHuPmA0e", $content['secret']);
28+
}
29+
}

dockerify/docker-compose.yml

-3
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,6 @@ application:
22
build: ../back-end
33
volumes:
44
- ../back-end:/var/www
5-
# - ./php-fpm/php.ini:/usr/local/etc/php/php.ini
6-
# - ./php-fpm/timezone.ini:/usr/local/etc/php/conf.d/timezone.ini
7-
# - ./php-fpm/symfony.pool.conf:/usr/local/etc/php-fpm.d/symfony.pool.conf
85
links:
96
- database
107

front-end/.tool-versions

-1
This file was deleted.

front-end/src/app/hello/hello.component.spec.ts

+6
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
2+
import { HttpClientTestingModule } from '@angular/common/http/testing';
3+
import { RouterTestingModule } from '@angular/router/testing';
24

35
import { HelloComponent } from './hello.component';
46

@@ -8,6 +10,10 @@ describe('HelloComponent', () => {
810

911
beforeEach(async(() => {
1012
TestBed.configureTestingModule({
13+
imports: [
14+
HttpClientTestingModule,
15+
RouterTestingModule,
16+
],
1117
declarations: [ HelloComponent ]
1218
})
1319
.compileComponents();

front-end/src/app/login/login.component.spec.ts

+8
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,21 @@
11
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
22

33
import { LoginComponent } from './login.component';
4+
import { ReactiveFormsModule } from '@angular/forms';
5+
import { HttpClientTestingModule } from '@angular/common/http/testing';
6+
import { RouterTestingModule } from '@angular/router/testing';
47

58
describe('LoginComponent', () => {
69
let component: LoginComponent;
710
let fixture: ComponentFixture<LoginComponent>;
811

912
beforeEach(async(() => {
1013
TestBed.configureTestingModule({
14+
imports: [
15+
ReactiveFormsModule,
16+
HttpClientTestingModule,
17+
RouterTestingModule
18+
],
1119
declarations: [ LoginComponent ]
1220
})
1321
.compileComponents();

front-end/src/app/wsse.service.spec.ts

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
import { TestBed } from '@angular/core/testing';
22

33
import { WSSEService } from './wsse.service';
4+
import { HttpClientTestingModule } from '@angular/common/http/testing';
45

56
describe('WSSEService', () => {
6-
beforeEach(() => TestBed.configureTestingModule({}));
7+
beforeEach(() => TestBed.configureTestingModule({
8+
imports: [
9+
HttpClientTestingModule,
10+
]
11+
}));
712

813
it('should be created', () => {
914
const service: WSSEService = TestBed.get(WSSEService);

0 commit comments

Comments
 (0)