ReceiveEmailTest.php 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088
  1. <?php
  2. namespace Tests\Feature;
  3. use App\AdditionalUsername;
  4. use App\Alias;
  5. use App\AliasRecipient;
  6. use App\Domain;
  7. use App\Mail\ForwardEmail;
  8. use App\Notifications\NearBandwidthLimit;
  9. use App\Recipient;
  10. use App\User;
  11. use Illuminate\Foundation\Testing\RefreshDatabase;
  12. use Illuminate\Support\Facades\Cache;
  13. use Illuminate\Support\Facades\Mail;
  14. use Illuminate\Support\Facades\Notification;
  15. use Tests\TestCase;
  16. class ReceiveEmailTest extends TestCase
  17. {
  18. use RefreshDatabase;
  19. protected $user;
  20. protected function setUp(): void
  21. {
  22. parent::setUp();
  23. $this->user = factory(User::class)->create(['username' => 'johndoe']);
  24. $this->user->recipients()->save($this->user->defaultRecipient);
  25. }
  26. /** @test */
  27. public function it_can_forward_email_from_file()
  28. {
  29. Mail::fake();
  30. Notification::fake();
  31. Mail::assertNothingSent();
  32. $this->artisan(
  33. 'anonaddy:receive-email',
  34. [
  35. 'file' => base_path('tests/emails/email.eml'),
  36. '--sender' => 'will@anonaddy.com',
  37. '--recipient' => ['ebay@johndoe.anonaddy.com'],
  38. '--local_part' => ['ebay'],
  39. '--extension' => [''],
  40. '--domain' => ['johndoe.anonaddy.com'],
  41. '--size' => '1000'
  42. ]
  43. )->assertExitCode(0);
  44. $this->assertDatabaseHas('aliases', [
  45. 'email' => 'ebay@johndoe.'.config('anonaddy.domain'),
  46. 'local_part' => 'ebay',
  47. 'domain' => 'johndoe.'.config('anonaddy.domain'),
  48. 'emails_forwarded' => 1,
  49. 'emails_blocked' => 0
  50. ]);
  51. $this->assertEquals(1, $this->user->aliases()->count());
  52. $this->assertDatabaseHas('users', [
  53. 'id' => $this->user->id,
  54. 'username' => 'johndoe',
  55. 'bandwidth' => '1000'
  56. ]);
  57. Mail::assertQueued(ForwardEmail::class, function ($mail) {
  58. return $mail->hasTo($this->user->email);
  59. });
  60. Notification::assertNothingSent();
  61. }
  62. /** @test */
  63. public function it_can_forward_email_from_file_with_capitals()
  64. {
  65. Mail::fake();
  66. Mail::assertNothingSent();
  67. $this->artisan(
  68. 'anonaddy:receive-email',
  69. [
  70. 'file' => base_path('tests/emails/email_caps.eml'),
  71. '--sender' => 'will@anonaddy.com',
  72. '--recipient' => ['EBAY@JOHNDOE.ANONADDY.COM'],
  73. '--local_part' => ['EBAY'],
  74. '--extension' => [''],
  75. '--domain' => ['JOHNDOE.ANONADDY.COM'],
  76. '--size' => '1000'
  77. ]
  78. )->assertExitCode(0);
  79. $this->assertDatabaseHas('aliases', [
  80. 'email' => 'ebay@johndoe.'.config('anonaddy.domain'),
  81. 'local_part' => 'ebay',
  82. 'domain' => 'johndoe.'.config('anonaddy.domain'),
  83. 'emails_forwarded' => 1,
  84. 'emails_blocked' => 0
  85. ]);
  86. $this->assertEquals(1, $this->user->aliases()->count());
  87. $this->assertDatabaseHas('users', [
  88. 'id' => $this->user->id,
  89. 'username' => 'johndoe',
  90. 'bandwidth' => '1000'
  91. ]);
  92. Mail::assertQueued(ForwardEmail::class, function ($mail) {
  93. return $mail->hasTo($this->user->email);
  94. });
  95. }
  96. /** @test */
  97. public function it_can_forward_email_from_file_with_attachment()
  98. {
  99. Mail::fake();
  100. Mail::assertNothingSent();
  101. $this->artisan(
  102. 'anonaddy:receive-email',
  103. [
  104. 'file' => base_path('tests/emails/email_with_attachment.eml'),
  105. '--sender' => 'will@anonaddy.com',
  106. '--recipient' => ['attachment@johndoe.anonaddy.com'],
  107. '--local_part' => ['attachment'],
  108. '--extension' => [''],
  109. '--domain' => ['johndoe.anonaddy.com'],
  110. '--size' => '1000'
  111. ]
  112. )->assertExitCode(0);
  113. $this->assertDatabaseHas('aliases', [
  114. 'email' => 'attachment@johndoe.'.config('anonaddy.domain'),
  115. 'local_part' => 'attachment',
  116. 'domain' => 'johndoe.'.config('anonaddy.domain'),
  117. 'emails_forwarded' => 1,
  118. 'emails_blocked' => 0
  119. ]);
  120. $this->assertEquals(1, $this->user->aliases()->count());
  121. $this->assertDatabaseHas('users', [
  122. 'id' => $this->user->id,
  123. 'username' => 'johndoe',
  124. 'bandwidth' => '1000'
  125. ]);
  126. Mail::assertQueued(ForwardEmail::class, function ($mail) {
  127. return $mail->hasTo($this->user->email);
  128. });
  129. }
  130. /** @test */
  131. public function it_can_forward_email_from_file_to_multiple_recipients()
  132. {
  133. Mail::fake();
  134. Mail::assertNothingSent();
  135. $this->artisan(
  136. 'anonaddy:receive-email',
  137. [
  138. 'file' => base_path('tests/emails/email_multiple_recipients.eml'),
  139. '--sender' => 'will@anonaddy.com',
  140. '--recipient' => ['ebay@johndoe.anonaddy.com', 'amazon@johndoe.anonaddy.com', 'paypal@johndoe.anonaddy.me'],
  141. '--local_part' => ['ebay', 'amazon', 'paypal'],
  142. '--extension' => ['', '', ''],
  143. '--domain' => ['johndoe.anonaddy.com', 'johndoe.anonaddy.com', 'johndoe.anonaddy.com'],
  144. '--size' => '1217'
  145. ]
  146. )->assertExitCode(0);
  147. $this->assertDatabaseHas('aliases', [
  148. 'email' => 'ebay@johndoe.'.config('anonaddy.domain'),
  149. 'local_part' => 'ebay',
  150. 'domain' => 'johndoe.'.config('anonaddy.domain'),
  151. 'emails_forwarded' => 1,
  152. 'emails_blocked' => 0
  153. ]);
  154. $this->assertDatabaseHas('aliases', [
  155. 'email' => 'amazon@johndoe.'.config('anonaddy.domain'),
  156. 'local_part' => 'amazon',
  157. 'domain' => 'johndoe.'.config('anonaddy.domain'),
  158. 'emails_forwarded' => 1,
  159. 'emails_blocked' => 0
  160. ]);
  161. $this->assertDatabaseHas('aliases', [
  162. 'email' => 'paypal@johndoe.'.config('anonaddy.domain'),
  163. 'local_part' => 'paypal',
  164. 'domain' => 'johndoe.'.config('anonaddy.domain'),
  165. 'emails_forwarded' => 1,
  166. 'emails_blocked' => 0
  167. ]);
  168. $this->assertEquals(3, $this->user->aliases()->count());
  169. $this->assertDatabaseHas('users', [
  170. 'id' => $this->user->id,
  171. 'username' => 'johndoe',
  172. 'bandwidth' => '1217'
  173. ]);
  174. Mail::assertQueued(ForwardEmail::class, function ($mail) {
  175. return $mail->hasTo($this->user->email);
  176. });
  177. }
  178. /** @test */
  179. public function it_can_forward_email_from_file_with_extension()
  180. {
  181. Mail::fake();
  182. Mail::assertNothingSent();
  183. $this->artisan(
  184. 'anonaddy:receive-email',
  185. [
  186. 'file' => base_path('tests/emails/email_with_extension.eml'),
  187. '--sender' => 'will@anonaddy.com',
  188. '--recipient' => ['ebay+a@johndoe.anonaddy.com'],
  189. '--local_part' => ['ebay'],
  190. '--extension' => ['2.3'],
  191. '--domain' => ['johndoe.anonaddy.com'],
  192. '--size' => '789'
  193. ]
  194. )->assertExitCode(0);
  195. $this->assertDatabaseHas('aliases', [
  196. 'email' => 'ebay@johndoe.'.config('anonaddy.domain'),
  197. 'local_part' => 'ebay',
  198. 'domain' => 'johndoe.'.config('anonaddy.domain'),
  199. 'emails_forwarded' => 1,
  200. 'emails_blocked' => 0
  201. ]);
  202. $this->assertDatabaseHas('users', [
  203. 'id' => $this->user->id,
  204. 'username' => 'johndoe',
  205. 'bandwidth' => '789'
  206. ]);
  207. $this->assertEquals(1, $this->user->aliases()->count());
  208. Mail::assertQueued(ForwardEmail::class, function ($mail) {
  209. return $mail->hasTo($this->user->email);
  210. });
  211. }
  212. /** @test */
  213. public function it_can_forward_email_with_existing_alias()
  214. {
  215. Mail::fake();
  216. Mail::assertNothingSent();
  217. factory(Alias::class)->create([
  218. 'user_id' => $this->user->id,
  219. 'email' => 'ebay@johndoe.'.config('anonaddy.domain'),
  220. 'local_part' => 'ebay',
  221. 'domain' => 'johndoe.'.config('anonaddy.domain'),
  222. ]);
  223. $defaultRecipient = $this->user->defaultRecipient;
  224. $this->artisan(
  225. 'anonaddy:receive-email',
  226. [
  227. 'file' => base_path('tests/emails/email.eml'),
  228. '--sender' => 'will@anonaddy.com',
  229. '--recipient' => ['ebay@johndoe.anonaddy.com'],
  230. '--local_part' => ['ebay'],
  231. '--extension' => [''],
  232. '--domain' => ['johndoe.anonaddy.com'],
  233. '--size' => '559'
  234. ]
  235. )->assertExitCode(0);
  236. $this->assertDatabaseHas('aliases', [
  237. 'email' => 'ebay@johndoe.'.config('anonaddy.domain'),
  238. 'emails_forwarded' => 1,
  239. 'emails_blocked' => 0
  240. ]);
  241. $this->assertDatabaseHas('users', [
  242. 'id' => $this->user->id,
  243. 'username' => 'johndoe',
  244. 'bandwidth' => '559'
  245. ]);
  246. $this->assertCount(1, $this->user->aliases);
  247. Mail::assertQueued(ForwardEmail::class, function ($mail) use ($defaultRecipient) {
  248. return $mail->hasTo($defaultRecipient->email);
  249. });
  250. }
  251. /** @test */
  252. public function it_can_forward_email_with_uuid_generated_alias()
  253. {
  254. Mail::fake();
  255. Mail::assertNothingSent();
  256. $uuid = '86064c92-da41-443e-a2bf-5a7b0247842f';
  257. config([
  258. 'anonaddy.admin_username' => 'random'
  259. ]);
  260. factory(Alias::class)->create([
  261. 'id' => $uuid,
  262. 'user_id' => $this->user->id,
  263. 'email' => $uuid.'@anonaddy.me',
  264. 'local_part' => $uuid,
  265. 'domain' => 'anonaddy.me',
  266. ]);
  267. $defaultRecipient = $this->user->defaultRecipient;
  268. $this->artisan(
  269. 'anonaddy:receive-email',
  270. [
  271. 'file' => base_path('tests/emails/email_with_uuid.eml'),
  272. '--sender' => 'will@anonaddy.com',
  273. '--recipient' => [$uuid.'@anonaddy.me'],
  274. '--local_part' => [$uuid],
  275. '--extension' => [''],
  276. '--domain' => ['anonaddy.me'],
  277. '--size' => '892'
  278. ]
  279. )->assertExitCode(0);
  280. $this->assertDatabaseHas('aliases', [
  281. 'local_part' => $uuid,
  282. 'domain' => 'anonaddy.me',
  283. 'email' => $uuid.'@anonaddy.me',
  284. 'emails_forwarded' => 1,
  285. 'emails_blocked' => 0
  286. ]);
  287. $this->assertDatabaseHas('users', [
  288. 'id' => $this->user->id,
  289. 'username' => 'johndoe',
  290. 'bandwidth' => '892'
  291. ]);
  292. $this->assertCount(1, $this->user->aliases);
  293. Mail::assertQueued(ForwardEmail::class, function ($mail) use ($defaultRecipient) {
  294. return $mail->hasTo($defaultRecipient->email);
  295. });
  296. }
  297. /** @test */
  298. public function it_can_forward_email_with_existing_alias_and_receipients()
  299. {
  300. Mail::fake();
  301. Mail::assertNothingSent();
  302. $alias = factory(Alias::class)->create([
  303. 'user_id' => $this->user->id,
  304. 'email' => 'ebay@johndoe.'.config('anonaddy.domain'),
  305. 'local_part' => 'ebay',
  306. 'domain' => 'johndoe.'.config('anonaddy.domain'),
  307. ]);
  308. $recipient = factory(Recipient::class)->create([
  309. 'user_id' => $this->user->id,
  310. 'email' => 'one@example.com'
  311. ]);
  312. $recipient2 = factory(Recipient::class)->create([
  313. 'user_id' => $this->user->id,
  314. 'email' => 'two@example.com'
  315. ]);
  316. AliasRecipient::create([
  317. 'alias' => $alias,
  318. 'recipient' => $recipient
  319. ]);
  320. AliasRecipient::create([
  321. 'alias' => $alias,
  322. 'recipient' => $recipient2
  323. ]);
  324. $this->artisan(
  325. 'anonaddy:receive-email',
  326. [
  327. 'file' => base_path('tests/emails/email.eml'),
  328. '--sender' => 'will@anonaddy.com',
  329. '--recipient' => ['ebay@johndoe.anonaddy.com'],
  330. '--local_part' => ['ebay'],
  331. '--extension' => [''],
  332. '--domain' => ['johndoe.anonaddy.com'],
  333. '--size' => '444'
  334. ]
  335. )->assertExitCode(0);
  336. $this->assertDatabaseHas('aliases', [
  337. 'email' => 'ebay@johndoe.'.config('anonaddy.domain'),
  338. 'emails_forwarded' => 1,
  339. 'emails_blocked' => 0
  340. ]);
  341. $this->assertDatabaseHas('users', [
  342. 'id' => $this->user->id,
  343. 'username' => 'johndoe',
  344. 'bandwidth' => '444'
  345. ]);
  346. Mail::assertQueued(ForwardEmail::class, function ($mail) {
  347. return $mail->hasTo('one@example.com');
  348. });
  349. Mail::assertQueued(ForwardEmail::class, function ($mail) {
  350. return $mail->hasTo('two@example.com');
  351. });
  352. }
  353. /** @test */
  354. public function it_can_attach_recipients_to_new_alias_with_extension()
  355. {
  356. Mail::fake();
  357. Mail::assertNothingSent();
  358. $recipient = factory(Recipient::class)->create([
  359. 'user_id' => $this->user->id,
  360. 'email' => 'one@example.com'
  361. ]);
  362. $recipient2 = factory(Recipient::class)->create([
  363. 'user_id' => $this->user->id,
  364. 'email' => 'two@example.com'
  365. ]);
  366. $this->artisan(
  367. 'anonaddy:receive-email',
  368. [
  369. 'file' => base_path('tests/emails/email_with_extension.eml'),
  370. '--sender' => 'will@anonaddy.com',
  371. '--recipient' => ['ebay@johndoe.anonaddy.com'],
  372. '--local_part' => ['ebay'],
  373. '--extension' => ['2.3'],
  374. '--domain' => ['johndoe.anonaddy.com'],
  375. '--size' => '444'
  376. ]
  377. )->assertExitCode(0);
  378. $this->assertDatabaseHas('aliases', [
  379. 'extension' => '2.3',
  380. 'email' => 'ebay@johndoe.'.config('anonaddy.domain'),
  381. 'emails_forwarded' => 1,
  382. 'emails_blocked' => 0
  383. ]);
  384. $this->assertDatabaseHas('users', [
  385. 'id' => $this->user->id,
  386. 'username' => 'johndoe',
  387. 'bandwidth' => '444'
  388. ]);
  389. Mail::assertQueued(ForwardEmail::class, function ($mail) use ($recipient) {
  390. return $mail->hasTo($recipient->email);
  391. });
  392. Mail::assertQueued(ForwardEmail::class, function ($mail) use ($recipient2) {
  393. return $mail->hasTo($recipient2->email);
  394. });
  395. }
  396. /** @test */
  397. public function it_can_not_attach_unverified_recipient_to_new_alias_with_extension()
  398. {
  399. Mail::fake();
  400. Mail::assertNothingSent();
  401. $defaultRecipient = $this->user->defaultRecipient;
  402. factory(Recipient::class)->create([
  403. 'user_id' => $this->user->id,
  404. 'email' => 'one@example.com',
  405. 'email_verified_at' => null
  406. ]);
  407. $verifiedRecipient = factory(Recipient::class)->create([
  408. 'user_id' => $this->user->id,
  409. 'email' => 'two@example.com'
  410. ]);
  411. $this->artisan(
  412. 'anonaddy:receive-email',
  413. [
  414. 'file' => base_path('tests/emails/email_with_extension.eml'),
  415. '--sender' => 'will@anonaddy.com',
  416. '--recipient' => ['ebay@johndoe.anonaddy.com'],
  417. '--local_part' => ['ebay'],
  418. '--extension' => ['2.3'],
  419. '--domain' => ['johndoe.anonaddy.com'],
  420. '--size' => '444'
  421. ]
  422. )->assertExitCode(0);
  423. $this->assertDatabaseHas('aliases', [
  424. 'email' => 'ebay@johndoe.'.config('anonaddy.domain'),
  425. 'emails_forwarded' => 1,
  426. 'emails_blocked' => 0
  427. ]);
  428. $this->assertDatabaseHas('users', [
  429. 'id' => $this->user->id,
  430. 'username' => 'johndoe',
  431. 'bandwidth' => '444'
  432. ]);
  433. $alias = $this->user->aliases()->where('email', 'ebay@johndoe.'.config('anonaddy.domain'))->first();
  434. $this->assertCount(1, $alias->recipients);
  435. Mail::assertQueued(ForwardEmail::class, function ($mail) use ($defaultRecipient, $verifiedRecipient) {
  436. return $mail->hasTo($verifiedRecipient->email);
  437. });
  438. }
  439. /** @test */
  440. public function it_does_not_send_email_if_default_recipient_has_not_yet_been_verified()
  441. {
  442. Mail::fake();
  443. Mail::assertNothingSent();
  444. $this->user->defaultRecipient->update(['email_verified_at' => null]);
  445. $this->assertNull($this->user->defaultRecipient->email_verified_at);
  446. $this->artisan(
  447. 'anonaddy:receive-email',
  448. [
  449. 'file' => base_path('tests/emails/email.eml'),
  450. '--sender' => 'will@anonaddy.com',
  451. '--recipient' => ['ebay@johndoe.anonaddy.com'],
  452. '--local_part' => ['ebay'],
  453. '--extension' => [''],
  454. '--domain' => ['johndoe.anonaddy.com'],
  455. '--size' => '1000'
  456. ]
  457. )->assertExitCode(0);
  458. $this->assertDatabaseMissing('aliases', [
  459. 'email' => 'ebay@johndoe.'.config('anonaddy.domain'),
  460. 'emails_blocked' => 0
  461. ]);
  462. $this->assertDatabaseHas('users', [
  463. 'id' => $this->user->id,
  464. 'username' => 'johndoe',
  465. 'bandwidth' => '0'
  466. ]);
  467. Mail::assertNotSent(ForwardEmail::class);
  468. }
  469. /** @test */
  470. public function it_can_unsubscribe_alias_by_emailing_list_unsubscribe()
  471. {
  472. Mail::fake();
  473. Mail::assertNothingSent();
  474. factory(Alias::class)->create([
  475. 'id' => '8f36380f-df4e-4875-bb12-9c4448573712',
  476. 'user_id' => $this->user->id,
  477. 'email' => 'ebay@johndoe.'.config('anonaddy.domain'),
  478. 'local_part' => 'ebay',
  479. 'domain' => 'johndoe.'.config('anonaddy.domain')
  480. ]);
  481. factory(Recipient::class)->create([
  482. 'user_id' => $this->user->id,
  483. 'email' => 'will@anonaddy.com'
  484. ]);
  485. $this->assertDatabaseHas('aliases', [
  486. 'id' => '8f36380f-df4e-4875-bb12-9c4448573712',
  487. 'user_id' => $this->user->id,
  488. 'email' => 'ebay@johndoe.'.config('anonaddy.domain'),
  489. 'active' => true
  490. ]);
  491. $this->artisan(
  492. 'anonaddy:receive-email',
  493. [
  494. 'file' => base_path('tests/emails/email_unsubscribe.eml'),
  495. '--sender' => 'will@anonaddy.com',
  496. '--recipient' => ['8f36380f-df4e-4875-bb12-9c4448573712@unsubscribe.anonaddy.com'],
  497. '--local_part' => ['8f36380f-df4e-4875-bb12-9c4448573712'],
  498. '--extension' => [''],
  499. '--domain' => ['unsubscribe.anonaddy.com'],
  500. '--size' => '1000'
  501. ]
  502. )->assertExitCode(0);
  503. $this->assertDatabaseHas('aliases', [
  504. 'id' => '8f36380f-df4e-4875-bb12-9c4448573712',
  505. 'user_id' => $this->user->id,
  506. 'email' => 'ebay@johndoe.'.config('anonaddy.domain'),
  507. 'local_part' => 'ebay',
  508. 'domain' => 'johndoe.'.config('anonaddy.domain'),
  509. 'active' => false
  510. ]);
  511. $this->assertDatabaseHas('users', [
  512. 'id' => $this->user->id,
  513. 'username' => 'johndoe',
  514. 'bandwidth' => '0'
  515. ]);
  516. Mail::assertNotSent(ForwardEmail::class);
  517. }
  518. /** @test */
  519. public function it_does_not_count_unsubscribe_recipient_when_calculating_size()
  520. {
  521. Mail::fake();
  522. Mail::assertNothingSent();
  523. factory(Alias::class)->create([
  524. 'id' => '8f36380f-df4e-4875-bb12-9c4448573712',
  525. 'user_id' => $this->user->id,
  526. 'email' => 'ebay@johndoe.'.config('anonaddy.domain'),
  527. 'local_part' => 'ebay',
  528. 'domain' => 'johndoe.'.config('anonaddy.domain')
  529. ]);
  530. factory(Recipient::class)->create([
  531. 'user_id' => $this->user->id,
  532. 'email' => 'will@anonaddy.com'
  533. ]);
  534. $this->assertDatabaseHas('aliases', [
  535. 'id' => '8f36380f-df4e-4875-bb12-9c4448573712',
  536. 'user_id' => $this->user->id,
  537. 'email' => 'ebay@johndoe.'.config('anonaddy.domain'),
  538. 'active' => true
  539. ]);
  540. $this->artisan(
  541. 'anonaddy:receive-email',
  542. [
  543. 'file' => base_path('tests/emails/email_unsubscribe_plus_other_recipient.eml'),
  544. '--sender' => 'will@anonaddy.com',
  545. '--recipient' => ['8f36380f-df4e-4875-bb12-9c4448573712@unsubscribe.anonaddy.com', 'another@johndoe.anonaddy.com'],
  546. '--local_part' => ['8f36380f-df4e-4875-bb12-9c4448573712', 'another'],
  547. '--extension' => ['', ''],
  548. '--domain' => ['unsubscribe.anonaddy.com', 'johndoe.anonaddy.com'],
  549. '--size' => '1000'
  550. ]
  551. )->assertExitCode(0);
  552. $this->assertDatabaseHas('aliases', [
  553. 'id' => '8f36380f-df4e-4875-bb12-9c4448573712',
  554. 'user_id' => $this->user->id,
  555. 'email' => 'ebay@johndoe.'.config('anonaddy.domain'),
  556. 'local_part' => 'ebay',
  557. 'domain' => 'johndoe.'.config('anonaddy.domain'),
  558. 'active' => false
  559. ]);
  560. $this->assertDatabaseHas('aliases', [
  561. 'user_id' => $this->user->id,
  562. 'email' => 'another@johndoe.'.config('anonaddy.domain'),
  563. 'local_part' => 'another',
  564. 'domain' => 'johndoe.'.config('anonaddy.domain'),
  565. 'active' => true
  566. ]);
  567. $this->assertDatabaseHas('users', [
  568. 'id' => $this->user->id,
  569. 'username' => 'johndoe',
  570. 'bandwidth' => '1000'
  571. ]);
  572. Mail::assertNotSent(ForwardEmail::class);
  573. }
  574. /** @test */
  575. public function it_cannot_unsubscribe_alias_if_not_a_verified_user_recipient()
  576. {
  577. Mail::fake();
  578. Mail::assertNothingSent();
  579. factory(Alias::class)->create([
  580. 'id' => '8f36380f-df4e-4875-bb12-9c4448573712',
  581. 'user_id' => $this->user->id,
  582. 'email' => 'ebay@johndoe.'.config('anonaddy.domain'),
  583. 'local_part' => 'ebay',
  584. 'domain' => 'johndoe.'.config('anonaddy.domain')
  585. ]);
  586. $this->assertDatabaseHas('aliases', [
  587. 'id' => '8f36380f-df4e-4875-bb12-9c4448573712',
  588. 'user_id' => $this->user->id,
  589. 'email' => 'ebay@johndoe.'.config('anonaddy.domain'),
  590. 'active' => true
  591. ]);
  592. $this->artisan(
  593. 'anonaddy:receive-email',
  594. [
  595. 'file' => base_path('tests/emails/email_unsubscribe.eml'),
  596. '--sender' => 'will@anonaddy.com',
  597. '--recipient' => ['8f36380f-df4e-4875-bb12-9c4448573712@unsubscribe.anonaddy.com'],
  598. '--local_part' => ['8f36380f-df4e-4875-bb12-9c4448573712'],
  599. '--extension' => [''],
  600. '--domain' => ['unsubscribe.anonaddy.com'],
  601. '--size' => '1000'
  602. ]
  603. )->assertExitCode(0);
  604. $this->assertDatabaseHas('aliases', [
  605. 'id' => '8f36380f-df4e-4875-bb12-9c4448573712',
  606. 'user_id' => $this->user->id,
  607. 'email' => 'ebay@johndoe.'.config('anonaddy.domain'),
  608. 'local_part' => 'ebay',
  609. 'domain' => 'johndoe.'.config('anonaddy.domain'),
  610. 'active' => true
  611. ]);
  612. $this->assertDatabaseHas('users', [
  613. 'id' => $this->user->id,
  614. 'username' => 'johndoe',
  615. 'bandwidth' => '0'
  616. ]);
  617. Mail::assertNotSent(ForwardEmail::class);
  618. }
  619. /** @test */
  620. public function it_can_forward_email_to_admin_username_for_root_domain()
  621. {
  622. Mail::fake();
  623. Mail::assertNothingSent();
  624. config(['anonaddy.admin_username' => 'johndoe']);
  625. $this->artisan(
  626. 'anonaddy:receive-email',
  627. [
  628. 'file' => base_path('tests/emails/email_admin.eml'),
  629. '--sender' => 'will@anonaddy.com',
  630. '--recipient' => ['ebay@anonaddy.me'],
  631. '--local_part' => ['ebay'],
  632. '--extension' => [''],
  633. '--domain' => ['anonaddy.me'],
  634. '--size' => '1346'
  635. ]
  636. )->assertExitCode(0);
  637. $this->assertDatabaseHas('aliases', [
  638. 'email' => 'ebay@anonaddy.me',
  639. 'local_part' => 'ebay',
  640. 'domain' => 'anonaddy.me',
  641. 'emails_forwarded' => 1,
  642. 'emails_blocked' => 0
  643. ]);
  644. $this->assertDatabaseHas('users', [
  645. 'id' => $this->user->id,
  646. 'username' => 'johndoe',
  647. 'bandwidth' => '1346'
  648. ]);
  649. $this->assertEquals(1, $this->user->aliases()->count());
  650. Mail::assertQueued(ForwardEmail::class, function ($mail) {
  651. return $mail->hasTo($this->user->email);
  652. });
  653. }
  654. /** @test */
  655. public function it_can_forward_email_for_custom_domain()
  656. {
  657. Mail::fake();
  658. Mail::assertNothingSent();
  659. $domain = factory(Domain::class)->create([
  660. 'user_id' => $this->user->id,
  661. 'domain' => 'example.com'
  662. ]);
  663. $this->artisan(
  664. 'anonaddy:receive-email',
  665. [
  666. 'file' => base_path('tests/emails/email_custom_domain.eml'),
  667. '--sender' => 'will@anonaddy.com',
  668. '--recipient' => ['ebay@example.com'],
  669. '--local_part' => ['ebay'],
  670. '--extension' => [''],
  671. '--domain' => ['example.com'],
  672. '--size' => '871'
  673. ]
  674. )->assertExitCode(0);
  675. $this->assertDatabaseHas('aliases', [
  676. 'aliasable_id' => $domain->id,
  677. 'aliasable_type' => 'App\Domain',
  678. 'email' => 'ebay@example.com',
  679. 'local_part' => 'ebay',
  680. 'domain' => 'example.com',
  681. 'emails_forwarded' => 1,
  682. 'emails_blocked' => 0
  683. ]);
  684. $this->assertDatabaseHas('users', [
  685. 'id' => $this->user->id,
  686. 'username' => 'johndoe',
  687. 'bandwidth' => '871'
  688. ]);
  689. $this->assertEquals(1, $this->user->aliases()->count());
  690. Mail::assertQueued(ForwardEmail::class, function ($mail) {
  691. return $mail->hasTo($this->user->email);
  692. });
  693. }
  694. /** @test */
  695. public function it_can_forward_email_for_additional_username()
  696. {
  697. Mail::fake();
  698. Mail::assertNothingSent();
  699. $additionalUsername = factory(AdditionalUsername::class)->create([
  700. 'user_id' => $this->user->id,
  701. 'username' => 'janedoe'
  702. ]);
  703. $this->artisan(
  704. 'anonaddy:receive-email',
  705. [
  706. 'file' => base_path('tests/emails/email_additional_username.eml'),
  707. '--sender' => 'will@anonaddy.com',
  708. '--recipient' => ['ebay@janedoe.anonaddy.com'],
  709. '--local_part' => ['ebay'],
  710. '--extension' => [''],
  711. '--domain' => ['janedoe.anonaddy.com'],
  712. '--size' => '638'
  713. ]
  714. )->assertExitCode(0);
  715. $this->assertDatabaseHas('aliases', [
  716. 'aliasable_id' => $additionalUsername->id,
  717. 'aliasable_type' => 'App\AdditionalUsername',
  718. 'email' => 'ebay@janedoe.anonaddy.com',
  719. 'local_part' => 'ebay',
  720. 'domain' => 'janedoe.anonaddy.com',
  721. 'emails_forwarded' => 1,
  722. 'emails_blocked' => 0
  723. ]);
  724. $this->assertDatabaseHas('users', [
  725. 'id' => $this->user->id,
  726. 'username' => 'johndoe',
  727. 'bandwidth' => '638'
  728. ]);
  729. $this->assertEquals(1, $this->user->aliases()->count());
  730. Mail::assertQueued(ForwardEmail::class, function ($mail) {
  731. return $mail->hasTo($this->user->email);
  732. });
  733. }
  734. /** @test */
  735. public function it_can_send_near_bandwidth_limit_notification()
  736. {
  737. Notification::fake();
  738. Notification::assertNothingSent();
  739. $this->user->update(['bandwidth' => 100943820]);
  740. $this->artisan(
  741. 'anonaddy:receive-email',
  742. [
  743. 'file' => base_path('tests/emails/email.eml'),
  744. '--sender' => 'will@anonaddy.com',
  745. '--recipient' => ['ebay@johndoe.anonaddy.com'],
  746. '--local_part' => ['ebay'],
  747. '--extension' => [''],
  748. '--domain' => ['johndoe.anonaddy.com'],
  749. '--size' => '1000'
  750. ]
  751. )->assertExitCode(0);
  752. $this->assertDatabaseHas('aliases', [
  753. 'email' => 'ebay@johndoe.'.config('anonaddy.domain'),
  754. 'local_part' => 'ebay',
  755. 'domain' => 'johndoe.'.config('anonaddy.domain'),
  756. 'emails_forwarded' => 1,
  757. 'emails_blocked' => 0
  758. ]);
  759. $this->assertEquals(1, $this->user->aliases()->count());
  760. $this->assertDatabaseHas('users', [
  761. 'id' => $this->user->id,
  762. 'username' => 'johndoe',
  763. 'bandwidth' => '100944820'
  764. ]);
  765. Notification::assertSentTo(
  766. $this->user,
  767. NearBandwidthLimit::class
  768. );
  769. }
  770. /** @test */
  771. public function it_does_not_send_near_bandwidth_limit_notification_more_than_once_per_day()
  772. {
  773. Notification::fake();
  774. Notification::assertNothingSent();
  775. Cache::put("user:{$this->user->username}:near-bandwidth", now()->toDateTimeString(), now()->addDay());
  776. $this->user->update(['bandwidth' => 9485760]);
  777. $this->artisan(
  778. 'anonaddy:receive-email',
  779. [
  780. 'file' => base_path('tests/emails/email.eml'),
  781. '--sender' => 'will@anonaddy.com',
  782. '--recipient' => ['ebay@johndoe.anonaddy.com'],
  783. '--local_part' => ['ebay'],
  784. '--extension' => [''],
  785. '--domain' => ['johndoe.anonaddy.com'],
  786. '--size' => '1000'
  787. ]
  788. )->assertExitCode(0);
  789. $this->assertDatabaseHas('aliases', [
  790. 'email' => 'ebay@johndoe.'.config('anonaddy.domain'),
  791. 'local_part' => 'ebay',
  792. 'domain' => 'johndoe.'.config('anonaddy.domain'),
  793. 'emails_forwarded' => 1,
  794. 'emails_blocked' => 0
  795. ]);
  796. $this->assertEquals(1, $this->user->aliases()->count());
  797. $this->assertDatabaseHas('users', [
  798. 'id' => $this->user->id,
  799. 'username' => 'johndoe',
  800. 'bandwidth' => '9486760'
  801. ]);
  802. Notification::assertNotSentTo(
  803. $this->user,
  804. NearBandwidthLimit::class
  805. );
  806. }
  807. /** @test */
  808. public function it_can_forward_email_from_file_for_all_domains()
  809. {
  810. Mail::fake();
  811. Mail::assertNothingSent();
  812. $this->artisan(
  813. 'anonaddy:receive-email',
  814. [
  815. 'file' => base_path('tests/emails/email_other_domain.eml'),
  816. '--sender' => 'will@anonaddy.com',
  817. '--recipient' => ['ebay@johndoe.anonaddy.me'],
  818. '--local_part' => ['ebay'],
  819. '--extension' => [''],
  820. '--domain' => ['johndoe.anonaddy.me'],
  821. '--size' => '1000'
  822. ]
  823. )->assertExitCode(0);
  824. $this->assertDatabaseHas('aliases', [
  825. 'email' => 'ebay@johndoe.anonaddy.me',
  826. 'local_part' => 'ebay',
  827. 'domain' => 'johndoe.anonaddy.me',
  828. 'emails_forwarded' => 1,
  829. 'emails_blocked' => 0
  830. ]);
  831. $this->assertEquals(1, $this->user->aliases()->count());
  832. $this->assertDatabaseHas('users', [
  833. 'id' => $this->user->id,
  834. 'username' => 'johndoe',
  835. 'bandwidth' => '1000'
  836. ]);
  837. Mail::assertQueued(ForwardEmail::class, function ($mail) {
  838. return $mail->hasTo($this->user->email);
  839. });
  840. }
  841. /** @test */
  842. public function it_can_forward_email_for_custom_domain_with_default_recipient()
  843. {
  844. Mail::fake();
  845. Mail::assertNothingSent();
  846. $recipient = factory(Recipient::class)->create([
  847. 'user_id' => $this->user->id
  848. ]);
  849. $domain = factory(Domain::class)->create([
  850. 'user_id' => $this->user->id,
  851. 'default_recipient_id' => $recipient->id,
  852. 'domain' => 'example.com',
  853. 'domain_verified_at' => now()
  854. ]);
  855. $this->artisan(
  856. 'anonaddy:receive-email',
  857. [
  858. 'file' => base_path('tests/emails/email_custom_domain.eml'),
  859. '--sender' => 'will@anonaddy.com',
  860. '--recipient' => ['ebay@example.com'],
  861. '--local_part' => ['ebay'],
  862. '--extension' => [''],
  863. '--domain' => ['example.com'],
  864. '--size' => '871'
  865. ]
  866. )->assertExitCode(0);
  867. $this->assertDatabaseHas('aliases', [
  868. 'aliasable_id' => $domain->id,
  869. 'aliasable_type' => 'App\Domain',
  870. 'email' => 'ebay@example.com',
  871. 'local_part' => 'ebay',
  872. 'domain' => 'example.com',
  873. 'emails_forwarded' => 1,
  874. 'emails_blocked' => 0
  875. ]);
  876. $this->assertDatabaseHas('users', [
  877. 'id' => $this->user->id,
  878. 'username' => 'johndoe',
  879. 'bandwidth' => '871'
  880. ]);
  881. $this->assertEquals(1, $this->user->aliases()->count());
  882. Mail::assertQueued(ForwardEmail::class, function ($mail) use ($recipient) {
  883. return $mail->hasTo($recipient->email);
  884. });
  885. }
  886. /** @test */
  887. public function it_can_forward_email_for_additional_username_with_default_recipient()
  888. {
  889. Mail::fake();
  890. Mail::assertNothingSent();
  891. $recipient = factory(Recipient::class)->create([
  892. 'user_id' => $this->user->id
  893. ]);
  894. $additionalUsername = factory(AdditionalUsername::class)->create([
  895. 'user_id' => $this->user->id,
  896. 'default_recipient_id' => $recipient->id,
  897. 'username' => 'janedoe'
  898. ]);
  899. $this->artisan(
  900. 'anonaddy:receive-email',
  901. [
  902. 'file' => base_path('tests/emails/email_additional_username.eml'),
  903. '--sender' => 'will@anonaddy.com',
  904. '--recipient' => ['ebay@janedoe.anonaddy.com'],
  905. '--local_part' => ['ebay'],
  906. '--extension' => [''],
  907. '--domain' => ['janedoe.anonaddy.com'],
  908. '--size' => '559'
  909. ]
  910. )->assertExitCode(0);
  911. $this->assertDatabaseHas('aliases', [
  912. 'aliasable_id' => $additionalUsername->id,
  913. 'aliasable_type' => 'App\AdditionalUsername',
  914. 'email' => 'ebay@janedoe.anonaddy.com',
  915. 'local_part' => 'ebay',
  916. 'domain' => 'janedoe.anonaddy.com',
  917. 'emails_forwarded' => 1,
  918. 'emails_blocked' => 0
  919. ]);
  920. $this->assertDatabaseHas('users', [
  921. 'id' => $this->user->id,
  922. 'username' => 'johndoe',
  923. 'bandwidth' => '559'
  924. ]);
  925. $this->assertEquals(1, $this->user->aliases()->count());
  926. Mail::assertQueued(ForwardEmail::class, function ($mail) use ($recipient) {
  927. return $mail->hasTo($recipient->email);
  928. });
  929. }
  930. }