This repository was archived by the owner on Aug 22, 2025. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMagicLinkFormTest.php
More file actions
125 lines (98 loc) · 3.59 KB
/
MagicLinkFormTest.php
File metadata and controls
125 lines (98 loc) · 3.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
<?php
declare(strict_types=1);
namespace Codedge\MagicLink\Tests\MagicLink;
use Codedge\MagicLink\Events\LinkCreated;
use Codedge\MagicLink\Http\Middleware\MagicLink;
use Codedge\MagicLink\Mail\MagicLink\LinkInformation;
use Codedge\MagicLink\Tests\TestCase;
use Illuminate\Support\Facades\Mail;
use Statamic\Facades\User;
class MagicLinkFormTest extends TestCase
{
/** @test */
public function can_show_magic_link_form(): void
{
$this->get(route('magiclink.show-send-link-form'))
->assertOk()
->assertSee(__('magiclink::web.back_to_classic_login'));
}
/** @test */
public function cannot_get_link_for_invalid_address(): void
{
$payload = [
'email' => 'wrong-email-format',
];
$this->post(route('magiclink.send-link'), $payload)
->assertSessionHasErrors(['email']);
$payload = [
'email' => '',
];
$this->post(route('magiclink.send-link'), $payload)
->assertSessionHasErrors(['email']);
}
/** @test */
public function can_request_link_for_non_existing_but_allowed_domain_user(): void
{
$this->expectsEvents([LinkCreated::class]);
$this->signInAdmin();
$payload = [
'enabled' => true,
'expireTime' => 999,
'allowedAddresses' => [],
'allowedDomains' => ['larifari.test'],
];
$this->patch(cp_route('magiclink.update'), $payload)->assertOk();
$this->app->make('auth')->guard(null)->logout();
$this->assertGuest();
$payload = [
'email' => 'test@larifari.test',
];
$this->withSession([MagicLink::MAGIC_LINK_REDIRECT_TO => cp_route('dashboard')])
->post(route('magiclink.send-link'), $payload)
->assertOk()
->assertSessionHas('success', __('magiclink::web.address_exists_then_email'));
}
/** @test */
public function can_request_link_for_non_existing_and_non_allowed_user(): void
{
$this->doesntExpectEvents([LinkCreated::class]);
$payload = [
'email' => 'i@donotexist.com',
];
$this->post(route('magiclink.send-link'), $payload)
->assertOk()
->assertSessionHas('success', __('magiclink::web.address_exists_then_email'));
}
/** @test */
public function can_request_link_for_existing_user(): void
{
$this->expectsEvents([LinkCreated::class]);
$user = User::make();
$user->id(99)->email('test@test.com');
$payload = [
'email' => 'test@test.com',
];
$this->withSession([MagicLink::MAGIC_LINK_REDIRECT_TO => cp_route('dashboard')])
->post(route('magiclink.send-link'), $payload)
->assertOk()
->assertSessionHas('success', __('magiclink::web.address_exists_then_email'));
}
/** @test */
public function can_sent_email_with_requested_link(): void
{
$user = User::make();
$user->id(99)->email('test@test.com');
$payload = [
'email' => 'test@test.com',
];
Mail::fake();
Mail::assertNothingSent();
$this->withSession([MagicLink::MAGIC_LINK_REDIRECT_TO => cp_route('dashboard')])
->post(route('magiclink.send-link'), $payload)
->assertOk()
->assertSessionHas('success', __('magiclink::web.address_exists_then_email'));
Mail::assertQueued(LinkInformation::class, function (LinkInformation $mail) use ($user) {
return $mail->hasTo($user->email());
});
}
}