mirror of
https://gitlab.com/omair-personal/exchange.git
synced 2026-08-19 04:24:08 +00:00
merge master
This commit is contained in:
@@ -0,0 +1,54 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Browser;
|
||||
|
||||
use App\User;
|
||||
use Tests\DuskTestCase;
|
||||
use Tests\Browser\Pages\Home;
|
||||
use Tests\Browser\Pages\Login;
|
||||
|
||||
class LoginTest extends DuskTestCase
|
||||
{
|
||||
public function setUp()
|
||||
{
|
||||
parent::setup();
|
||||
|
||||
static::closeAll();
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function login_with_valid_credentials()
|
||||
{
|
||||
$user = factory(User::class)->create();
|
||||
|
||||
$this->browse(function ($browser) use ($user) {
|
||||
$browser->visit(new Login)
|
||||
->submit($user->email, 'secret')
|
||||
->assertPageIs(Home::class);
|
||||
});
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function login_with_invalid_credentials()
|
||||
{
|
||||
$this->browse(function ($browser) {
|
||||
$browser->visit(new Login)
|
||||
->submit('test@test.app', 'secret')
|
||||
->assertSee('These credentials do not match our records.');
|
||||
});
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function log_out_the_user()
|
||||
{
|
||||
$user = factory(User::class)->create();
|
||||
|
||||
$this->browse(function ($browser) use ($user) {
|
||||
$browser->visit(new Login)
|
||||
->submit($user->email, 'secret')
|
||||
->on(new Home)
|
||||
->clickLogout()
|
||||
->assertPageIs(Login::class);
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Browser\Pages;
|
||||
|
||||
class Home extends Page
|
||||
{
|
||||
/**
|
||||
* Get the URL for the page.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function url()
|
||||
{
|
||||
return '/home';
|
||||
}
|
||||
|
||||
/**
|
||||
* Click on the log out link.
|
||||
*
|
||||
* @param \Laravel\Dusk\Browser $browser
|
||||
* @return void
|
||||
*/
|
||||
public function clickLogout($browser)
|
||||
{
|
||||
$browser->clickLink('Logout')
|
||||
->pause(300);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Browser\Pages;
|
||||
|
||||
class Login extends Page
|
||||
{
|
||||
/**
|
||||
* Get the URL for the page.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function url()
|
||||
{
|
||||
return '/login';
|
||||
}
|
||||
|
||||
/**
|
||||
* Submit the form with the given credentials.
|
||||
*
|
||||
* @param \Laravel\Dusk\Browser $browser
|
||||
* @param string $email
|
||||
* @param string $password
|
||||
* @return void
|
||||
*/
|
||||
public function submit($browser, $email, $password)
|
||||
{
|
||||
$browser->type('email', $email)
|
||||
->type('password', $password)
|
||||
->press('Log In')
|
||||
->pause(350);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Browser\Pages;
|
||||
|
||||
use Laravel\Dusk\Browser;
|
||||
use Laravel\Dusk\Page as BasePage;
|
||||
|
||||
abstract class Page extends BasePage
|
||||
{
|
||||
/**
|
||||
* Assert that the browser is on the page.
|
||||
*
|
||||
* @param Browser $browser
|
||||
* @return void
|
||||
*/
|
||||
public function assert(Browser $browser)
|
||||
{
|
||||
$browser->assertPathIs($this->url());
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the global element shortcuts for the site.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public static function siteElements()
|
||||
{
|
||||
return [
|
||||
'@element' => '#selector',
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Browser\Pages;
|
||||
|
||||
class Register extends Page
|
||||
{
|
||||
/**
|
||||
* Get the URL for the page.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function url()
|
||||
{
|
||||
return '/register';
|
||||
}
|
||||
|
||||
/**
|
||||
* Submit the form with the given data.
|
||||
*
|
||||
* @param \Laravel\Dusk\Browser $browser
|
||||
* @param array $data
|
||||
* @return void
|
||||
*/
|
||||
public function submit($browser, array $data = [])
|
||||
{
|
||||
foreach ($data as $key => $value) {
|
||||
$browser->type($key, $value);
|
||||
}
|
||||
|
||||
$browser->press('Register')
|
||||
->pause(500);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Browser;
|
||||
|
||||
use App\User;
|
||||
use Tests\DuskTestCase;
|
||||
use Tests\Browser\Pages\Home;
|
||||
use Tests\Browser\Pages\Register;
|
||||
|
||||
class RegisterTest extends DuskTestCase
|
||||
{
|
||||
public function setUp()
|
||||
{
|
||||
parent::setup();
|
||||
|
||||
static::closeAll();
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function register_with_valid_data()
|
||||
{
|
||||
$this->browse(function ($browser) {
|
||||
$browser->visit(new Register)
|
||||
->submit([
|
||||
'name' => 'Test User',
|
||||
'email' => 'test@test.app',
|
||||
'password' => 'secret',
|
||||
'password_confirmation' => 'secret',
|
||||
])
|
||||
->assertPageIs(Home::class);
|
||||
});
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function can_not_register_with_the_same_twice()
|
||||
{
|
||||
$user = factory(User::class)->create();
|
||||
|
||||
$this->browse(function ($browser) use ($user) {
|
||||
$browser->visit(new Register)
|
||||
->submit([
|
||||
'name' => 'Test User',
|
||||
'email' => $user->email,
|
||||
'password' => 'secret',
|
||||
'password_confirmation' => 'secret',
|
||||
])
|
||||
->assertSee('The email has already been taken.');
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Browser;
|
||||
|
||||
use Tests\DuskTestCase;
|
||||
|
||||
class WelcomeTest extends DuskTestCase
|
||||
{
|
||||
/** @test */
|
||||
public function basic_test()
|
||||
{
|
||||
$this->browse(function ($browser) {
|
||||
$browser->visit('/')
|
||||
->assertSee('Laravel');
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
*
|
||||
!.gitignore
|
||||
@@ -0,0 +1,2 @@
|
||||
*
|
||||
!.gitignore
|
||||
@@ -18,7 +18,7 @@ trait CreatesApplication
|
||||
|
||||
$app->make(Kernel::class)->bootstrap();
|
||||
|
||||
Hash::driver('bcrypt')->setRounds(4);
|
||||
Hash::setRounds(4);
|
||||
|
||||
return $app;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,55 @@
|
||||
<?php
|
||||
|
||||
namespace Tests;
|
||||
|
||||
use Laravel\Dusk\Page;
|
||||
use Laravel\Dusk\Browser;
|
||||
use Laravel\Dusk\TestCase as BaseTestCase;
|
||||
use Facebook\WebDriver\Chrome\ChromeOptions;
|
||||
use Facebook\WebDriver\Remote\RemoteWebDriver;
|
||||
use Facebook\WebDriver\Remote\DesiredCapabilities;
|
||||
use Illuminate\Foundation\Testing\DatabaseMigrations;
|
||||
|
||||
Browser::macro('assertPageIs', function ($page) {
|
||||
if (! $page instanceof Page) {
|
||||
$page = new $page;
|
||||
}
|
||||
|
||||
return $this->assertPathIs($page->url());
|
||||
});
|
||||
|
||||
abstract class DuskTestCase extends BaseTestCase
|
||||
{
|
||||
use DatabaseMigrations;
|
||||
use CreatesApplication;
|
||||
|
||||
/**
|
||||
* Prepare for Dusk test execution.
|
||||
*
|
||||
* @beforeClass
|
||||
* @return void
|
||||
*/
|
||||
public static function prepare()
|
||||
{
|
||||
static::startChromeDriver();
|
||||
}
|
||||
|
||||
/**
|
||||
* Create the RemoteWebDriver instance.
|
||||
*
|
||||
* @return \Facebook\WebDriver\Remote\RemoteWebDriver
|
||||
*/
|
||||
protected function driver()
|
||||
{
|
||||
$options = (new ChromeOptions)->addArguments([
|
||||
'--disable-gpu',
|
||||
'--headless',
|
||||
]);
|
||||
|
||||
return RemoteWebDriver::create(
|
||||
'http://localhost:9515', DesiredCapabilities::chrome()->setCapability(
|
||||
ChromeOptions::CAPABILITY, $options
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Feature;
|
||||
|
||||
use Tests\TestCase;
|
||||
|
||||
class LocaleTest extends TestCase
|
||||
{
|
||||
/** @test */
|
||||
public function set_locale_from_header()
|
||||
{
|
||||
$this->withHeaders(['Accept-Language' => 'zh-CN'])
|
||||
->postJson('/api/login');
|
||||
|
||||
$this->assertEquals('zh-CN', $this->app->getLocale());
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function set_locale_from_header_short()
|
||||
{
|
||||
$this->withHeaders(['Accept-Language' => 'en-US'])
|
||||
->postJson('/api/login');
|
||||
|
||||
$this->assertEquals('en', $this->app->getLocale());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Feature;
|
||||
|
||||
use App\User;
|
||||
use Tests\TestCase;
|
||||
|
||||
class LoginTest extends TestCase
|
||||
{
|
||||
/** @var \App\User */
|
||||
protected $user;
|
||||
|
||||
public function setUp()
|
||||
{
|
||||
parent::setUp();
|
||||
|
||||
$this->user = factory(User::class)->create();
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function authenticate()
|
||||
{
|
||||
$this->postJson('/api/login', [
|
||||
'email' => $this->user->email,
|
||||
'password' => 'secret',
|
||||
])
|
||||
->assertSuccessful()
|
||||
->assertJsonStructure(['token', 'expires_in'])
|
||||
->assertJson(['token_type' => 'bearer']);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function fetch_the_current_user()
|
||||
{
|
||||
$this->actingAs($this->user)
|
||||
->getJson('/api/user')
|
||||
->assertSuccessful()
|
||||
->assertJsonStructure(['id', 'name', 'email']);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function log_out()
|
||||
{
|
||||
$token = $this->postJson('/api/login', [
|
||||
'email' => $this->user->email,
|
||||
'password' => 'secret',
|
||||
])->json()['token'];
|
||||
|
||||
$this->postJson("/api/logout?token=$token")
|
||||
->assertSuccessful();
|
||||
|
||||
$this->getJson("/api/user?token=$token")
|
||||
->assertStatus(401);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,129 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Feature;
|
||||
|
||||
use App\User;
|
||||
use Mockery as m;
|
||||
use Tests\TestCase;
|
||||
use Laravel\Socialite\Facades\Socialite;
|
||||
use PHPUnit\Framework\Assert as PHPUnit;
|
||||
use Illuminate\Foundation\Testing\TestResponse;
|
||||
use Laravel\Socialite\Two\User as SocialiteUser;
|
||||
|
||||
class OAuthTest extends TestCase
|
||||
{
|
||||
public function setUp()
|
||||
{
|
||||
parent::setUp();
|
||||
|
||||
TestResponse::macro('assertText', function ($text) {
|
||||
PHPUnit::assertTrue(str_contains($this->getContent(), $text), "Expected text [{$text}] not found.");
|
||||
|
||||
return $this;
|
||||
});
|
||||
|
||||
TestResponse::macro('assertTextMissing', function ($text) {
|
||||
PHPUnit::assertFalse(str_contains($this->getContent(), $text), "Expected missing text [{$text}] found.");
|
||||
|
||||
return $this;
|
||||
});
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function redirect_to_provider()
|
||||
{
|
||||
$this->mockSocialite('github');
|
||||
|
||||
$this->postJson('/api/oauth/github')
|
||||
->assertSuccessful()
|
||||
->assertJson(['url' => 'https://url-to-provider']);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function create_user_and_return_token()
|
||||
{
|
||||
$this->mockSocialite('github', [
|
||||
'id' => '123',
|
||||
'name' => 'Test User',
|
||||
'email' => 'test@example.com',
|
||||
'token' => 'access-token',
|
||||
'refreshToken' => 'refresh-token',
|
||||
]);
|
||||
|
||||
$this->withoutExceptionHandling();
|
||||
|
||||
$this->get('/api/oauth/github/callback')
|
||||
->assertText('token')
|
||||
->assertSuccessful();
|
||||
|
||||
$this->assertDatabaseHas('users', [
|
||||
'name' => 'Test User',
|
||||
'email' => 'test@example.com',
|
||||
]);
|
||||
|
||||
$this->assertDatabaseHas('oauth_providers', [
|
||||
'user_id' => User::first()->id,
|
||||
'provider' => 'github',
|
||||
'provider_user_id' => '123',
|
||||
'access_token' => 'access-token',
|
||||
'refresh_token' => 'refresh-token',
|
||||
]);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function update_user_and_return_token()
|
||||
{
|
||||
$user = factory(User::class)->create(['email' => 'test@example.com']);
|
||||
$user->oauthProviders()->create([
|
||||
'provider' => 'github',
|
||||
'provider_user_id' => '123',
|
||||
]);
|
||||
|
||||
$this->mockSocialite('github', [
|
||||
'id' => '123',
|
||||
'email' => 'test@example.com',
|
||||
'token' => 'updated-access-token',
|
||||
'refreshToken' => 'updated-refresh-token',
|
||||
]);
|
||||
|
||||
$this->get('/api/oauth/github/callback')
|
||||
->assertText('token')
|
||||
->assertSuccessful();
|
||||
|
||||
$this->assertDatabaseHas('oauth_providers', [
|
||||
'user_id' => $user->id,
|
||||
'access_token' => 'updated-access-token',
|
||||
'refresh_token' => 'updated-refresh-token',
|
||||
]);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function can_not_create_user_if_email_is_taken()
|
||||
{
|
||||
factory(User::class)->create(['email' => 'test@example.com']);
|
||||
|
||||
$this->mockSocialite('github', ['email' => 'test@example.com']);
|
||||
|
||||
$this->get('/api/oauth/github/callback')
|
||||
->assertText('Email already taken.')
|
||||
->assertTextMissing('token')
|
||||
->assertStatus(400);
|
||||
}
|
||||
|
||||
protected function mockSocialite($provider, $user = null)
|
||||
{
|
||||
$mock = Socialite::shouldReceive('stateless')
|
||||
->andReturn(m::self())
|
||||
->shouldReceive('driver')
|
||||
->with($provider)
|
||||
->andReturn(m::self());
|
||||
|
||||
if ($user) {
|
||||
$mock->shouldReceive('user')
|
||||
->andReturn((new SocialiteUser)->setRaw($user)->map($user));
|
||||
} else {
|
||||
$mock->shouldReceive('redirect')
|
||||
->andReturn(redirect('https://url-to-provider'));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Feature;
|
||||
|
||||
use Tests\TestCase;
|
||||
|
||||
class RegisterTest extends TestCase
|
||||
{
|
||||
/** @test */
|
||||
public function can_register()
|
||||
{
|
||||
$this->postJson('/api/register', [
|
||||
'name' => 'Test User',
|
||||
'email' => 'test@test.app',
|
||||
'password' => 'secret',
|
||||
'password_confirmation' => 'secret',
|
||||
])
|
||||
->assertSuccessful()
|
||||
->assertJsonStructure(['id', 'name', 'email']);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Feature;
|
||||
|
||||
use App\User;
|
||||
use Tests\TestCase;
|
||||
use Illuminate\Support\Facades\Hash;
|
||||
|
||||
class SettingsTest extends TestCase
|
||||
{
|
||||
/** @var \App\User */
|
||||
protected $user;
|
||||
|
||||
public function setUp()
|
||||
{
|
||||
parent::setUp();
|
||||
|
||||
$this->user = factory(User::class)->create();
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function update_profile_info()
|
||||
{
|
||||
$this->actingAs($this->user)
|
||||
->patchJson('/api/settings/profile', [
|
||||
'name' => 'Test User',
|
||||
'email' => 'test@test.app',
|
||||
])
|
||||
->assertSuccessful()
|
||||
->assertJsonStructure(['id', 'name', 'email']);
|
||||
|
||||
$this->assertDatabaseHas('users', [
|
||||
'id' => $this->user->id,
|
||||
'name' => 'Test User',
|
||||
'email' => 'test@test.app',
|
||||
]);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function update_password()
|
||||
{
|
||||
$this->actingAs($this->user)
|
||||
->patchJson('/api/settings/password', [
|
||||
'password' => 'updated',
|
||||
'password_confirmation' => 'updated',
|
||||
])
|
||||
->assertSuccessful();
|
||||
|
||||
$this->assertTrue(Hash::check('updated', $this->user->password));
|
||||
}
|
||||
}
|
||||
@@ -2,9 +2,11 @@
|
||||
|
||||
namespace Tests;
|
||||
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Illuminate\Foundation\Testing\TestCase as BaseTestCase;
|
||||
|
||||
abstract class TestCase extends BaseTestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
use CreatesApplication;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,18 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Unit;
|
||||
|
||||
use Tests\TestCase;
|
||||
|
||||
class ExampleTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* A basic test example.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testBasicTest()
|
||||
{
|
||||
$this->assertTrue(true);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user