22
33namespace Domains \Links \Tests \Feature ;
44
5+ use Domains \Accounts \Database \Factories \UserFactory ;
56use Domains \Tags \Database \Factories \TagFactory ;
67use Domains \Tags \Models \Tag ;
78use Faker \Factory ;
@@ -18,20 +19,17 @@ class LinksStoreTest extends TestCase
1819 private Tag $ tag ;
1920 private Generator $ faker ;
2021
22+ private array $ payload ;
23+ private array $ files ;
24+
2125 protected function setUp (): void
2226 {
2327 parent ::setUp ();
2428
2529 $ this ->tag = TagFactory::new ()->create ();
2630 $ this ->faker = Factory::create ();
27- }
28-
29- /** @test */
30- public function it_stores_resources (): void
31- {
32- Storage::fake ('local ' );
3331
34- $ payload = [
32+ $ this -> payload = [
3533 'link ' => $ this ->faker ->url ,
3634 'title ' => $ this ->faker ->title ,
3735 'description ' => $ this ->faker ->paragraph ,
@@ -41,21 +39,26 @@ public function it_stores_resources(): void
4139 ['id ' => $ this ->tag ->id ],
4240 ],
4341 ];
44-
45- $ files = [
42+ $ this ->files = [
4643 'cover_image ' => UploadedFile::fake ()->image ('cover_image.jpg ' ),
4744 ];
4845
49- $ response = $ this ->call ('POST ' , '/links ' , $ payload , [], $ files );
46+ Storage::fake ('local ' );
47+ }
48+
49+ /** @test */
50+ public function it_stores_resources (): void
51+ {
52+ $ response = $ this ->call ('POST ' , '/links ' , $ this ->payload , [], $ this ->files );
5053 self ::assertTrue ($ response ->isEmpty ());
5154
5255 $ this ->seeInDatabase ('links ' , [
53- 'link ' => $ payload ['link ' ],
54- 'title ' => $ payload ['title ' ],
55- 'description ' => $ payload ['description ' ],
56- 'author_name ' => $ payload ['author_name ' ],
57- 'author_email ' => $ payload ['author_email ' ],
58- 'cover_image ' => 'cover_images/ ' . $ files ['cover_image ' ]->hashName (),
56+ 'link ' => $ this -> payload ['link ' ],
57+ 'title ' => $ this -> payload ['title ' ],
58+ 'description ' => $ this -> payload ['description ' ],
59+ 'author_name ' => $ this -> payload ['author_name ' ],
60+ 'author_email ' => $ this -> payload ['author_email ' ],
61+ 'cover_image ' => 'cover_images/ ' . $ this -> files ['cover_image ' ]->hashName (),
5962 'approved_at ' => null ,
6063 ]);
6164
@@ -66,7 +69,7 @@ public function it_stores_resources(): void
6669 ]
6770 );
6871
69- Storage::assertExists ('cover_images/ ' . $ files ['cover_image ' ]->hashName ());
72+ Storage::assertExists ('cover_images/ ' . $ this -> files ['cover_image ' ]->hashName ());
7073 }
7174
7275 /** @test */
@@ -87,18 +90,9 @@ public function it_fails_to_store_resources_on_validation_errors(): void
8790 /** @test */
8891 public function it_fails_to_store_resources_with_invalid_link (): void
8992 {
90- $ payload = [
91- 'link ' => 'this_is_not_a_valid_url ' ,
92- 'title ' => $ this ->faker ->title ,
93- 'description ' => $ this ->faker ->paragraph ,
94- 'author_name ' => $ this ->faker ->name ,
95- 'author_email ' => $ this ->faker ->safeEmail ,
96- 'tags ' => [
97- ['id ' => $ this ->tag ->id ],
98- ],
99- ];
93+ $ this ->payload ['link ' ] = 'this_is_not_a_valid_url ' ;
10094
101- $ this ->post ('/links ' , $ payload )
95+ $ this ->post ('/links ' , $ this -> payload )
10296 ->seeJsonStructure ([
10397 'link ' ,
10498 ]);
@@ -107,26 +101,47 @@ public function it_fails_to_store_resources_with_invalid_link(): void
107101 /** @test */
108102 public function it_stores_resources_with_unregistered_link_domain (): void
109103 {
110- Storage:: fake ( ' local ' ) ;
104+ $ this -> payload [ ' link ' ] = ' http://unregistered.laravel.pt ' ;
111105
112- $ payload = [
113- 'link ' => 'http://unregistered.laravel.pt ' ,
114- 'title ' => $ this ->faker ->title ,
115- 'description ' => $ this ->faker ->paragraph ,
116- 'author_name ' => $ this ->faker ->name ,
117- 'author_email ' => $ this ->faker ->safeEmail ,
118- 'tags ' => [
119- ['id ' => $ this ->tag ->id ],
120- ],
121- ];
106+ $ response = $ this ->call ('POST ' , '/links ' , $ this ->payload , [], $ this ->files );
122107
123- $ files = [
124- 'cover_image ' => UploadedFile::fake ()->image ('cover_image.jpg ' ),
125- ];
108+ self ::assertEquals (204 , $ response ->getStatusCode ());
109+ self ::assertTrue ($ response ->isEmpty ());
110+ }
111+
112+ /** @test */
113+ public function it_forbids_guest_to_use_a_registered_users_email_when_submitting_a_link (): void
114+ {
115+ $ user = UserFactory::new (['email ' => $ this ->faker ->safeEmail ])
116+ ->create ();
126117
127- $ response = $ this ->call ('POST ' , '/links ' , $ payload , [], $ files );
118+ $ this ->payload ['author_email ' ] = $ user ->email ;
119+
120+ $ this ->post ('/links ' , $ this ->payload )
121+ ->seeJsonStructure (['author_email ' ]);
122+ }
123+
124+ /** @test */
125+ public function it_uses_logged_in_user_email_and_name_when_submitting_a_link (): void
126+ {
127+ // create a random user
128+ $ randomUser = UserFactory::new (['email ' => $ this ->faker ->safeEmail ])->create ();
129+ // create a user and login
130+ $ user = UserFactory::new (['email ' => $ this ->faker ->safeEmail ])->create ();
131+ $ this ->actingAs ($ user );
132+
133+ // use an existing user's email and it should go OK since we're logged in.
134+ $ this ->payload ['author_email ' ] = $ randomUser ->email ;
135+
136+ $ response = $ this ->call ('POST ' , '/links ' , $ this ->payload , [], $ this ->files );
128137
129138 self ::assertEquals (204 , $ response ->getStatusCode ());
130- self ::assertTrue ($ response ->isEmpty ());
139+ $ this ->seeInDatabase (
140+ 'links ' ,
141+ [
142+ 'author_email ' => $ user ->email ,
143+ 'author_name ' => $ user ->name ,
144+ ]
145+ );
131146 }
132147}
0 commit comments