From 55bea8411204b5ad777a45d934a67ef07313417b Mon Sep 17 00:00:00 2001 From: Kovah Date: Tue, 17 Nov 2020 12:22:41 +0100 Subject: [PATCH] Add tests for password resets and sharing helper --- .../views/auth/passwords/reset.blade.php | 2 - .../links/partials/share-link.blade.php | 2 +- tests/Controller/Auth/PasswordResetTest.php | 75 +++++++++++++++++++ tests/Helper/HelperFunctionsTest.php | 6 +- tests/Helper/SharingTest.php | 50 +++++++++++++ 5 files changed, 128 insertions(+), 7 deletions(-) create mode 100644 tests/Controller/Auth/PasswordResetTest.php create mode 100644 tests/Helper/SharingTest.php diff --git a/resources/views/auth/passwords/reset.blade.php b/resources/views/auth/passwords/reset.blade.php index 154d60b6..48f47b8f 100644 --- a/resources/views/auth/passwords/reset.blade.php +++ b/resources/views/auth/passwords/reset.blade.php @@ -15,8 +15,6 @@ aria-label="@lang('linkace.reset_password')"> @csrf - {{ implode('|',$errors->all()) }} -
diff --git a/resources/views/models/links/partials/share-link.blade.php b/resources/views/models/links/partials/share-link.blade.php index c81cdabc..5e08d960 100644 --- a/resources/views/models/links/partials/share-link.blade.php +++ b/resources/views/models/links/partials/share-link.blade.php @@ -1,3 +1,3 @@ - + diff --git a/tests/Controller/Auth/PasswordResetTest.php b/tests/Controller/Auth/PasswordResetTest.php new file mode 100644 index 00000000..faa2cdd3 --- /dev/null +++ b/tests/Controller/Auth/PasswordResetTest.php @@ -0,0 +1,75 @@ +get('forgot-password'); + $confirmView->assertSee('forgot-password'); + } + + public function testForgotPasswordRequest(): void + { + Notification::fake(); + + $user = User::factory()->create([ + 'email' => 'reset@linkace.org', + ]); + + $response = $this->post('forgot-password', [ + 'email' => 'reset@linkace.org', + ]); + + $response->assertSessionHas('status'); + + Notification::assertSentTo($user, ResetPassword::class); + } + + public function testPasswordResetView(): void + { + $user = User::factory()->create([ + 'email' => 'reset@linkace.org', + ]); + + $token = app('auth.password.broker')->createToken($user); + + $confirmView = $this->get('reset-password/' . $token); + $confirmView->assertSee('reset-password'); + } + + public function testPasswordResetRequest(): void + { + $user = User::factory()->create([ + 'email' => 'reset@linkace.org', + ]); + + $token = app('auth.password.broker')->createToken($user); + + $confirmView = $this->post('reset-password/', [ + 'token' => $token, + 'email' => 'reset@linkace.org', + 'password' => 'newPassword', + 'password_confirmation' => 'newPassword', + ]); + + $confirmView->assertRedirect('login'); + + $loginAttempt = Auth::attempt([ + 'email' => 'reset@linkace.org', + 'password' => 'newPassword', + ]); + + self::assertTrue($loginAttempt); + } +} diff --git a/tests/Helper/HelperFunctionsTest.php b/tests/Helper/HelperFunctionsTest.php index 70231f7b..26ebe14e 100644 --- a/tests/Helper/HelperFunctionsTest.php +++ b/tests/Helper/HelperFunctionsTest.php @@ -4,15 +4,13 @@ namespace Tests\Helper; use App\Models\Link; use App\Models\User; -use Illuminate\Foundation\Testing\DatabaseMigrations; -use Illuminate\Foundation\Testing\DatabaseTransactions; +use Illuminate\Foundation\Testing\RefreshDatabase; use Illuminate\Support\Facades\Storage; use Tests\TestCase; class HelperFunctionsTest extends TestCase { - use DatabaseMigrations; - use DatabaseTransactions; + use RefreshDatabase; /** @var User */ private $user; diff --git a/tests/Helper/SharingTest.php b/tests/Helper/SharingTest.php new file mode 100644 index 00000000..5a12a656 --- /dev/null +++ b/tests/Helper/SharingTest.php @@ -0,0 +1,50 @@ + 'mailto:?subject=Example%20Website&body=I%20found%20this%20awesome%20link%2C%20go%20check%20it%20out%3A%20https%3A%2F%2Fexample.com%2F"', + 'facebook' => 'https://www.facebook.com/sharer/sharer.php?u=https://example.com/', + 'twitter' => 'https://twitter.com/intent/tweet?text=I found this awesome link, go check it out: https://example.com/', + 'reddit' => 'http://www.reddit.com/submit?url=https://example.com/&title=Example Website', + 'pinterest' => 'http://pinterest.com/pin/create/button/?url=https://example.com/&description=Example Website', + 'whatsapp' => 'whatsapp://send?text=I found this awesome link, go check it out: https://example.com/', + 'telegram' => 'tg://msg?text==I found this awesome link, go check it out: https://example.com/', + 'wechat' => 'https://www.addtoany.com/ext/wechat/share/#url=https://example.com/', + 'sms' => 'sms:?&body=I found this awesome link, go check it out: https://example.com/', + 'skype' => 'https://web.skype.com/share?url=https%3A%2F%2Fexample.com%2F', + 'hackernews' => 'https://news.ycombinator.com/submitlink?u=https://example.com/&t=Example Website', + 'mastodon' => 'web+mastodon://share?text=I%20found%20this%20awesome%20link%2C%20go%20check%20it%20out%3A%20https%3A%2F%2Fexample.com%2F', + 'pocket' => 'https://getpocket.com/save?url=https://example.com/', + 'flipboard' => 'https://share.flipboard.com/bookmarklet/popout?v=Example Website&url=https%3A%2F%2Fexample.com%2F', + 'evernote' => 'https://www.evernote.com/clip.action?url=https%3A%2F%2Fexample.com%2F&title=Example Website', + 'trello' => 'https://trello.com/add-card?mode=popup&url=https%3A%2F%2Fexample.com%2F&name=Example%20Website&desc=', + 'buffer' => 'https://buffer.com/add?url=https%3A%2F%2Fexample.com%2F&text=Example%20Website', + 'tumblr' => 'http://tumblr.com/share/link?url=https://example.com/&name=Example Website', + 'xing' => 'https://www.xing.com/spi/shares/new?url=https://example.com/', + 'linkedin' => 'https://www.linkedin.com/shareArticle?mini=true&url=https%3A%2F%2Fexample.com%2F&title=Example%20Website&summary=&source=AddToAny', + ]; + + public function testCorrectShareLinkForServices(): void + { + $testLink = Link::factory()->create([ + 'url' => 'https://example.com/', + 'title' => 'Example Website', + ]); + + foreach (self::$shareData as $service => $expectedLink) { + $shareLink = Sharing::getShareLink($service, $testLink); + + self::assertStringContainsString($expectedLink, $shareLink); + } + } +}