diff options
author | Daniel Kesselberg <mail@danielkesselberg.de> | 2021-10-12 19:33:40 +0300 |
---|---|---|
committer | Daniel Kesselberg <mail@danielkesselberg.de> | 2021-10-17 18:55:52 +0300 |
commit | 05320c75a07edbc6af0fe5534d380b58f1a9ae2b (patch) | |
tree | 5270766d5fd6f60cf5f5d446a8f275c0ccf2ac15 /tests | |
parent | 0ad54d85d74617423c86a14a3d0a22227ed03a91 (diff) |
Rework IspDb to use IClient
Signed-off-by: Daniel Kesselberg <mail@danielkesselberg.de>
Diffstat (limited to 'tests')
-rw-r--r-- | tests/Unit/Service/Autoconfig/IspDbTest.php | 114 | ||||
-rw-r--r-- | tests/resources/autoconfig-gmx.xml | 71 | ||||
-rw-r--r-- | tests/resources/autoconfig-outlook.xml | 53 | ||||
-rw-r--r-- | tests/resources/autoconfig-posteo.xml | 33 |
4 files changed, 233 insertions, 38 deletions
diff --git a/tests/Unit/Service/Autoconfig/IspDbTest.php b/tests/Unit/Service/Autoconfig/IspDbTest.php index 6c540a0ae..c1ba40827 100644 --- a/tests/Unit/Service/Autoconfig/IspDbTest.php +++ b/tests/Unit/Service/Autoconfig/IspDbTest.php @@ -25,25 +25,33 @@ declare(strict_types=1); namespace OCA\Mail\Tests\Unit\Service\Autoconfig; use ChristophWurst\Nextcloud\Testing\TestCase; +use OCA\Mail\Service\AutoConfig\IspDb; +use OCP\Http\Client\IClient; +use OCP\Http\Client\IClientService; +use OCP\Http\Client\IResponse; use PHPUnit\Framework\MockObject\MockObject; use Psr\Log\LoggerInterface; class IspDbTest extends TestCase { - /** @var MockObject|LoggerInterface */ + /** @var IClientService|MockObject */ + private $clientService; + + /** @var IClient|MockObject */ + private $client; + + /** @var LoggerInterface|MockObject */ private $logger; protected function setUp(): void { parent::setUp(); + $this->clientService = $this->createMock(IClientService::class); + $this->client = $this->createMock(IClient::class); $this->logger = $this->createMock(LoggerInterface::class); - } - public function queryData() { - return [ - ['gmail.com', 'user@gmail.com',], - ['outlook.com', 'user@outlook.com',], - ]; + $this->clientService->method('newClient') + ->willReturn($this->client); } public function fakeAutoconfigData() { @@ -53,42 +61,72 @@ class IspDbTest extends TestCase { ]; } - /** - * @dataProvider fakeAutoconfigData - */ - public function testQueryFakeAutoconfig(string $domain, string $email, bool $shouldSucceed) { - $urls = [ - dirname(__FILE__) . '/../../../resources/autoconfig-freenet.xml', - ]; - $ispDb = $this->getIspDbMock($urls); + public function testQueryGmx(): void { + $this->client->method('get') + ->willReturnCallback(function ($url) { + switch ($url) { + case 'https://autoconfig.gmx.com/mail/config-v1.1.xml?emailaddress=test@gmx.com': + case 'http://autoconfig.gmx.com/mail/config-v1.1.xml?emailaddress=test@gmx.com': + throw new \Exception('cURL error 6: Could not resolve host: autoconfig.gmx.com'); + case 'https://gmx.com/.well-known/autoconfig/mail/config-v1.1.xml?emailaddress=test@gmx.com': + case 'http://gmx.com/.well-known/autoconfig/mail/config-v1.1.xml?emailaddress=test@gmx.com': + throw new \Exception('Client error: `GET https://gmx.com/.well-known/autoconfig/mail/config-v1.1.xml?emailaddress=test@gmx.com` resulted in a `404 Not Found` response'); + case 'https://autoconfig.thunderbird.net/v1.1/gmx.com': + $response = $this->createMock(IResponse::class); + $response->method('getBody')->willReturn(file_get_contents(__DIR__ . '/../../../resources/autoconfig-gmx.xml')); + return $response; + } + }); + + $ispDb = new IspDb($this->clientService, $this->logger); - $result = $ispDb->query($domain, $email); + $providers = $ispDb->query('gmx.com', 'test@gmx.com'); - if ($shouldSucceed) { - $this->assertContainsIspData($result); - } else { - $this->assertEmpty($result); - } + $this->assertEquals('GMX Freemail', $providers['displayName']); + $this->assertCount(2, $providers['imap']); + $this->assertCount(2, $providers['smtp']); } - private function getIspDbMock($urls) { - $mock = $this->getMockBuilder('\OCA\Mail\Service\AutoConfig\IspDb') - ->setMethods(['getUrls']) - ->setConstructorArgs([$this->logger]) - ->getMock(); - $mock->expects($this->once()) - ->method('getUrls') - ->will($this->returnValue($urls)); - return $mock; + public function testQueryOutlook(): void { + $this->client->method('get') + ->willReturnCallback(function ($url) { + switch ($url) { + case 'https://autoconfig.outlook.com/mail/config-v1.1.xml?emailaddress=test@outlook.com': + case 'http://autoconfig.outlook.com/mail/config-v1.1.xml?emailaddress=test@outlook.com': + throw new \Exception('cURL error 6: Could not resolve host: autoconfig.outlook.com'); + case 'https://outlook.com/.well-known/autoconfig/mail/config-v1.1.xml?emailaddress=test@outlook.com': + case 'http://outlook.com/.well-known/autoconfig/mail/config-v1.1.xml?emailaddress=test@outlook.com': + throw new \Exception('Client error: `GET https://outlook.com/.well-known/autoconfig/mail/config-v1.1.xml?emailaddress=test@outlook.com` resulted in a `404 Not Found` response'); + case 'https://autoconfig.thunderbird.net/v1.1/outlook.com': + $response = $this->createMock(IResponse::class); + $response->method('getBody')->willReturn(file_get_contents(__DIR__ . '/../../../resources/autoconfig-outlook.xml')); + return $response; + } + }); + + $ispDb = new IspDb($this->clientService, $this->logger); + + $providers = $ispDb->query('outlook.com', 'test@outlook.com'); + + $this->assertEquals('Microsoft', $providers['displayName']); + $this->assertCount(1, $providers['imap']); + $this->assertCount(1, $providers['smtp']); } - /** - * @todo check actual values - */ - private function assertContainsIspData($data) { - $this->assertArrayHasKey('imap', $data); - $this->assertTrue(count($data['imap']) >= 1, 'no isp imap data returned'); - $this->assertArrayHasKey('smtp', $data); - $this->assertTrue(count($data['smtp']) >= 1, 'no isp smtp data returned'); + public function testQueryPosteo(): void { + $this->client->method('get') + ->willReturnCallback(function () { + $response = $this->createMock(IResponse::class); + $response->method('getBody')->willReturn(file_get_contents(__DIR__ . '/../../../resources/autoconfig-posteo.xml')); + return $response; + }); + + $ispDb = new IspDb($this->clientService, $this->logger); + + $providers = $ispDb->query('posteo.org', 'test@postdeo.org'); + + $this->assertEquals('Posteo', $providers['displayName']); + $this->assertCount(1, $providers['imap']); + $this->assertCount(1, $providers['smtp']); } } diff --git a/tests/resources/autoconfig-gmx.xml b/tests/resources/autoconfig-gmx.xml new file mode 100644 index 000000000..d88883e52 --- /dev/null +++ b/tests/resources/autoconfig-gmx.xml @@ -0,0 +1,71 @@ +<clientConfig version="1.1"> + <emailProvider id="gmx.com"> + <domain>gmx.com</domain> + <domain>gmx.tm</domain> + <domain>gmx.us</domain> + <domain>gmx.co.uk</domain> + <domain>gmx.es</domain> + <domain>gmx.fr</domain> + <domain>gmx.ca</domain> + <domain>gmx.cn</domain> + <domain>gmx.co.in</domain> + <domain>gmx.com.br</domain> + <domain>gmx.com.my</domain> + <domain>gmx.hk</domain> + <domain>gmx.ie</domain> + <domain>gmx.ph</domain> + <domain>gmx.pt</domain> + <domain>gmx.ru</domain> + <domain>gmx.se</domain> + <domain>gmx.sg</domain> + <domain>gmx.tw</domain> + <domain>gmx.com.tr</domain> + <domain>gmx.it</domain> + <domain>gmx.li</domain> + <!-- gmx.net is same company, but different access servers --> + <displayName>GMX Freemail</displayName> + <displayShortName>GMX</displayShortName> + <incomingServer type="imap"> + <hostname>imap.gmx.com</hostname> + <port>993</port> + <socketType>SSL</socketType> + <username>%EMAILADDRESS%</username> + <authentication>password-cleartext</authentication> + </incomingServer> + <incomingServer type="imap"> + <hostname>imap.gmx.com</hostname> + <port>143</port> + <socketType>STARTTLS</socketType> + <username>%EMAILADDRESS%</username> + <authentication>password-cleartext</authentication> + </incomingServer> + <incomingServer type="pop3"> + <hostname>pop.gmx.com</hostname> + <port>995</port> + <socketType>SSL</socketType> + <username>%EMAILADDRESS%</username> + <authentication>password-cleartext</authentication> + </incomingServer> + <incomingServer type="pop3"> + <hostname>pop.gmx.com</hostname> + <port>110</port> + <socketType>STARTTLS</socketType> + <username>%EMAILADDRESS%</username> + <authentication>password-cleartext</authentication> + </incomingServer> + <outgoingServer type="smtp"> + <hostname>mail.gmx.com</hostname> + <port>465</port> + <socketType>SSL</socketType> + <username>%EMAILADDRESS%</username> + <authentication>password-cleartext</authentication> + </outgoingServer> + <outgoingServer type="smtp"> + <hostname>mail.gmx.com</hostname> + <port>587</port> + <socketType>STARTTLS</socketType> + <username>%EMAILADDRESS%</username> + <authentication>password-cleartext</authentication> + </outgoingServer> + </emailProvider> +</clientConfig> diff --git a/tests/resources/autoconfig-outlook.xml b/tests/resources/autoconfig-outlook.xml new file mode 100644 index 000000000..745cdd147 --- /dev/null +++ b/tests/resources/autoconfig-outlook.xml @@ -0,0 +1,53 @@ +<clientConfig version="1.1"> + <emailProvider id="outlook.com"> + <!-- This is a temporary config until bug 1185366 is fixed. + Without the fix, this config is fetched both for + for all (!) Office365 users, due to the MX setup. + So, this config here is mostly adjusted to Office365 users. + It is unfortunately also used for foo@outlook.com email addresses + (and only this exact domain, not outlook.de, not hotmail.com etc.), + Bug 1185366 fixes this and means that Office365 will + fetch the separate office365.com config. + Once that fix is deployed to 95+% of end users, + remove this config and add the outlook.com domain + to the hotmail.com config. --> + <domain>outlook.com</domain> + <displayName>Microsoft</displayName> + <displayShortName>Microsoft</displayShortName> + <incomingServer type="imap"> + <hostname>outlook.office365.com</hostname> + <port>993</port> + <socketType>SSL</socketType> + <authentication>password-cleartext</authentication> + <username>%EMAILADDRESS%</username> + </incomingServer> + <incomingServer type="pop3"> + <hostname>outlook.office365.com</hostname> + <port>995</port> + <socketType>SSL</socketType> + <authentication>password-cleartext</authentication> + <username>%EMAILADDRESS%</username> + <pop3> + <leaveMessagesOnServer>true</leaveMessagesOnServer> + <!-- Outlook.com docs specifically mention that POP3 deletes have effect on the main inbox on webmail and IMAP --> + </pop3> + </incomingServer> + <incomingServer type="exchange"> + <hostname>outlook.office365.com</hostname> + <port>443</port> + <username>%EMAILADDRESS%</username> + <socketType>SSL</socketType> + <authentication>OAuth2</authentication> + <owaURL>https://outlook.office365.com/owa/</owaURL> + <ewsURL>https://outlook.office365.com/ews/exchange.asmx</ewsURL> + <useGlobalPreferredServer>true</useGlobalPreferredServer> + </incomingServer> + <outgoingServer type="smtp"> + <hostname>smtp.office365.com</hostname> + <port>587</port> + <socketType>STARTTLS</socketType> + <authentication>password-cleartext</authentication> + <username>%EMAILADDRESS%</username> + </outgoingServer> + </emailProvider> +</clientConfig> diff --git a/tests/resources/autoconfig-posteo.xml b/tests/resources/autoconfig-posteo.xml new file mode 100644 index 000000000..7c7be6766 --- /dev/null +++ b/tests/resources/autoconfig-posteo.xml @@ -0,0 +1,33 @@ +<clientConfig version="1.1"> + <emailProvider id="posteo.de"> + <domain>posteo.de</domain> + <domain>posteo.at</domain> + <domain>posteo.ch</domain> + <domain>posteo.org</domain> + <domain>posteo.eu</domain> + <domain>posteo.ie</domain> + <displayName>Posteo</displayName> + <displayShortName>Posteo</displayShortName> + <incomingServer type="imap"> + <hostname>posteo.de</hostname> + <port>993</port> + <socketType>SSL</socketType> + <username>%EMAILADDRESS%</username> + <authentication>password-cleartext</authentication> + </incomingServer> + <incomingServer type="pop3"> + <hostname>posteo.de</hostname> + <port>995</port> + <socketType>SSL</socketType> + <username>%EMAILADDRESS%</username> + <authentication>password-cleartext</authentication> + </incomingServer> + <outgoingServer type="smtp"> + <hostname>posteo.de</hostname> + <port>465</port> + <socketType>SSL</socketType> + <username>%EMAILADDRESS%</username> + <authentication>password-cleartext</authentication> + </outgoingServer> + </emailProvider> +</clientConfig> |