浏览代码

Attach recipients to new aliases

Will Browning 6 年之前
父节点
当前提交
109bd030cd
共有 3 个文件被更改,包括 76 次插入13 次删除
  1. 26 11
      app/Console/Commands/ReceiveEmail.php
  2. 49 1
      tests/Feature/ReceiveEmailTest.php
  3. 1 1
      tests/emails/email_with_extension.eml

+ 26 - 11
app/Console/Commands/ReceiveEmail.php

@@ -162,11 +162,6 @@ class ReceiveEmail extends Command
 
     protected function handleForward($user, $recipient, $customDomainId)
     {
-        if ($recipient['extension'] !== '') {
-            // TODO override default recipient for alias?
-            // or pass number and if forwarded equals that no. then block?
-        }
-
         $alias = $user->aliases()->firstOrNew([
             'email' => $recipient['local_part'] . '@' . $recipient['domain'],
             'local_part' => $recipient['local_part'],
@@ -174,13 +169,33 @@ class ReceiveEmail extends Command
             'domain_id' => $customDomainId
         ]);
 
-        if (!isset($alias->id) && $user->hasExceededNewAliasLimit()) {
-            $this->error('4.2.1 New aliases per hour limit exceeded for user ' . $user->username . '.');
+        if (!isset($alias->id)) {
+            // this is a new alias
+            if ($user->hasExceededNewAliasLimit()) {
+                $this->error('4.2.1 New aliases per hour limit exceeded for user ' . $user->username . '.');
 
-            exit(1);
-        } else {
-            $alias->save();
-            $alias->refresh();
+                exit(1);
+            }
+
+            if ($recipient['extension'] !== '') {
+                $ids = explode('.', $recipient['extension']);
+
+                $recipient_ids = $user
+                                    ->recipients()
+                                    ->latest()
+                                    ->pluck('id')
+                                    ->filter(function ($value, $key) use ($ids) {
+                                        return in_array($key+1, $ids);
+                                    })
+                                    ->toArray();
+            }
+        }
+
+        $alias->save();
+        $alias->refresh();
+
+        if (isset($recipient_ids)) {
+            $alias->recipients()->sync($recipient_ids);
         }
 
         $emailData = new EmailData($this->parser);

+ 49 - 1
tests/Feature/ReceiveEmailTest.php

@@ -176,7 +176,7 @@ class ReceiveEmailTest extends TestCase
                 '--sender' => 'will@anonaddy.com',
                 '--recipient' => ['ebay+a@johndoe.anonaddy.com'],
                 '--local_part' => ['ebay'],
-                '--extension' => ['a'],
+                '--extension' => ['2.3'],
                 '--domain' => ['johndoe.anonaddy.com'],
                 '--size' => '789'
             ]
@@ -311,6 +311,54 @@ class ReceiveEmailTest extends TestCase
         });
     }
 
+    /** @test */
+    public function it_can_attach_recipients_to_new_alias_with_extension()
+    {
+        $this->withoutExceptionHandling();
+        Mail::fake();
+
+        Mail::assertNothingSent();
+
+        $recipient = factory(Recipient::class)->create([
+            'user_id' => $this->user->id,
+            'email' => 'one@example.com'
+        ]);
+
+        $recipient2 = factory(Recipient::class)->create([
+            'user_id' => $this->user->id,
+            'email' => 'two@example.com'
+        ]);
+
+        $this->artisan(
+            'anonaddy:receive-email',
+            [
+                'file' => base_path('tests/emails/email_with_extension.eml'),
+                '--sender' => 'will@anonaddy.com',
+                '--recipient' => ['ebay@johndoe.anonaddy.com'],
+                '--local_part' => ['ebay'],
+                '--extension' => ['2.3'],
+                '--domain' => ['johndoe.anonaddy.com'],
+                '--size' => '444'
+            ]
+        )->assertExitCode(0);
+
+        $this->assertDatabaseHas('aliases', [
+            'email' => 'ebay@johndoe.'.config('anonaddy.domain'),
+            'emails_forwarded' => 1,
+            'emails_blocked' => 0
+        ]);
+        $this->assertDatabaseHas('users', [
+            'id' => $this->user->id,
+            'username' => 'johndoe',
+            'bandwidth' => '444'
+        ]);
+
+        Mail::assertQueued(ForwardEmail::class, function ($mail) use ($recipient, $recipient2) {
+            return $mail->hasTo($recipient->email) &&
+                   $mail->hasTo($recipient2->email);
+        });
+    }
+
     /** @test */
     public function it_does_not_send_email_if_default_recipient_has_not_yet_been_verified()
     {

+ 1 - 1
tests/emails/email_with_extension.eml

@@ -1,6 +1,6 @@
 Date: Wed, 20 Feb 2019 15:00:00 +0100 (CET)
 From: Will <will@anonaddy.com>
-To: <ebay+a@johndoe.anonaddy.com>
+To: <ebay+2.3@johndoe.anonaddy.com>
 Subject: Test Email
 Content-Type: multipart/mixed; boundary="----=_Part_10031_1199410393.1550677940425"