diff options
Diffstat (limited to 'tests/units/Integration')
-rw-r--r-- | tests/units/Integration/BitbucketWebhookTest.php | 398 | ||||
-rw-r--r-- | tests/units/Integration/GithubWebhookTest.php | 459 | ||||
-rw-r--r-- | tests/units/Integration/GitlabWebhookTest.php | 221 | ||||
-rw-r--r-- | tests/units/Integration/MailgunTest.php | 71 | ||||
-rw-r--r-- | tests/units/Integration/PostmarkTest.php | 106 | ||||
-rw-r--r-- | tests/units/Integration/SendgridTest.php | 135 |
6 files changed, 1390 insertions, 0 deletions
diff --git a/tests/units/Integration/BitbucketWebhookTest.php b/tests/units/Integration/BitbucketWebhookTest.php new file mode 100644 index 00000000..1902f738 --- /dev/null +++ b/tests/units/Integration/BitbucketWebhookTest.php @@ -0,0 +1,398 @@ +<?php + +require_once __DIR__.'/../Base.php'; + +use Integration\BitbucketWebhook; +use Model\TaskCreation; +use Model\TaskFinder; +use Model\Project; +use Model\ProjectPermission; +use Model\User; + +class BitbucketWebhookTest extends Base +{ + public function testHandlePush() + { + $this->container['dispatcher']->addListener(BitbucketWebhook::EVENT_COMMIT, array($this, 'onCommit')); + + $tc = new TaskCreation($this->container); + $p = new Project($this->container); + $bw = new BitbucketWebhook($this->container); + $payload = json_decode(file_get_contents(__DIR__.'/../fixtures/bitbucket_push.json'), true); + + $this->assertEquals(1, $p->create(array('name' => 'test'))); + $bw->setProjectId(1); + + // No task + $this->assertFalse($bw->handlePush($payload)); + + // Create task with the wrong id + $this->assertEquals(1, $tc->create(array('title' => 'test1', 'project_id' => 1))); + $this->assertFalse($bw->handlePush($payload)); + + // Create task with the right id + $this->assertEquals(2, $tc->create(array('title' => 'test2', 'project_id' => 1))); + $this->assertTrue($bw->handlePush($payload)); + + $called = $this->container['dispatcher']->getCalledListeners(); + $this->assertArrayHasKey(BitbucketWebhook::EVENT_COMMIT.'.BitbucketWebhookTest::onCommit', $called); + } + + public function testIssueOpened() + { + $this->container['dispatcher']->addListener(BitbucketWebhook::EVENT_ISSUE_OPENED, array($this, 'onIssueOpened')); + + $p = new Project($this->container); + $this->assertEquals(1, $p->create(array('name' => 'foobar'))); + + $bw = new BitbucketWebhook($this->container); + $bw->setProjectId(1); + + $this->assertNotFalse($bw->parsePayload( + 'issue:created', + json_decode(file_get_contents(__DIR__.'/../fixtures/bitbucket_issue_opened.json'), true) + )); + } + + public function testCommentCreatedWithNoUser() + { + $this->container['dispatcher']->addListener(BitbucketWebhook::EVENT_ISSUE_COMMENT, array($this, 'onCommentCreatedWithNoUser')); + + $p = new Project($this->container); + $this->assertEquals(1, $p->create(array('name' => 'foobar'))); + + $tc = new TaskCreation($this->container); + $this->assertEquals(1, $tc->create(array('title' => 'boo', 'reference' => 1, 'project_id' => 1))); + + $g = new BitbucketWebhook($this->container); + $g->setProjectId(1); + + $this->assertNotFalse($g->parsePayload( + 'issue:comment_created', + json_decode(file_get_contents(__DIR__.'/../fixtures/bitbucket_comment_created.json'), true) + )); + } + + public function testCommentCreatedWithNotMember() + { + $this->container['dispatcher']->addListener(BitbucketWebhook::EVENT_ISSUE_COMMENT, array($this, 'onCommentCreatedWithNotMember')); + + $p = new Project($this->container); + $this->assertEquals(1, $p->create(array('name' => 'foobar'))); + + $tc = new TaskCreation($this->container); + $this->assertEquals(1, $tc->create(array('title' => 'boo', 'reference' => 1, 'project_id' => 1))); + + $u = new User($this->container); + $this->assertEquals(2, $u->create(array('username' => 'fguillot'))); + + $g = new BitbucketWebhook($this->container); + $g->setProjectId(1); + + $this->assertNotFalse($g->parsePayload( + 'issue:comment_created', + json_decode(file_get_contents(__DIR__.'/../fixtures/bitbucket_comment_created.json'), true) + )); + } + + public function testCommentCreatedWithUser() + { + $this->container['dispatcher']->addListener(BitbucketWebhook::EVENT_ISSUE_COMMENT, array($this, 'onCommentCreatedWithUser')); + + $p = new Project($this->container); + $this->assertEquals(1, $p->create(array('name' => 'foobar'))); + + $tc = new TaskCreation($this->container); + $this->assertEquals(1, $tc->create(array('title' => 'boo', 'reference' => 1, 'project_id' => 1))); + + $u = new User($this->container); + $this->assertEquals(2, $u->create(array('username' => 'minicoders'))); + + $pp = new ProjectPermission($this->container); + $this->assertTrue($pp->addMember(1, 2)); + + $g = new BitbucketWebhook($this->container); + $g->setProjectId(1); + + $this->assertNotFalse($g->parsePayload( + 'issue:comment_created', + json_decode(file_get_contents(__DIR__.'/../fixtures/bitbucket_comment_created.json'), true) + )); + } + + public function testIssueClosed() + { + $this->container['dispatcher']->addListener(BitbucketWebhook::EVENT_ISSUE_CLOSED, array($this, 'onIssueClosed')); + + $p = new Project($this->container); + $this->assertEquals(1, $p->create(array('name' => 'foobar'))); + + $tc = new TaskCreation($this->container); + $this->assertEquals(1, $tc->create(array('title' => 'boo', 'reference' => 1, 'project_id' => 1))); + + $g = new BitbucketWebhook($this->container); + $g->setProjectId(1); + + $this->assertNotFalse($g->parsePayload( + 'issue:updated', + json_decode(file_get_contents(__DIR__.'/../fixtures/bitbucket_issue_closed.json'), true) + )); + } + + public function testIssueClosedWithNoTask() + { + $this->container['dispatcher']->addListener(BitbucketWebhook::EVENT_ISSUE_CLOSED, array($this, 'onIssueClosed')); + + $p = new Project($this->container); + $this->assertEquals(1, $p->create(array('name' => 'foobar'))); + + $tc = new TaskCreation($this->container); + $this->assertEquals(1, $tc->create(array('title' => 'boo', 'reference' => 42, 'project_id' => 1))); + + $g = new BitbucketWebhook($this->container); + $g->setProjectId(1); + + $this->assertFalse($g->parsePayload( + 'issue:updated', + json_decode(file_get_contents(__DIR__.'/../fixtures/bitbucket_issue_closed.json'), true) + )); + + $this->assertEmpty($this->container['dispatcher']->getCalledListeners()); + } + + public function testIssueReopened() + { + $this->container['dispatcher']->addListener(BitbucketWebhook::EVENT_ISSUE_REOPENED, array($this, 'onIssueReopened')); + + $p = new Project($this->container); + $this->assertEquals(1, $p->create(array('name' => 'foobar'))); + + $tc = new TaskCreation($this->container); + $this->assertEquals(1, $tc->create(array('title' => 'boo', 'reference' => 1, 'project_id' => 1))); + + $g = new BitbucketWebhook($this->container); + $g->setProjectId(1); + + $this->assertNotFalse($g->parsePayload( + 'issue:updated', + json_decode(file_get_contents(__DIR__.'/../fixtures/bitbucket_issue_reopened.json'), true) + )); + } + + public function testIssueReopenedWithNoTask() + { + $this->container['dispatcher']->addListener(BitbucketWebhook::EVENT_ISSUE_REOPENED, array($this, 'onIssueReopened')); + + $p = new Project($this->container); + $this->assertEquals(1, $p->create(array('name' => 'foobar'))); + + $tc = new TaskCreation($this->container); + $this->assertEquals(1, $tc->create(array('title' => 'boo', 'reference' => 42, 'project_id' => 1))); + + $g = new BitbucketWebhook($this->container); + $g->setProjectId(1); + + $this->assertFalse($g->parsePayload( + 'issue:updated', + json_decode(file_get_contents(__DIR__.'/../fixtures/bitbucket_issue_reopened.json'), true) + )); + + $this->assertEmpty($this->container['dispatcher']->getCalledListeners()); + } + + public function testIssueUnassigned() + { + $this->container['dispatcher']->addListener(BitbucketWebhook::EVENT_ISSUE_ASSIGNEE_CHANGE, array($this, 'onIssueUnassigned')); + + $p = new Project($this->container); + $this->assertEquals(1, $p->create(array('name' => 'foobar'))); + + $tc = new TaskCreation($this->container); + $this->assertEquals(1, $tc->create(array('title' => 'boo', 'reference' => 1, 'project_id' => 1))); + + $g = new BitbucketWebhook($this->container); + $g->setProjectId(1); + + $this->assertNotFalse($g->parsePayload( + 'issue:updated', + json_decode(file_get_contents(__DIR__.'/../fixtures/bitbucket_issue_unassigned.json'), true) + )); + } + + public function testIssueAssigned() + { + $this->container['dispatcher']->addListener(BitbucketWebhook::EVENT_ISSUE_ASSIGNEE_CHANGE, array($this, 'onIssueAssigned')); + + $p = new Project($this->container); + $this->assertEquals(1, $p->create(array('name' => 'foobar'))); + + $tc = new TaskCreation($this->container); + $this->assertEquals(1, $tc->create(array('title' => 'boo', 'reference' => 1, 'project_id' => 1))); + + $u = new User($this->container); + $this->assertEquals(2, $u->create(array('username' => 'minicoders'))); + + $pp = new ProjectPermission($this->container); + $this->assertTrue($pp->addMember(1, 2)); + + $g = new BitbucketWebhook($this->container); + $g->setProjectId(1); + + $this->assertNotFalse($g->parsePayload( + 'issue:updated', + json_decode(file_get_contents(__DIR__.'/../fixtures/bitbucket_issue_assigned.json'), true) + )); + + $this->assertNotEmpty($this->container['dispatcher']->getCalledListeners()); + } + + public function testIssueAssignedWithNoPermission() + { + $this->container['dispatcher']->addListener(BitbucketWebhook::EVENT_ISSUE_ASSIGNEE_CHANGE, function() {}); + + $p = new Project($this->container); + $this->assertEquals(1, $p->create(array('name' => 'foobar'))); + + $tc = new TaskCreation($this->container); + $this->assertEquals(1, $tc->create(array('title' => 'boo', 'reference' => 1, 'project_id' => 1))); + + $u = new User($this->container); + $this->assertEquals(2, $u->create(array('username' => 'minicoders'))); + + $g = new BitbucketWebhook($this->container); + $g->setProjectId(1); + + $this->assertFalse($g->parsePayload( + 'issue:updated', + json_decode(file_get_contents(__DIR__.'/../fixtures/bitbucket_issue_assigned.json'), true) + )); + + $this->assertEmpty($this->container['dispatcher']->getCalledListeners()); + } + + public function testIssueAssignedWithNoUser() + { + $this->container['dispatcher']->addListener(BitbucketWebhook::EVENT_ISSUE_ASSIGNEE_CHANGE, function() {}); + + $p = new Project($this->container); + $this->assertEquals(1, $p->create(array('name' => 'foobar'))); + + $tc = new TaskCreation($this->container); + $this->assertEquals(1, $tc->create(array('title' => 'boo', 'reference' => 1, 'project_id' => 1))); + + $g = new BitbucketWebhook($this->container); + $g->setProjectId(1); + + $this->assertFalse($g->parsePayload( + 'issue:updated', + json_decode(file_get_contents(__DIR__.'/../fixtures/bitbucket_issue_assigned.json'), true) + )); + + $this->assertEmpty($this->container['dispatcher']->getCalledListeners()); + } + + public function testIssueAssignedWithNoTask() + { + $this->container['dispatcher']->addListener(BitbucketWebhook::EVENT_ISSUE_ASSIGNEE_CHANGE, function() {}); + + $p = new Project($this->container); + $this->assertEquals(1, $p->create(array('name' => 'foobar'))); + + $tc = new TaskCreation($this->container); + $this->assertEquals(1, $tc->create(array('title' => 'boo', 'reference' => 43, 'project_id' => 1))); + + $g = new BitbucketWebhook($this->container); + $g->setProjectId(1); + + $this->assertFalse($g->parsePayload( + 'issue:updated', + json_decode(file_get_contents(__DIR__.'/../fixtures/bitbucket_issue_assigned.json'), true) + )); + + $this->assertEmpty($this->container['dispatcher']->getCalledListeners()); + } + + public function onCommit($event) + { + $data = $event->getAll(); + $this->assertEquals(1, $data['project_id']); + $this->assertEquals(2, $data['task_id']); + $this->assertEquals('test2', $data['title']); + $this->assertEquals("Test another commit #2\n\n\n[Commit made by @Frederic Guillot on Bitbucket](https://bitbucket.org/minicoders/test-webhook/commits/824059cce7667d3f8d8780cc707391be821e0ea6)", $data['commit_comment']); + $this->assertEquals("Test another commit #2\n", $data['commit_message']); + $this->assertEquals('https://bitbucket.org/minicoders/test-webhook/commits/824059cce7667d3f8d8780cc707391be821e0ea6', $data['commit_url']); + } + + public function onIssueOpened($event) + { + $data = $event->getAll(); + $this->assertEquals(1, $data['project_id']); + $this->assertEquals(1, $data['reference']); + $this->assertEquals('My new issue', $data['title']); + $this->assertEquals("**test**\n\n[Bitbucket Issue](https://bitbucket.org/minicoders/test-webhook/issue/1/my-new-issue)", $data['description']); + } + + public function onCommentCreatedWithNoUser($event) + { + $data = $event->getAll(); + $this->assertEquals(1, $data['project_id']); + $this->assertEquals(1, $data['task_id']); + $this->assertEquals(0, $data['user_id']); + $this->assertEquals(19176252, $data['reference']); + $this->assertEquals("1. step1\n2. step2\n\n[By @Frederic Guillot on Bitbucket](https://bitbucket.org/minicoders/test-webhook/issue/1#comment-19176252)", $data['comment']); + } + + public function onCommentCreatedWithNotMember($event) + { + $data = $event->getAll(); + $this->assertEquals(1, $data['project_id']); + $this->assertEquals(1, $data['task_id']); + $this->assertEquals(0, $data['user_id']); + $this->assertEquals(19176252, $data['reference']); + $this->assertEquals("1. step1\n2. step2\n\n[By @Frederic Guillot on Bitbucket](https://bitbucket.org/minicoders/test-webhook/issue/1#comment-19176252)", $data['comment']); + } + + public function onCommentCreatedWithUser($event) + { + $data = $event->getAll(); + $this->assertEquals(1, $data['project_id']); + $this->assertEquals(1, $data['task_id']); + $this->assertEquals(2, $data['user_id']); + $this->assertEquals(19176252, $data['reference']); + $this->assertEquals("1. step1\n2. step2\n\n[By @Frederic Guillot on Bitbucket](https://bitbucket.org/minicoders/test-webhook/issue/1#comment-19176252)", $data['comment']); + } + + public function onIssueClosed($event) + { + $data = $event->getAll(); + $this->assertEquals(1, $data['project_id']); + $this->assertEquals(1, $data['task_id']); + $this->assertEquals(1, $data['reference']); + } + + public function onIssueReopened($event) + { + $data = $event->getAll(); + $this->assertEquals(1, $data['project_id']); + $this->assertEquals(1, $data['task_id']); + $this->assertEquals(1, $data['reference']); + } + + public function onIssueAssigned($event) + { + $data = $event->getAll(); + $this->assertEquals(1, $data['project_id']); + $this->assertEquals(1, $data['task_id']); + $this->assertEquals(1, $data['reference']); + $this->assertEquals(2, $data['owner_id']); + } + + public function onIssueUnassigned($event) + { + $data = $event->getAll(); + $this->assertEquals(1, $data['project_id']); + $this->assertEquals(1, $data['task_id']); + $this->assertEquals(1, $data['reference']); + $this->assertEquals(0, $data['owner_id']); + } +} diff --git a/tests/units/Integration/GithubWebhookTest.php b/tests/units/Integration/GithubWebhookTest.php new file mode 100644 index 00000000..3b67ad1c --- /dev/null +++ b/tests/units/Integration/GithubWebhookTest.php @@ -0,0 +1,459 @@ +<?php + +require_once __DIR__.'/../Base.php'; + +use Integration\GithubWebhook; +use Model\TaskCreation; +use Model\TaskFinder; +use Model\Project; +use Model\ProjectPermission; +use Model\User; + +class GithubWebhookTest extends Base +{ + public function testIssueOpened() + { + $this->container['dispatcher']->addListener(GithubWebhook::EVENT_ISSUE_OPENED, array($this, 'onIssueOpened')); + + $p = new Project($this->container); + $this->assertEquals(1, $p->create(array('name' => 'foobar'))); + + $g = new GithubWebhook($this->container); + $g->setProjectId(1); + + $this->assertNotFalse($g->parsePayload( + 'issues', + json_decode(file_get_contents(__DIR__.'/../fixtures/github_issue_opened.json'), true) + )); + } + + public function testIssueAssigned() + { + $this->container['dispatcher']->addListener(GithubWebhook::EVENT_ISSUE_ASSIGNEE_CHANGE, array($this, 'onIssueAssigned')); + + $p = new Project($this->container); + $this->assertEquals(1, $p->create(array('name' => 'foobar'))); + + $tc = new TaskCreation($this->container); + $this->assertEquals(1, $tc->create(array('title' => 'boo', 'reference' => 3, 'project_id' => 1))); + + $u = new User($this->container); + $this->assertEquals(2, $u->create(array('username' => 'fguillot'))); + + $pp = new ProjectPermission($this->container); + $this->assertTrue($pp->addMember(1, 2)); + + $g = new GithubWebhook($this->container); + $g->setProjectId(1); + + $this->assertNotFalse($g->parsePayload( + 'issues', + json_decode(file_get_contents(__DIR__.'/../fixtures/github_issue_assigned.json'), true) + )); + } + + public function testIssueAssignedWithNoExistingTask() + { + $p = new Project($this->container); + $this->assertEquals(1, $p->create(array('name' => 'foobar'))); + + $g = new GithubWebhook($this->container); + $g->setProjectId(1); + + $payload = json_decode(file_get_contents(__DIR__.'/../fixtures/github_issue_assigned.json'), true); + + $this->assertFalse($g->handleIssueAssigned($payload['issue'])); + } + + public function testIssueAssignedWithNoExistingUser() + { + $p = new Project($this->container); + $this->assertEquals(1, $p->create(array('name' => 'foobar'))); + + $tc = new TaskCreation($this->container); + $this->assertEquals(1, $tc->create(array('title' => 'boo', 'reference' => 3, 'project_id' => 1))); + + $g = new GithubWebhook($this->container); + $g->setProjectId(1); + + $payload = json_decode(file_get_contents(__DIR__.'/../fixtures/github_issue_assigned.json'), true); + + $this->assertFalse($g->handleIssueAssigned($payload['issue'])); + } + + public function testIssueAssignedWithNoMember() + { + $p = new Project($this->container); + $this->assertEquals(1, $p->create(array('name' => 'foobar'))); + + $tc = new TaskCreation($this->container); + $this->assertEquals(1, $tc->create(array('title' => 'boo', 'reference' => 3, 'project_id' => 1))); + + $u = new User($this->container); + $this->assertEquals(2, $u->create(array('username' => 'fguillot'))); + + $g = new GithubWebhook($this->container); + $g->setProjectId(1); + + $payload = json_decode(file_get_contents(__DIR__.'/../fixtures/github_issue_assigned.json'), true); + + $this->assertFalse($g->handleIssueAssigned($payload['issue'])); + } + + public function testIssueAssignedWithMember() + { + $p = new Project($this->container); + $this->assertEquals(1, $p->create(array('name' => 'foobar'))); + + $tc = new TaskCreation($this->container); + $this->assertEquals(1, $tc->create(array('title' => 'boo', 'reference' => 3, 'project_id' => 1))); + + $u = new User($this->container); + $this->assertEquals(2, $u->create(array('username' => 'fguillot'))); + + $pp = new ProjectPermission($this->container); + $this->assertTrue($pp->addMember(1, 2)); + + $g = new GithubWebhook($this->container); + $g->setProjectId(1); + + $payload = json_decode(file_get_contents(__DIR__.'/../fixtures/github_issue_assigned.json'), true); + + $this->assertTrue($g->handleIssueAssigned($payload['issue'])); + } + + public function testIssueUnassigned() + { + $this->container['dispatcher']->addListener(GithubWebhook::EVENT_ISSUE_ASSIGNEE_CHANGE, array($this, 'onIssueUnassigned')); + + $p = new Project($this->container); + $this->assertEquals(1, $p->create(array('name' => 'foobar'))); + + $tc = new TaskCreation($this->container); + $this->assertEquals(1, $tc->create(array('title' => 'boo', 'reference' => 3, 'project_id' => 1))); + + $g = new GithubWebhook($this->container); + $g->setProjectId(1); + + $this->assertNotFalse($g->parsePayload( + 'issues', + json_decode(file_get_contents(__DIR__.'/../fixtures/github_issue_unassigned.json'), true) + )); + } + + public function testIssueClosed() + { + $this->container['dispatcher']->addListener(GithubWebhook::EVENT_ISSUE_CLOSED, array($this, 'onIssueClosed')); + + $p = new Project($this->container); + $this->assertEquals(1, $p->create(array('name' => 'foobar'))); + + $tc = new TaskCreation($this->container); + $this->assertEquals(1, $tc->create(array('title' => 'boo', 'reference' => 3, 'project_id' => 1))); + + $g = new GithubWebhook($this->container); + $g->setProjectId(1); + + $this->assertNotFalse($g->parsePayload( + 'issues', + json_decode(file_get_contents(__DIR__.'/../fixtures/github_issue_closed.json'), true) + )); + } + + public function testIssueClosedWithTaskNotFound() + { + $p = new Project($this->container); + $this->assertEquals(1, $p->create(array('name' => 'foobar'))); + + $g = new GithubWebhook($this->container); + $g->setProjectId(1); + + $payload = json_decode(file_get_contents(__DIR__.'/../fixtures/github_issue_closed.json'), true); + + $this->assertFalse($g->handleIssueClosed($payload['issue'])); + } + + public function testIssueReopened() + { + $this->container['dispatcher']->addListener(GithubWebhook::EVENT_ISSUE_REOPENED, array($this, 'onIssueReopened')); + + $p = new Project($this->container); + $this->assertEquals(1, $p->create(array('name' => 'foobar'))); + + $tc = new TaskCreation($this->container); + $this->assertEquals(1, $tc->create(array('title' => 'boo', 'reference' => 3, 'project_id' => 1))); + + $g = new GithubWebhook($this->container); + $g->setProjectId(1); + + $this->assertNotFalse($g->parsePayload( + 'issues', + json_decode(file_get_contents(__DIR__.'/../fixtures/github_issue_reopened.json'), true) + )); + } + + public function testIssueReopenedWithTaskNotFound() + { + $p = new Project($this->container); + $this->assertEquals(1, $p->create(array('name' => 'foobar'))); + + $g = new GithubWebhook($this->container); + $g->setProjectId(1); + + $payload = json_decode(file_get_contents(__DIR__.'/../fixtures/github_issue_reopened.json'), true); + + $this->assertFalse($g->handleIssueReopened($payload['issue'])); + } + + public function testIssueLabeled() + { + $this->container['dispatcher']->addListener(GithubWebhook::EVENT_ISSUE_LABEL_CHANGE, array($this, 'onIssueLabeled')); + + $p = new Project($this->container); + $this->assertEquals(1, $p->create(array('name' => 'foobar'))); + + $tc = new TaskCreation($this->container); + $this->assertEquals(1, $tc->create(array('title' => 'boo', 'reference' => 3, 'project_id' => 1))); + + $g = new GithubWebhook($this->container); + $g->setProjectId(1); + + $this->assertNotFalse($g->parsePayload( + 'issues', + json_decode(file_get_contents(__DIR__.'/../fixtures/github_issue_labeled.json'), true) + )); + } + + public function testIssueLabeledWithTaskNotFound() + { + $p = new Project($this->container); + $this->assertEquals(1, $p->create(array('name' => 'foobar'))); + + $g = new GithubWebhook($this->container); + $g->setProjectId(1); + + $payload = json_decode(file_get_contents(__DIR__.'/../fixtures/github_issue_labeled.json'), true); + + $this->assertFalse($g->handleIssueLabeled($payload['issue'], $payload['label'])); + } + + public function testIssueUnLabeled() + { + $this->container['dispatcher']->addListener(GithubWebhook::EVENT_ISSUE_LABEL_CHANGE, array($this, 'onIssueUnlabeled')); + + $p = new Project($this->container); + $this->assertEquals(1, $p->create(array('name' => 'foobar'))); + + $tc = new TaskCreation($this->container); + $this->assertEquals(1, $tc->create(array('title' => 'boo', 'reference' => 3, 'project_id' => 1))); + + $g = new GithubWebhook($this->container); + $g->setProjectId(1); + + $this->assertNotFalse($g->parsePayload( + 'issues', + json_decode(file_get_contents(__DIR__.'/../fixtures/github_issue_unlabeled.json'), true) + )); + } + + public function testIssueUnLabeledWithTaskNotFound() + { + $p = new Project($this->container); + $this->assertEquals(1, $p->create(array('name' => 'foobar'))); + + $g = new GithubWebhook($this->container); + $g->setProjectId(1); + + $payload = json_decode(file_get_contents(__DIR__.'/../fixtures/github_issue_unlabeled.json'), true); + + $this->assertFalse($g->handleIssueUnlabeled($payload['issue'], $payload['label'])); + } + + public function testCommentCreatedWithNoUser() + { + $this->container['dispatcher']->addListener(GithubWebhook::EVENT_ISSUE_COMMENT, array($this, 'onCommentCreatedWithNoUser')); + + $p = new Project($this->container); + $this->assertEquals(1, $p->create(array('name' => 'foobar'))); + + $tc = new TaskCreation($this->container); + $this->assertEquals(1, $tc->create(array('title' => 'boo', 'reference' => 3, 'project_id' => 1))); + + $g = new GithubWebhook($this->container); + $g->setProjectId(1); + + $this->assertNotFalse($g->parsePayload( + 'issue_comment', + json_decode(file_get_contents(__DIR__.'/../fixtures/github_comment_created.json'), true) + )); + } + + public function testCommentCreatedWithNotMember() + { + $this->container['dispatcher']->addListener(GithubWebhook::EVENT_ISSUE_COMMENT, array($this, 'onCommentCreatedWithNotMember')); + + $p = new Project($this->container); + $this->assertEquals(1, $p->create(array('name' => 'foobar'))); + + $tc = new TaskCreation($this->container); + $this->assertEquals(1, $tc->create(array('title' => 'boo', 'reference' => 3, 'project_id' => 1))); + + $u = new User($this->container); + $this->assertEquals(2, $u->create(array('username' => 'fguillot'))); + + $g = new GithubWebhook($this->container); + $g->setProjectId(1); + + $this->assertNotFalse($g->parsePayload( + 'issue_comment', + json_decode(file_get_contents(__DIR__.'/../fixtures/github_comment_created.json'), true) + )); + } + + public function testCommentCreatedWithUser() + { + $this->container['dispatcher']->addListener(GithubWebhook::EVENT_ISSUE_COMMENT, array($this, 'onCommentCreatedWithUser')); + + $p = new Project($this->container); + $this->assertEquals(1, $p->create(array('name' => 'foobar'))); + + $tc = new TaskCreation($this->container); + $this->assertEquals(1, $tc->create(array('title' => 'boo', 'reference' => 3, 'project_id' => 1))); + + $u = new User($this->container); + $this->assertEquals(2, $u->create(array('username' => 'fguillot'))); + + $pp = new ProjectPermission($this->container); + $this->assertTrue($pp->addMember(1, 2)); + + $g = new GithubWebhook($this->container); + $g->setProjectId(1); + + $this->assertNotFalse($g->parsePayload( + 'issue_comment', + json_decode(file_get_contents(__DIR__.'/../fixtures/github_comment_created.json'), true) + )); + } + + public function testPush() + { + $this->container['dispatcher']->addListener(GithubWebhook::EVENT_COMMIT, array($this, 'onPush')); + + $p = new Project($this->container); + $this->assertEquals(1, $p->create(array('name' => 'foobar'))); + + $tc = new TaskCreation($this->container); + $this->assertEquals(1, $tc->create(array('title' => 'boo', 'project_id' => 1))); + + $g = new GithubWebhook($this->container); + $g->setProjectId(1); + + $this->assertNotFalse($g->parsePayload( + 'push', + json_decode(file_get_contents(__DIR__.'/../fixtures/github_push.json'), true) + )); + } + + public function onIssueOpened($event) + { + $data = $event->getAll(); + $this->assertEquals(1, $data['project_id']); + $this->assertEquals(3, $data['reference']); + $this->assertEquals('Test Webhook', $data['title']); + $this->assertEquals("plop\n\n[Github Issue](https://github.com/kanboardapp/webhook/issues/3)", $data['description']); + } + + public function onIssueAssigned($event) + { + $data = $event->getAll(); + $this->assertEquals(1, $data['project_id']); + $this->assertEquals(1, $data['task_id']); + $this->assertEquals(3, $data['reference']); + $this->assertEquals(2, $data['owner_id']); + } + + public function onIssueUnassigned($event) + { + $data = $event->getAll(); + $this->assertEquals(1, $data['project_id']); + $this->assertEquals(1, $data['task_id']); + $this->assertEquals(3, $data['reference']); + $this->assertEquals(0, $data['owner_id']); + } + + public function onIssueClosed($event) + { + $data = $event->getAll(); + $this->assertEquals(1, $data['project_id']); + $this->assertEquals(1, $data['task_id']); + $this->assertEquals(3, $data['reference']); + } + + public function onIssueReopened($event) + { + $data = $event->getAll(); + $this->assertEquals(1, $data['project_id']); + $this->assertEquals(1, $data['task_id']); + $this->assertEquals(3, $data['reference']); + } + + public function onIssueLabeled($event) + { + $data = $event->getAll(); + $this->assertEquals(1, $data['project_id']); + $this->assertEquals(1, $data['task_id']); + $this->assertEquals(3, $data['reference']); + $this->assertEquals('bug', $data['label']); + } + + public function onIssueUnlabeled($event) + { + $data = $event->getAll(); + $this->assertEquals(1, $data['project_id']); + $this->assertEquals(1, $data['task_id']); + $this->assertEquals(3, $data['reference']); + $this->assertEquals('bug', $data['label']); + $this->assertEquals(0, $data['category_id']); + } + + public function onCommentCreatedWithNoUser($event) + { + $data = $event->getAll(); + $this->assertEquals(1, $data['project_id']); + $this->assertEquals(1, $data['task_id']); + $this->assertEquals(0, $data['user_id']); + $this->assertEquals(113834672, $data['reference']); + $this->assertEquals("test\n\n[By @fguillot on Github](https://github.com/kanboardapp/webhook/issues/3#issuecomment-113834672)", $data['comment']); + } + + public function onCommentCreatedWithNotMember($event) + { + $data = $event->getAll(); + $this->assertEquals(1, $data['project_id']); + $this->assertEquals(1, $data['task_id']); + $this->assertEquals(0, $data['user_id']); + $this->assertEquals(113834672, $data['reference']); + $this->assertEquals("test\n\n[By @fguillot on Github](https://github.com/kanboardapp/webhook/issues/3#issuecomment-113834672)", $data['comment']); + } + + public function onCommentCreatedWithUser($event) + { + $data = $event->getAll(); + $this->assertEquals(1, $data['project_id']); + $this->assertEquals(1, $data['task_id']); + $this->assertEquals(2, $data['user_id']); + $this->assertEquals(113834672, $data['reference']); + $this->assertEquals("test\n\n[By @fguillot on Github](https://github.com/kanboardapp/webhook/issues/3#issuecomment-113834672)", $data['comment']); + } + + public function onPush($event) + { + $data = $event->getAll(); + $this->assertEquals(1, $data['project_id']); + $this->assertEquals(1, $data['task_id']); + $this->assertEquals('boo', $data['title']); + $this->assertEquals("Update README to fix #1\n\n[Commit made by @fguillot on Github](https://github.com/kanboardapp/webhook/commit/98dee3e49ee7aa66ffec1f761af93da5ffd711f6)", $data['commit_comment']); + $this->assertEquals('Update README to fix #1', $data['commit_message']); + $this->assertEquals('https://github.com/kanboardapp/webhook/commit/98dee3e49ee7aa66ffec1f761af93da5ffd711f6', $data['commit_url']); + } +} diff --git a/tests/units/Integration/GitlabWebhookTest.php b/tests/units/Integration/GitlabWebhookTest.php new file mode 100644 index 00000000..ec073fee --- /dev/null +++ b/tests/units/Integration/GitlabWebhookTest.php @@ -0,0 +1,221 @@ +<?php + +require_once __DIR__.'/../Base.php'; + +use Integration\GitlabWebhook; +use Model\TaskCreation; +use Model\TaskFinder; +use Model\Project; +use Model\ProjectPermission; +use Model\User; + +class GitlabWebhookTest extends Base +{ + public function testGetEventType() + { + $g = new GitlabWebhook($this->container); + + $this->assertEquals(GitlabWebhook::TYPE_PUSH, $g->getType(json_decode(file_get_contents(__DIR__.'/../fixtures/gitlab_push.json'), true))); + $this->assertEquals(GitlabWebhook::TYPE_ISSUE, $g->getType(json_decode(file_get_contents(__DIR__.'/../fixtures/gitlab_issue_opened.json'), true))); + $this->assertEquals(GitlabWebhook::TYPE_COMMENT, $g->getType(json_decode(file_get_contents(__DIR__.'/../fixtures/gitlab_comment_created.json'), true))); + $this->assertEquals('', $g->getType(array())); + } + + public function testHandleCommit() + { + $g = new GitlabWebhook($this->container); + $p = new Project($this->container); + $tc = new TaskCreation($this->container); + $tf = new TaskFinder($this->container); + + $this->assertEquals(1, $p->create(array('name' => 'test'))); + $g->setProjectId(1); + + $this->container['dispatcher']->addListener(GitlabWebhook::EVENT_COMMIT, array($this, 'onCommit')); + + $event = json_decode(file_get_contents(__DIR__.'/../fixtures/gitlab_push.json'), true); + + // No task + $this->assertFalse($g->handleCommit($event['commits'][0])); + + // Create task with the wrong id + $this->assertEquals(1, $tc->create(array('title' => 'test1', 'project_id' => 1))); + $this->assertFalse($g->handleCommit($event['commits'][0])); + + // Create task with the right id + $this->assertEquals(2, $tc->create(array('title' => 'test2', 'project_id' => 1))); + $this->assertTrue($g->handleCommit($event['commits'][0])); + + $called = $this->container['dispatcher']->getCalledListeners(); + $this->assertArrayHasKey(GitlabWebhook::EVENT_COMMIT.'.GitlabWebhookTest::onCommit', $called); + } + + public function testHandleIssueOpened() + { + $g = new GitlabWebhook($this->container); + $g->setProjectId(1); + + $this->container['dispatcher']->addListener(GitlabWebhook::EVENT_ISSUE_OPENED, array($this, 'onOpen')); + + $event = json_decode(file_get_contents(__DIR__.'/../fixtures/gitlab_issue_opened.json'), true); + $this->assertTrue($g->handleIssueOpened($event['object_attributes'])); + + $called = $this->container['dispatcher']->getCalledListeners(); + $this->assertArrayHasKey(GitlabWebhook::EVENT_ISSUE_OPENED.'.GitlabWebhookTest::onOpen', $called); + } + + public function testHandleIssueClosed() + { + $g = new GitlabWebhook($this->container); + $p = new Project($this->container); + $tc = new TaskCreation($this->container); + $tf = new TaskFinder($this->container); + + $this->assertEquals(1, $p->create(array('name' => 'test'))); + $g->setProjectId(1); + + $this->container['dispatcher']->addListener(GitlabWebhook::EVENT_ISSUE_CLOSED, array($this, 'onClose')); + + $event = json_decode(file_get_contents(__DIR__.'/../fixtures/gitlab_issue_closed.json'), true); + + // Issue not there + $this->assertFalse($g->handleIssueClosed($event['object_attributes'])); + + $called = $this->container['dispatcher']->getCalledListeners(); + $this->assertEmpty($called); + + // Create a task with the issue reference + $this->assertEquals(1, $tc->create(array('title' => 'A', 'project_id' => 1, 'reference' => 355691))); + $task = $tf->getByReference(1, 355691); + $this->assertNotEmpty($task); + + $task = $tf->getByReference(2, 355691); + $this->assertEmpty($task); + + $this->assertTrue($g->handleIssueClosed($event['object_attributes'])); + + $called = $this->container['dispatcher']->getCalledListeners(); + $this->assertArrayHasKey(GitlabWebhook::EVENT_ISSUE_CLOSED.'.GitlabWebhookTest::onClose', $called); + } + + public function testCommentCreatedWithNoUser() + { + $this->container['dispatcher']->addListener(GitlabWebhook::EVENT_ISSUE_COMMENT, array($this, 'onCommentCreatedWithNoUser')); + + $p = new Project($this->container); + $this->assertEquals(1, $p->create(array('name' => 'foobar'))); + + $tc = new TaskCreation($this->container); + $this->assertEquals(1, $tc->create(array('title' => 'boo', 'reference' => 355691, 'project_id' => 1))); + + $g = new GitlabWebhook($this->container); + $g->setProjectId(1); + + $this->assertNotFalse($g->parsePayload( + json_decode(file_get_contents(__DIR__.'/../fixtures/gitlab_comment_created.json'), true) + )); + } + + public function testCommentCreatedWithNotMember() + { + $this->container['dispatcher']->addListener(GitlabWebhook::EVENT_ISSUE_COMMENT, array($this, 'onCommentCreatedWithNotMember')); + + $p = new Project($this->container); + $this->assertEquals(1, $p->create(array('name' => 'foobar'))); + + $tc = new TaskCreation($this->container); + $this->assertEquals(1, $tc->create(array('title' => 'boo', 'reference' => 355691, 'project_id' => 1))); + + $u = new User($this->container); + $this->assertEquals(2, $u->create(array('username' => 'minicoders'))); + + $g = new GitlabWebhook($this->container); + $g->setProjectId(1); + + $this->assertNotFalse($g->parsePayload( + json_decode(file_get_contents(__DIR__.'/../fixtures/gitlab_comment_created.json'), true) + )); + } + + public function testCommentCreatedWithUser() + { + $this->container['dispatcher']->addListener(GitlabWebhook::EVENT_ISSUE_COMMENT, array($this, 'onCommentCreatedWithUser')); + + $p = new Project($this->container); + $this->assertEquals(1, $p->create(array('name' => 'foobar'))); + + $tc = new TaskCreation($this->container); + $this->assertEquals(1, $tc->create(array('title' => 'boo', 'reference' => 355691, 'project_id' => 1))); + + $u = new User($this->container); + $this->assertEquals(2, $u->create(array('username' => 'minicoders'))); + + $pp = new ProjectPermission($this->container); + $this->assertTrue($pp->addMember(1, 2)); + + $g = new GitlabWebhook($this->container); + $g->setProjectId(1); + + $this->assertNotFalse($g->parsePayload( + json_decode(file_get_contents(__DIR__.'/../fixtures/gitlab_comment_created.json'), true) + )); + } + + public function onOpen($event) + { + $data = $event->getAll(); + $this->assertEquals(1, $data['project_id']); + $this->assertEquals(355691, $data['reference']); + $this->assertEquals('Bug', $data['title']); + $this->assertEquals("There is a bug somewhere.\r\n\r\nBye\n\n[Gitlab Issue](https://gitlab.com/minicoders/test-webhook/issues/1)", $data['description']); + } + + public function onClose($event) + { + $data = $event->getAll(); + $this->assertEquals(1, $data['project_id']); + $this->assertEquals(1, $data['task_id']); + $this->assertEquals(355691, $data['reference']); + } + + public function onCommit($event) + { + $data = $event->getAll(); + $this->assertEquals(1, $data['project_id']); + $this->assertEquals(2, $data['task_id']); + $this->assertEquals('test2', $data['title']); + $this->assertEquals("Fix bug #2\n\n[Commit made by @Fred on Gitlab](https://gitlab.com/minicoders/test-webhook/commit/48aafa75eef9ad253aa254b0c82c987a52ebea78)", $data['commit_comment']); + $this->assertEquals("Fix bug #2", $data['commit_message']); + $this->assertEquals('https://gitlab.com/minicoders/test-webhook/commit/48aafa75eef9ad253aa254b0c82c987a52ebea78', $data['commit_url']); + } + + public function onCommentCreatedWithNoUser($event) + { + $data = $event->getAll(); + $this->assertEquals(1, $data['project_id']); + $this->assertEquals(1, $data['task_id']); + $this->assertEquals(0, $data['user_id']); + $this->assertEquals(1642761, $data['reference']); + $this->assertEquals("Super comment!\n\n[By @minicoders on Gitlab](https://gitlab.com/minicoders/test-webhook/issues/1#note_1642761)", $data['comment']); + } + + public function onCommentCreatedWithNotMember($event) + { + $data = $event->getAll(); + $this->assertEquals(1, $data['project_id']); + $this->assertEquals(1, $data['task_id']); + $this->assertEquals(0, $data['user_id']); + $this->assertEquals(1642761, $data['reference']); + $this->assertEquals("Super comment!\n\n[By @minicoders on Gitlab](https://gitlab.com/minicoders/test-webhook/issues/1#note_1642761)", $data['comment']); + } + + public function onCommentCreatedWithUser($event) + { + $data = $event->getAll(); + $this->assertEquals(1, $data['project_id']); + $this->assertEquals(1, $data['task_id']); + $this->assertEquals(2, $data['user_id']); + $this->assertEquals(1642761, $data['reference']); + $this->assertEquals("Super comment!\n\n[By @minicoders on Gitlab](https://gitlab.com/minicoders/test-webhook/issues/1#note_1642761)", $data['comment']); + } +} diff --git a/tests/units/Integration/MailgunTest.php b/tests/units/Integration/MailgunTest.php new file mode 100644 index 00000000..67914b0a --- /dev/null +++ b/tests/units/Integration/MailgunTest.php @@ -0,0 +1,71 @@ +<?php + +require_once __DIR__.'/../Base.php'; + +use Integration\Mailgun; +use Model\TaskCreation; +use Model\TaskFinder; +use Model\Project; +use Model\ProjectPermission; +use Model\User; + +class MailgunTest extends Base +{ + public function testSendEmail() + { + $pm = new Mailgun($this->container); + $pm->sendEmail('test@localhost', 'Me', 'Test', 'Content', 'Bob'); + + $this->assertStringStartsWith('https://api.mailgun.net/v3/', $this->container['httpClient']->getUrl()); + + $data = $this->container['httpClient']->getData(); + + $this->assertArrayHasKey('from', $data); + $this->assertArrayHasKey('to', $data); + $this->assertArrayHasKey('subject', $data); + $this->assertArrayHasKey('html', $data); + + $this->assertEquals('Me <test@localhost>', $data['to']); + $this->assertEquals('Bob <notifications@kanboard.local>', $data['from']); + $this->assertEquals('Test', $data['subject']); + $this->assertEquals('Content', $data['html']); + } + + public function testHandlePayload() + { + $w = new Mailgun($this->container); + $p = new Project($this->container); + $pp = new ProjectPermission($this->container); + $u = new User($this->container); + $tc = new TaskCreation($this->container); + $tf = new TaskFinder($this->container); + + $this->assertEquals(2, $u->create(array('username' => 'me', 'email' => 'me@localhost'))); + + $this->assertEquals(1, $p->create(array('name' => 'test1'))); + $this->assertEquals(2, $p->create(array('name' => 'test2', 'identifier' => 'TEST1'))); + + // Empty payload + $this->assertFalse($w->receiveEmail(array())); + + // Unknown user + $this->assertFalse($w->receiveEmail(array('sender' => 'a@b.c', 'subject' => 'Email task', 'recipient' => 'foobar', 'stripped-text' => 'boo'))); + + // Project not found + $this->assertFalse($w->receiveEmail(array('sender' => 'me@localhost', 'subject' => 'Email task', 'recipient' => 'foo+test@localhost', 'stripped-text' => 'boo'))); + + // User is not member + $this->assertFalse($w->receiveEmail(array('sender' => 'me@localhost', 'subject' => 'Email task', 'recipient' => 'foo+test1@localhost', 'stripped-text' => 'boo'))); + $this->assertTrue($pp->addMember(2, 2)); + + // The task must be created + $this->assertTrue($w->receiveEmail(array('sender' => 'me@localhost', 'subject' => 'Email task', 'recipient' => 'foo+test1@localhost', 'stripped-text' => 'boo'))); + + $task = $tf->getById(1); + $this->assertNotEmpty($task); + $this->assertEquals(2, $task['project_id']); + $this->assertEquals('Email task', $task['title']); + $this->assertEquals('boo', $task['description']); + $this->assertEquals(2, $task['creator_id']); + } +} diff --git a/tests/units/Integration/PostmarkTest.php b/tests/units/Integration/PostmarkTest.php new file mode 100644 index 00000000..9115e24a --- /dev/null +++ b/tests/units/Integration/PostmarkTest.php @@ -0,0 +1,106 @@ +<?php + +require_once __DIR__.'/../Base.php'; + +use Integration\Postmark; +use Model\TaskCreation; +use Model\TaskFinder; +use Model\Project; +use Model\ProjectPermission; +use Model\User; + +class PostmarkTest extends Base +{ + public function testSendEmail() + { + $pm = new Postmark($this->container); + $pm->sendEmail('test@localhost', 'Me', 'Test', 'Content', 'Bob'); + + $this->assertEquals('https://api.postmarkapp.com/email', $this->container['httpClient']->getUrl()); + + $data = $this->container['httpClient']->getData(); + + $this->assertArrayHasKey('From', $data); + $this->assertArrayHasKey('To', $data); + $this->assertArrayHasKey('Subject', $data); + $this->assertArrayHasKey('HtmlBody', $data); + + $this->assertEquals('Me <test@localhost>', $data['To']); + $this->assertEquals('Bob <notifications@kanboard.local>', $data['From']); + $this->assertEquals('Test', $data['Subject']); + $this->assertEquals('Content', $data['HtmlBody']); + + $this->assertContains('Accept: application/json', $this->container['httpClient']->getHeaders()); + $this->assertContains('X-Postmark-Server-Token: ', $this->container['httpClient']->getHeaders()); + } + + public function testHandlePayload() + { + $w = new Postmark($this->container); + $p = new Project($this->container); + $pp = new ProjectPermission($this->container); + $u = new User($this->container); + $tc = new TaskCreation($this->container); + $tf = new TaskFinder($this->container); + + $this->assertEquals(2, $u->create(array('username' => 'me', 'email' => 'me@localhost'))); + + $this->assertEquals(1, $p->create(array('name' => 'test1'))); + $this->assertEquals(2, $p->create(array('name' => 'test2', 'identifier' => 'TEST1'))); + + // Empty payload + $this->assertFalse($w->receiveEmail(array())); + + // Unknown user + $this->assertFalse($w->receiveEmail(array('From' => 'a@b.c', 'Subject' => 'Email task', 'MailboxHash' => 'foobar', 'TextBody' => 'boo'))); + + // Project not found + $this->assertFalse($w->receiveEmail(array('From' => 'me@localhost', 'Subject' => 'Email task', 'MailboxHash' => 'test', 'TextBody' => 'boo'))); + + // User is not member + $this->assertFalse($w->receiveEmail(array('From' => 'me@localhost', 'Subject' => 'Email task', 'MailboxHash' => 'test1', 'TextBody' => 'boo'))); + $this->assertTrue($pp->addMember(2, 2)); + + // The task must be created + $this->assertTrue($w->receiveEmail(array('From' => 'me@localhost', 'Subject' => 'Email task', 'MailboxHash' => 'test1', 'TextBody' => 'boo'))); + + $task = $tf->getById(1); + $this->assertNotEmpty($task); + $this->assertEquals(2, $task['project_id']); + $this->assertEquals('Email task', $task['title']); + $this->assertEquals('boo', $task['description']); + $this->assertEquals(2, $task['creator_id']); + } + + public function testHtml2Markdown() + { + $w = new Postmark($this->container); + $p = new Project($this->container); + $pp = new ProjectPermission($this->container); + $u = new User($this->container); + $tc = new TaskCreation($this->container); + $tf = new TaskFinder($this->container); + + $this->assertEquals(2, $u->create(array('username' => 'me', 'email' => 'me@localhost'))); + $this->assertEquals(1, $p->create(array('name' => 'test2', 'identifier' => 'TEST1'))); + $this->assertTrue($pp->addMember(1, 2)); + + $this->assertTrue($w->receiveEmail(array('From' => 'me@localhost', 'Subject' => 'Email task', 'MailboxHash' => 'test1', 'TextBody' => 'boo', 'HtmlBody' => '<p><strong>boo</strong></p>'))); + + $task = $tf->getById(1); + $this->assertNotEmpty($task); + $this->assertEquals(1, $task['project_id']); + $this->assertEquals('Email task', $task['title']); + $this->assertEquals('**boo**', $task['description']); + $this->assertEquals(2, $task['creator_id']); + + $this->assertTrue($w->receiveEmail(array('From' => 'me@localhost', 'Subject' => 'Email task', 'MailboxHash' => 'test1', 'TextBody' => '**boo**', 'HtmlBody' => ''))); + + $task = $tf->getById(2); + $this->assertNotEmpty($task); + $this->assertEquals(1, $task['project_id']); + $this->assertEquals('Email task', $task['title']); + $this->assertEquals('**boo**', $task['description']); + $this->assertEquals(2, $task['creator_id']); + } +} diff --git a/tests/units/Integration/SendgridTest.php b/tests/units/Integration/SendgridTest.php new file mode 100644 index 00000000..b2352076 --- /dev/null +++ b/tests/units/Integration/SendgridTest.php @@ -0,0 +1,135 @@ +<?php + +require_once __DIR__.'/../Base.php'; + +use Integration\Sendgrid; +use Model\TaskCreation; +use Model\TaskFinder; +use Model\Project; +use Model\ProjectPermission; +use Model\User; + +class SendgridTest extends Base +{ + public function testSendEmail() + { + $pm = new Sendgrid($this->container); + $pm->sendEmail('test@localhost', 'Me', 'Test', 'Content', 'Bob'); + + $this->assertEquals('https://api.sendgrid.com/api/mail.send.json', $this->container['httpClient']->getUrl()); + + $data = $this->container['httpClient']->getData(); + + $this->assertArrayHasKey('api_user', $data); + $this->assertArrayHasKey('api_key', $data); + $this->assertArrayHasKey('from', $data); + $this->assertArrayHasKey('fromname', $data); + $this->assertArrayHasKey('to', $data); + $this->assertArrayHasKey('toname', $data); + $this->assertArrayHasKey('subject', $data); + $this->assertArrayHasKey('html', $data); + + $this->assertEquals('test@localhost', $data['to']); + $this->assertEquals('Me', $data['toname']); + $this->assertEquals('notifications@kanboard.local', $data['from']); + $this->assertEquals('Bob', $data['fromname']); + $this->assertEquals('Test', $data['subject']); + $this->assertEquals('Content', $data['html']); + $this->assertEquals('', $data['api_key']); + $this->assertEquals('', $data['api_user']); + } + + public function testHandlePayload() + { + $w = new Sendgrid($this->container); + $p = new Project($this->container); + $pp = new ProjectPermission($this->container); + $u = new User($this->container); + $tc = new TaskCreation($this->container); + $tf = new TaskFinder($this->container); + + $this->assertEquals(2, $u->create(array('username' => 'me', 'email' => 'me@localhost'))); + + $this->assertEquals(1, $p->create(array('name' => 'test1'))); + $this->assertEquals(2, $p->create(array('name' => 'test2', 'identifier' => 'TEST1'))); + + // Empty payload + $this->assertFalse($w->receiveEmail(array())); + + // Unknown user + $this->assertFalse($w->receiveEmail(array( + 'envelope' => '{"to":["a@b.c"],"from":"a.b.c"}', + 'subject' => 'Email task' + ))); + + // Project not found + $this->assertFalse($w->receiveEmail(array( + 'envelope' => '{"to":["a@b.c"],"from":"me@localhost"}', + 'subject' => 'Email task' + ))); + + // User is not member + $this->assertFalse($w->receiveEmail(array( + 'envelope' => '{"to":["something+test1@localhost"],"from":"me@localhost"}', + 'subject' => 'Email task' + ))); + + $this->assertTrue($pp->addMember(2, 2)); + + // The task must be created + $this->assertTrue($w->receiveEmail(array( + 'envelope' => '{"to":["something+test1@localhost"],"from":"me@localhost"}', + 'subject' => 'Email task' + ))); + + $task = $tf->getById(1); + $this->assertNotEmpty($task); + $this->assertEquals(2, $task['project_id']); + $this->assertEquals('Email task', $task['title']); + $this->assertEquals('', $task['description']); + $this->assertEquals(2, $task['creator_id']); + + // Html content + $this->assertTrue($w->receiveEmail(array( + 'envelope' => '{"to":["something+test1@localhost"],"from":"me@localhost"}', + 'subject' => 'Email task', + 'html' => '<strong>bold</strong> text', + ))); + + $task = $tf->getById(2); + $this->assertNotEmpty($task); + $this->assertEquals(2, $task['project_id']); + $this->assertEquals('Email task', $task['title']); + $this->assertEquals('**bold** text', $task['description']); + $this->assertEquals(2, $task['creator_id']); + + // Text content + $this->assertTrue($w->receiveEmail(array( + 'envelope' => '{"to":["something+test1@localhost"],"from":"me@localhost"}', + 'subject' => 'Email task', + 'text' => '**bold** text', + ))); + + $task = $tf->getById(3); + $this->assertNotEmpty($task); + $this->assertEquals(2, $task['project_id']); + $this->assertEquals('Email task', $task['title']); + $this->assertEquals('**bold** text', $task['description']); + $this->assertEquals(2, $task['creator_id']); + + // Text + html content + $this->assertTrue($w->receiveEmail(array( + 'envelope' => '{"to":["something+test1@localhost"],"from":"me@localhost"}', + 'subject' => 'Email task', + 'html' => '<strong>bold</strong> html', + 'text' => '**bold** text', + ))); + + $task = $tf->getById(4); + $this->assertNotEmpty($task); + $this->assertEquals(2, $task['project_id']); + $this->assertEquals('Email task', $task['title']); + $this->assertEquals('**bold** html', $task['description']); + $this->assertEquals(2, $task['creator_id']); + } +} |