ReceiveEmailTest.php 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847
  1. <?php
  2. namespace Tests\Feature;
  3. use App\Alias;
  4. use App\AliasRecipient;
  5. use App\Domain;
  6. use App\Mail\ForwardEmail;
  7. use App\Notifications\NearBandwidthLimit;
  8. use App\Recipient;
  9. use App\User;
  10. use Illuminate\Foundation\Testing\RefreshDatabase;
  11. use Illuminate\Support\Facades\Mail;
  12. use Illuminate\Support\Facades\Notification;
  13. use Tests\TestCase;
  14. class ReceiveEmailTest extends TestCase
  15. {
  16. use RefreshDatabase;
  17. protected $user;
  18. protected function setUp(): void
  19. {
  20. parent::setUp();
  21. $this->user = factory(User::class)->create(['username' => 'johndoe']);
  22. $this->user->recipients()->save($this->user->defaultRecipient);
  23. }
  24. /** @test */
  25. public function it_can_forward_email_from_file()
  26. {
  27. Mail::fake();
  28. Notification::fake();
  29. Mail::assertNothingSent();
  30. $this->artisan(
  31. 'anonaddy:receive-email',
  32. [
  33. 'file' => base_path('tests/emails/email.eml'),
  34. '--sender' => 'will@anonaddy.com',
  35. '--recipient' => ['ebay@johndoe.anonaddy.com'],
  36. '--local_part' => ['ebay'],
  37. '--extension' => [''],
  38. '--domain' => ['johndoe.anonaddy.com'],
  39. '--size' => '1000'
  40. ]
  41. )->assertExitCode(0);
  42. $this->assertDatabaseHas('aliases', [
  43. 'email' => 'ebay@johndoe.'.config('anonaddy.domain'),
  44. 'local_part' => 'ebay',
  45. 'domain' => 'johndoe.'.config('anonaddy.domain'),
  46. 'emails_forwarded' => 1,
  47. 'emails_blocked' => 0
  48. ]);
  49. $this->assertEquals(1, $this->user->aliases()->count());
  50. $this->assertDatabaseHas('users', [
  51. 'id' => $this->user->id,
  52. 'username' => 'johndoe',
  53. 'bandwidth' => '1000'
  54. ]);
  55. Mail::assertQueued(ForwardEmail::class, function ($mail) {
  56. return $mail->hasTo($this->user->email);
  57. });
  58. Notification::assertNothingSent();
  59. }
  60. /** @test */
  61. public function it_can_forward_email_from_file_with_attachment()
  62. {
  63. Mail::fake();
  64. Mail::assertNothingSent();
  65. $this->artisan(
  66. 'anonaddy:receive-email',
  67. [
  68. 'file' => base_path('tests/emails/email_with_attachment.eml'),
  69. '--sender' => 'will@anonaddy.com',
  70. '--recipient' => ['attachment@johndoe.anonaddy.com'],
  71. '--local_part' => ['attachment'],
  72. '--extension' => [''],
  73. '--domain' => ['johndoe.anonaddy.com'],
  74. '--size' => '1000'
  75. ]
  76. )->assertExitCode(0);
  77. $this->assertDatabaseHas('aliases', [
  78. 'email' => 'attachment@johndoe.'.config('anonaddy.domain'),
  79. 'local_part' => 'attachment',
  80. 'domain' => 'johndoe.'.config('anonaddy.domain'),
  81. 'emails_forwarded' => 1,
  82. 'emails_blocked' => 0
  83. ]);
  84. $this->assertEquals(1, $this->user->aliases()->count());
  85. $this->assertDatabaseHas('users', [
  86. 'id' => $this->user->id,
  87. 'username' => 'johndoe',
  88. 'bandwidth' => '1000'
  89. ]);
  90. Mail::assertQueued(ForwardEmail::class, function ($mail) {
  91. return $mail->hasTo($this->user->email);
  92. });
  93. }
  94. /** @test */
  95. public function it_can_forward_email_from_file_to_multiple_recipients()
  96. {
  97. Mail::fake();
  98. Mail::assertNothingSent();
  99. $this->artisan(
  100. 'anonaddy:receive-email',
  101. [
  102. 'file' => base_path('tests/emails/email_multiple_recipients.eml'),
  103. '--sender' => 'will@anonaddy.com',
  104. '--recipient' => ['ebay@johndoe.anonaddy.com', 'amazon@johndoe.anonaddy.com', 'paypal@johndoe.anonaddy.me'],
  105. '--local_part' => ['ebay', 'amazon', 'paypal'],
  106. '--extension' => ['', '', ''],
  107. '--domain' => ['johndoe.anonaddy.com', 'johndoe.anonaddy.com', 'johndoe.anonaddy.com'],
  108. '--size' => '1217'
  109. ]
  110. )->assertExitCode(0);
  111. $this->assertDatabaseHas('aliases', [
  112. 'email' => 'ebay@johndoe.'.config('anonaddy.domain'),
  113. 'local_part' => 'ebay',
  114. 'domain' => 'johndoe.'.config('anonaddy.domain'),
  115. 'emails_forwarded' => 1,
  116. 'emails_blocked' => 0
  117. ]);
  118. $this->assertDatabaseHas('aliases', [
  119. 'email' => 'amazon@johndoe.'.config('anonaddy.domain'),
  120. 'local_part' => 'amazon',
  121. 'domain' => 'johndoe.'.config('anonaddy.domain'),
  122. 'emails_forwarded' => 1,
  123. 'emails_blocked' => 0
  124. ]);
  125. $this->assertDatabaseHas('aliases', [
  126. 'email' => 'paypal@johndoe.'.config('anonaddy.domain'),
  127. 'local_part' => 'paypal',
  128. 'domain' => 'johndoe.'.config('anonaddy.domain'),
  129. 'emails_forwarded' => 1,
  130. 'emails_blocked' => 0
  131. ]);
  132. $this->assertEquals(3, $this->user->aliases()->count());
  133. $this->assertDatabaseHas('users', [
  134. 'id' => $this->user->id,
  135. 'username' => 'johndoe',
  136. 'bandwidth' => '1217'
  137. ]);
  138. Mail::assertQueued(ForwardEmail::class, function ($mail) {
  139. return $mail->hasTo($this->user->email);
  140. });
  141. }
  142. /** @test */
  143. public function it_can_forward_email_from_file_with_extension()
  144. {
  145. Mail::fake();
  146. Mail::assertNothingSent();
  147. $this->artisan(
  148. 'anonaddy:receive-email',
  149. [
  150. 'file' => base_path('tests/emails/email_with_extension.eml'),
  151. '--sender' => 'will@anonaddy.com',
  152. '--recipient' => ['ebay+a@johndoe.anonaddy.com'],
  153. '--local_part' => ['ebay'],
  154. '--extension' => ['2.3'],
  155. '--domain' => ['johndoe.anonaddy.com'],
  156. '--size' => '789'
  157. ]
  158. )->assertExitCode(0);
  159. $this->assertDatabaseHas('aliases', [
  160. 'email' => 'ebay@johndoe.'.config('anonaddy.domain'),
  161. 'local_part' => 'ebay',
  162. 'domain' => 'johndoe.'.config('anonaddy.domain'),
  163. 'emails_forwarded' => 1,
  164. 'emails_blocked' => 0
  165. ]);
  166. $this->assertDatabaseHas('users', [
  167. 'id' => $this->user->id,
  168. 'username' => 'johndoe',
  169. 'bandwidth' => '789'
  170. ]);
  171. $this->assertEquals(1, $this->user->aliases()->count());
  172. Mail::assertQueued(ForwardEmail::class, function ($mail) {
  173. return $mail->hasTo($this->user->email);
  174. });
  175. }
  176. /** @test */
  177. public function it_can_forward_email_with_existing_alias()
  178. {
  179. Mail::fake();
  180. Mail::assertNothingSent();
  181. factory(Alias::class)->create([
  182. 'user_id' => $this->user->id,
  183. 'email' => 'ebay@johndoe.'.config('anonaddy.domain'),
  184. 'local_part' => 'ebay',
  185. 'domain' => 'johndoe.'.config('anonaddy.domain'),
  186. ]);
  187. $defaultRecipient = $this->user->defaultRecipient;
  188. $this->artisan(
  189. 'anonaddy:receive-email',
  190. [
  191. 'file' => base_path('tests/emails/email.eml'),
  192. '--sender' => 'will@anonaddy.com',
  193. '--recipient' => ['ebay@johndoe.anonaddy.com'],
  194. '--local_part' => ['ebay'],
  195. '--extension' => [''],
  196. '--domain' => ['johndoe.anonaddy.com'],
  197. '--size' => '559'
  198. ]
  199. )->assertExitCode(0);
  200. $this->assertDatabaseHas('aliases', [
  201. 'email' => 'ebay@johndoe.'.config('anonaddy.domain'),
  202. 'emails_forwarded' => 1,
  203. 'emails_blocked' => 0
  204. ]);
  205. $this->assertDatabaseHas('users', [
  206. 'id' => $this->user->id,
  207. 'username' => 'johndoe',
  208. 'bandwidth' => '559'
  209. ]);
  210. $this->assertCount(1, $this->user->aliases);
  211. Mail::assertQueued(ForwardEmail::class, function ($mail) use ($defaultRecipient) {
  212. return $mail->hasTo($defaultRecipient->email);
  213. });
  214. }
  215. /** @test */
  216. public function it_can_forward_email_with_uuid_generated_alias()
  217. {
  218. Mail::fake();
  219. Mail::assertNothingSent();
  220. $uuid = '86064c92-da41-443e-a2bf-5a7b0247842f';
  221. config([
  222. 'anonaddy.admin_username' => 'random'
  223. ]);
  224. factory(Alias::class)->create([
  225. 'id' => $uuid,
  226. 'user_id' => $this->user->id,
  227. 'email' => $uuid.'@anonaddy.me',
  228. 'local_part' => $uuid,
  229. 'domain' => 'anonaddy.me',
  230. ]);
  231. $defaultRecipient = $this->user->defaultRecipient;
  232. $this->artisan(
  233. 'anonaddy:receive-email',
  234. [
  235. 'file' => base_path('tests/emails/email_with_uuid.eml'),
  236. '--sender' => 'will@anonaddy.com',
  237. '--recipient' => [$uuid.'@anonaddy.me'],
  238. '--local_part' => [$uuid],
  239. '--extension' => [''],
  240. '--domain' => ['anonaddy.me'],
  241. '--size' => '892'
  242. ]
  243. )->assertExitCode(0);
  244. $this->assertDatabaseHas('aliases', [
  245. 'local_part' => $uuid,
  246. 'domain' => 'anonaddy.me',
  247. 'email' => $uuid.'@anonaddy.me',
  248. 'emails_forwarded' => 1,
  249. 'emails_blocked' => 0
  250. ]);
  251. $this->assertDatabaseHas('users', [
  252. 'id' => $this->user->id,
  253. 'username' => 'johndoe',
  254. 'bandwidth' => '892'
  255. ]);
  256. $this->assertCount(1, $this->user->aliases);
  257. Mail::assertQueued(ForwardEmail::class, function ($mail) use ($defaultRecipient) {
  258. return $mail->hasTo($defaultRecipient->email);
  259. });
  260. }
  261. /** @test */
  262. public function it_can_forward_email_with_existing_alias_and_receipients()
  263. {
  264. Mail::fake();
  265. Mail::assertNothingSent();
  266. $alias = factory(Alias::class)->create([
  267. 'user_id' => $this->user->id,
  268. 'email' => 'ebay@johndoe.'.config('anonaddy.domain'),
  269. 'local_part' => 'ebay',
  270. 'domain' => 'johndoe.'.config('anonaddy.domain'),
  271. ]);
  272. $recipient = factory(Recipient::class)->create([
  273. 'user_id' => $this->user->id,
  274. 'email' => 'one@example.com'
  275. ]);
  276. $recipient2 = factory(Recipient::class)->create([
  277. 'user_id' => $this->user->id,
  278. 'email' => 'two@example.com'
  279. ]);
  280. AliasRecipient::create([
  281. 'alias' => $alias,
  282. 'recipient' => $recipient
  283. ]);
  284. AliasRecipient::create([
  285. 'alias' => $alias,
  286. 'recipient' => $recipient2
  287. ]);
  288. $this->artisan(
  289. 'anonaddy:receive-email',
  290. [
  291. 'file' => base_path('tests/emails/email.eml'),
  292. '--sender' => 'will@anonaddy.com',
  293. '--recipient' => ['ebay@johndoe.anonaddy.com'],
  294. '--local_part' => ['ebay'],
  295. '--extension' => [''],
  296. '--domain' => ['johndoe.anonaddy.com'],
  297. '--size' => '444'
  298. ]
  299. )->assertExitCode(0);
  300. $this->assertDatabaseHas('aliases', [
  301. 'email' => 'ebay@johndoe.'.config('anonaddy.domain'),
  302. 'emails_forwarded' => 1,
  303. 'emails_blocked' => 0
  304. ]);
  305. $this->assertDatabaseHas('users', [
  306. 'id' => $this->user->id,
  307. 'username' => 'johndoe',
  308. 'bandwidth' => '444'
  309. ]);
  310. Mail::assertQueued(ForwardEmail::class, function ($mail) {
  311. return $mail->hasTo('one@example.com') &&
  312. $mail->hasTo('two@example.com');
  313. });
  314. }
  315. /** @test */
  316. public function it_can_attach_recipients_to_new_alias_with_extension()
  317. {
  318. Mail::fake();
  319. Mail::assertNothingSent();
  320. $recipient = factory(Recipient::class)->create([
  321. 'user_id' => $this->user->id,
  322. 'email' => 'one@example.com'
  323. ]);
  324. $recipient2 = factory(Recipient::class)->create([
  325. 'user_id' => $this->user->id,
  326. 'email' => 'two@example.com'
  327. ]);
  328. $this->artisan(
  329. 'anonaddy:receive-email',
  330. [
  331. 'file' => base_path('tests/emails/email_with_extension.eml'),
  332. '--sender' => 'will@anonaddy.com',
  333. '--recipient' => ['ebay@johndoe.anonaddy.com'],
  334. '--local_part' => ['ebay'],
  335. '--extension' => ['2.3'],
  336. '--domain' => ['johndoe.anonaddy.com'],
  337. '--size' => '444'
  338. ]
  339. )->assertExitCode(0);
  340. $this->assertDatabaseHas('aliases', [
  341. 'extension' => '2.3',
  342. 'email' => 'ebay@johndoe.'.config('anonaddy.domain'),
  343. 'emails_forwarded' => 1,
  344. 'emails_blocked' => 0
  345. ]);
  346. $this->assertDatabaseHas('users', [
  347. 'id' => $this->user->id,
  348. 'username' => 'johndoe',
  349. 'bandwidth' => '444'
  350. ]);
  351. Mail::assertQueued(ForwardEmail::class, function ($mail) use ($recipient, $recipient2) {
  352. return $mail->hasTo($recipient->email) &&
  353. $mail->hasTo($recipient2->email);
  354. });
  355. }
  356. /** @test */
  357. public function it_can_not_attach_unverified_recipient_to_new_alias_with_extension()
  358. {
  359. Mail::fake();
  360. Mail::assertNothingSent();
  361. $defaultRecipient = $this->user->defaultRecipient;
  362. factory(Recipient::class)->create([
  363. 'user_id' => $this->user->id,
  364. 'email' => 'one@example.com',
  365. 'email_verified_at' => null
  366. ]);
  367. $verifiedRecipient = factory(Recipient::class)->create([
  368. 'user_id' => $this->user->id,
  369. 'email' => 'two@example.com'
  370. ]);
  371. $this->artisan(
  372. 'anonaddy:receive-email',
  373. [
  374. 'file' => base_path('tests/emails/email_with_extension.eml'),
  375. '--sender' => 'will@anonaddy.com',
  376. '--recipient' => ['ebay@johndoe.anonaddy.com'],
  377. '--local_part' => ['ebay'],
  378. '--extension' => ['2.3'],
  379. '--domain' => ['johndoe.anonaddy.com'],
  380. '--size' => '444'
  381. ]
  382. )->assertExitCode(0);
  383. $this->assertDatabaseHas('aliases', [
  384. 'email' => 'ebay@johndoe.'.config('anonaddy.domain'),
  385. 'emails_forwarded' => 1,
  386. 'emails_blocked' => 0
  387. ]);
  388. $this->assertDatabaseHas('users', [
  389. 'id' => $this->user->id,
  390. 'username' => 'johndoe',
  391. 'bandwidth' => '444'
  392. ]);
  393. $alias = $this->user->aliases()->where('email', 'ebay@johndoe.'.config('anonaddy.domain'))->first();
  394. $this->assertCount(1, $alias->recipients);
  395. Mail::assertQueued(ForwardEmail::class, function ($mail) use ($defaultRecipient, $verifiedRecipient) {
  396. return $mail->hasTo($verifiedRecipient->email);
  397. });
  398. }
  399. /** @test */
  400. public function it_does_not_send_email_if_default_recipient_has_not_yet_been_verified()
  401. {
  402. Mail::fake();
  403. Mail::assertNothingSent();
  404. $this->user->defaultRecipient->update(['email_verified_at' => null]);
  405. $this->assertNull($this->user->defaultRecipient->email_verified_at);
  406. $this->artisan(
  407. 'anonaddy:receive-email',
  408. [
  409. 'file' => base_path('tests/emails/email.eml'),
  410. '--sender' => 'will@anonaddy.com',
  411. '--recipient' => ['ebay@johndoe.anonaddy.com'],
  412. '--local_part' => ['ebay'],
  413. '--extension' => [''],
  414. '--domain' => ['johndoe.anonaddy.com'],
  415. '--size' => '1000'
  416. ]
  417. )->assertExitCode(0);
  418. $this->assertDatabaseMissing('aliases', [
  419. 'email' => 'ebay@johndoe.'.config('anonaddy.domain'),
  420. 'emails_blocked' => 0
  421. ]);
  422. $this->assertDatabaseHas('users', [
  423. 'id' => $this->user->id,
  424. 'username' => 'johndoe',
  425. 'bandwidth' => '0'
  426. ]);
  427. Mail::assertNotSent(ForwardEmail::class);
  428. }
  429. /** @test */
  430. public function it_can_unsubscribe_alias_by_emailing_list_unsubscribe()
  431. {
  432. Mail::fake();
  433. Mail::assertNothingSent();
  434. factory(Alias::class)->create([
  435. 'id' => '8f36380f-df4e-4875-bb12-9c4448573712',
  436. 'user_id' => $this->user->id,
  437. 'email' => 'ebay@johndoe.'.config('anonaddy.domain'),
  438. 'local_part' => 'ebay',
  439. 'domain' => 'johndoe.'.config('anonaddy.domain')
  440. ]);
  441. factory(Recipient::class)->create([
  442. 'user_id' => $this->user->id,
  443. 'email' => 'will@anonaddy.com'
  444. ]);
  445. $this->assertDatabaseHas('aliases', [
  446. 'id' => '8f36380f-df4e-4875-bb12-9c4448573712',
  447. 'user_id' => $this->user->id,
  448. 'email' => 'ebay@johndoe.'.config('anonaddy.domain'),
  449. 'active' => true
  450. ]);
  451. $this->artisan(
  452. 'anonaddy:receive-email',
  453. [
  454. 'file' => base_path('tests/emails/email_unsubscribe.eml'),
  455. '--sender' => 'will@anonaddy.com',
  456. '--recipient' => ['8f36380f-df4e-4875-bb12-9c4448573712@unsubscribe.anonaddy.com'],
  457. '--local_part' => ['8f36380f-df4e-4875-bb12-9c4448573712'],
  458. '--extension' => [''],
  459. '--domain' => ['unsubscribe.anonaddy.com'],
  460. '--size' => '1000'
  461. ]
  462. )->assertExitCode(0);
  463. $this->assertDatabaseHas('aliases', [
  464. 'id' => '8f36380f-df4e-4875-bb12-9c4448573712',
  465. 'user_id' => $this->user->id,
  466. 'email' => 'ebay@johndoe.'.config('anonaddy.domain'),
  467. 'local_part' => 'ebay',
  468. 'domain' => 'johndoe.'.config('anonaddy.domain'),
  469. 'active' => false
  470. ]);
  471. $this->assertDatabaseHas('users', [
  472. 'id' => $this->user->id,
  473. 'username' => 'johndoe',
  474. 'bandwidth' => '0'
  475. ]);
  476. Mail::assertNotSent(ForwardEmail::class);
  477. }
  478. /** @test */
  479. public function it_does_not_count_unsubscribe_recipient_when_calculating_size()
  480. {
  481. Mail::fake();
  482. Mail::assertNothingSent();
  483. factory(Alias::class)->create([
  484. 'id' => '8f36380f-df4e-4875-bb12-9c4448573712',
  485. 'user_id' => $this->user->id,
  486. 'email' => 'ebay@johndoe.'.config('anonaddy.domain'),
  487. 'local_part' => 'ebay',
  488. 'domain' => 'johndoe.'.config('anonaddy.domain')
  489. ]);
  490. factory(Recipient::class)->create([
  491. 'user_id' => $this->user->id,
  492. 'email' => 'will@anonaddy.com'
  493. ]);
  494. $this->assertDatabaseHas('aliases', [
  495. 'id' => '8f36380f-df4e-4875-bb12-9c4448573712',
  496. 'user_id' => $this->user->id,
  497. 'email' => 'ebay@johndoe.'.config('anonaddy.domain'),
  498. 'active' => true
  499. ]);
  500. $this->artisan(
  501. 'anonaddy:receive-email',
  502. [
  503. 'file' => base_path('tests/emails/email_unsubscribe_plus_other_recipient.eml'),
  504. '--sender' => 'will@anonaddy.com',
  505. '--recipient' => ['8f36380f-df4e-4875-bb12-9c4448573712@unsubscribe.anonaddy.com', 'another@johndoe.anonaddy.com'],
  506. '--local_part' => ['8f36380f-df4e-4875-bb12-9c4448573712', 'another'],
  507. '--extension' => ['', ''],
  508. '--domain' => ['unsubscribe.anonaddy.com', 'johndoe.anonaddy.com'],
  509. '--size' => '1000'
  510. ]
  511. )->assertExitCode(0);
  512. $this->assertDatabaseHas('aliases', [
  513. 'id' => '8f36380f-df4e-4875-bb12-9c4448573712',
  514. 'user_id' => $this->user->id,
  515. 'email' => 'ebay@johndoe.'.config('anonaddy.domain'),
  516. 'local_part' => 'ebay',
  517. 'domain' => 'johndoe.'.config('anonaddy.domain'),
  518. 'active' => false
  519. ]);
  520. $this->assertDatabaseHas('aliases', [
  521. 'user_id' => $this->user->id,
  522. 'email' => 'another@johndoe.'.config('anonaddy.domain'),
  523. 'local_part' => 'another',
  524. 'domain' => 'johndoe.'.config('anonaddy.domain'),
  525. 'active' => true
  526. ]);
  527. $this->assertDatabaseHas('users', [
  528. 'id' => $this->user->id,
  529. 'username' => 'johndoe',
  530. 'bandwidth' => '1000'
  531. ]);
  532. Mail::assertNotSent(ForwardEmail::class);
  533. }
  534. /** @test */
  535. public function it_cannot_unsubscribe_alias_if_not_a_verified_user_recipient()
  536. {
  537. Mail::fake();
  538. Mail::assertNothingSent();
  539. factory(Alias::class)->create([
  540. 'id' => '8f36380f-df4e-4875-bb12-9c4448573712',
  541. 'user_id' => $this->user->id,
  542. 'email' => 'ebay@johndoe.'.config('anonaddy.domain'),
  543. 'local_part' => 'ebay',
  544. 'domain' => 'johndoe.'.config('anonaddy.domain')
  545. ]);
  546. $this->assertDatabaseHas('aliases', [
  547. 'id' => '8f36380f-df4e-4875-bb12-9c4448573712',
  548. 'user_id' => $this->user->id,
  549. 'email' => 'ebay@johndoe.'.config('anonaddy.domain'),
  550. 'active' => true
  551. ]);
  552. $this->artisan(
  553. 'anonaddy:receive-email',
  554. [
  555. 'file' => base_path('tests/emails/email_unsubscribe.eml'),
  556. '--sender' => 'will@anonaddy.com',
  557. '--recipient' => ['8f36380f-df4e-4875-bb12-9c4448573712@unsubscribe.anonaddy.com'],
  558. '--local_part' => ['8f36380f-df4e-4875-bb12-9c4448573712'],
  559. '--extension' => [''],
  560. '--domain' => ['unsubscribe.anonaddy.com'],
  561. '--size' => '1000'
  562. ]
  563. )->assertExitCode(0);
  564. $this->assertDatabaseHas('aliases', [
  565. 'id' => '8f36380f-df4e-4875-bb12-9c4448573712',
  566. 'user_id' => $this->user->id,
  567. 'email' => 'ebay@johndoe.'.config('anonaddy.domain'),
  568. 'local_part' => 'ebay',
  569. 'domain' => 'johndoe.'.config('anonaddy.domain'),
  570. 'active' => true
  571. ]);
  572. $this->assertDatabaseHas('users', [
  573. 'id' => $this->user->id,
  574. 'username' => 'johndoe',
  575. 'bandwidth' => '0'
  576. ]);
  577. Mail::assertNotSent(ForwardEmail::class);
  578. }
  579. /** @test */
  580. public function it_can_forward_email_to_admin_username_for_root_domain()
  581. {
  582. Mail::fake();
  583. Mail::assertNothingSent();
  584. config(['anonaddy.admin_username' => 'johndoe']);
  585. $this->artisan(
  586. 'anonaddy:receive-email',
  587. [
  588. 'file' => base_path('tests/emails/email_admin.eml'),
  589. '--sender' => 'will@anonaddy.com',
  590. '--recipient' => ['ebay@anonaddy.me'],
  591. '--local_part' => ['ebay'],
  592. '--extension' => [''],
  593. '--domain' => ['anonaddy.me'],
  594. '--size' => '1346'
  595. ]
  596. )->assertExitCode(0);
  597. $this->assertDatabaseHas('aliases', [
  598. 'email' => 'ebay@anonaddy.me',
  599. 'local_part' => 'ebay',
  600. 'domain' => 'anonaddy.me',
  601. 'emails_forwarded' => 1,
  602. 'emails_blocked' => 0
  603. ]);
  604. $this->assertDatabaseHas('users', [
  605. 'id' => $this->user->id,
  606. 'username' => 'johndoe',
  607. 'bandwidth' => '1346'
  608. ]);
  609. $this->assertEquals(1, $this->user->aliases()->count());
  610. Mail::assertQueued(ForwardEmail::class, function ($mail) {
  611. return $mail->hasTo($this->user->email);
  612. });
  613. }
  614. /** @test */
  615. public function it_can_forward_email_for_custom_domain()
  616. {
  617. Mail::fake();
  618. Mail::assertNothingSent();
  619. $domain = factory(Domain::class)->create([
  620. 'user_id' => $this->user->id,
  621. 'domain' => 'example.com'
  622. ]);
  623. $this->artisan(
  624. 'anonaddy:receive-email',
  625. [
  626. 'file' => base_path('tests/emails/email_custom_domain.eml'),
  627. '--sender' => 'will@anonaddy.com',
  628. '--recipient' => ['ebay@example.com'],
  629. '--local_part' => ['ebay'],
  630. '--extension' => [''],
  631. '--domain' => ['example.com'],
  632. '--size' => '871'
  633. ]
  634. )->assertExitCode(0);
  635. $this->assertDatabaseHas('aliases', [
  636. 'domain_id' => $domain->id,
  637. 'email' => 'ebay@example.com',
  638. 'local_part' => 'ebay',
  639. 'domain' => 'example.com',
  640. 'emails_forwarded' => 1,
  641. 'emails_blocked' => 0
  642. ]);
  643. $this->assertDatabaseHas('users', [
  644. 'id' => $this->user->id,
  645. 'username' => 'johndoe',
  646. 'bandwidth' => '871'
  647. ]);
  648. $this->assertEquals(1, $this->user->aliases()->count());
  649. Mail::assertQueued(ForwardEmail::class, function ($mail) {
  650. return $mail->hasTo($this->user->email);
  651. });
  652. }
  653. /** @test */
  654. public function it_can_send_near_bandwidth_limit_notification()
  655. {
  656. Notification::fake();
  657. Notification::assertNothingSent();
  658. $this->user->update(['bandwidth' => 100943820]);
  659. $this->artisan(
  660. 'anonaddy:receive-email',
  661. [
  662. 'file' => base_path('tests/emails/email.eml'),
  663. '--sender' => 'will@anonaddy.com',
  664. '--recipient' => ['ebay@johndoe.anonaddy.com'],
  665. '--local_part' => ['ebay'],
  666. '--extension' => [''],
  667. '--domain' => ['johndoe.anonaddy.com'],
  668. '--size' => '1000'
  669. ]
  670. )->assertExitCode(0);
  671. $this->assertDatabaseHas('aliases', [
  672. 'email' => 'ebay@johndoe.'.config('anonaddy.domain'),
  673. 'local_part' => 'ebay',
  674. 'domain' => 'johndoe.'.config('anonaddy.domain'),
  675. 'emails_forwarded' => 1,
  676. 'emails_blocked' => 0
  677. ]);
  678. $this->assertEquals(1, $this->user->aliases()->count());
  679. $this->assertDatabaseHas('users', [
  680. 'id' => $this->user->id,
  681. 'username' => 'johndoe',
  682. 'bandwidth' => '100944820'
  683. ]);
  684. Notification::assertSentTo(
  685. $this->user,
  686. NearBandwidthLimit::class
  687. );
  688. }
  689. /** @test */
  690. public function it_can_forward_email_from_file_for_all_domains()
  691. {
  692. Mail::fake();
  693. Mail::assertNothingSent();
  694. $this->artisan(
  695. 'anonaddy:receive-email',
  696. [
  697. 'file' => base_path('tests/emails/email_other_domain.eml'),
  698. '--sender' => 'will@anonaddy.com',
  699. '--recipient' => ['ebay@johndoe.anonaddy.me'],
  700. '--local_part' => ['ebay'],
  701. '--extension' => [''],
  702. '--domain' => ['johndoe.anonaddy.me'],
  703. '--size' => '1000'
  704. ]
  705. )->assertExitCode(0);
  706. $this->assertDatabaseHas('aliases', [
  707. 'email' => 'ebay@johndoe.anonaddy.me',
  708. 'local_part' => 'ebay',
  709. 'domain' => 'johndoe.anonaddy.me',
  710. 'emails_forwarded' => 1,
  711. 'emails_blocked' => 0
  712. ]);
  713. $this->assertEquals(1, $this->user->aliases()->count());
  714. $this->assertDatabaseHas('users', [
  715. 'id' => $this->user->id,
  716. 'username' => 'johndoe',
  717. 'bandwidth' => '1000'
  718. ]);
  719. Mail::assertQueued(ForwardEmail::class, function ($mail) {
  720. return $mail->hasTo($this->user->email);
  721. });
  722. }
  723. }