Skip to content

Commit

Permalink
adding verification for \Auth0\SDK\API\Management\EmailTemplates::cre…
Browse files Browse the repository at this point in the history
…ate, better tests
  • Loading branch information
joshcanhelp committed May 3, 2018
1 parent 3e612ad commit 9310228
Show file tree
Hide file tree
Showing 3 changed files with 131 additions and 28 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
}
},
"scripts": {
"test": "vendor/bin/phpunit",
"test": "SHELL_INTERACTIVE=1 vendor/bin/phpunit --colors=always --verbose ",
"test-ci": "vendor/bin/phpunit --coverage-text --coverage-clover=build/coverage.xml"
},
"license": "MIT"
Expand Down
92 changes: 85 additions & 7 deletions src/API/Management/EmailTemplates.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
*/
namespace Auth0\SDK\API\Management;

use \Auth0\SDK\Exception\CoreException;

/**
* Class EmailTemplates.
* Handles requests to the Email Templates endpoint of the v2 Management API.
Expand All @@ -12,12 +14,57 @@
*/
class EmailTemplates extends GenericResource
{
/**
* @var string
*/
const TEMPLATE_VERIFY_EMAIL = 'verify_email';

/**
* @var string
*/
const TEMPLATE_RESET_EMAIL = 'reset_email';

/**
* @var string
*/
const TEMPLATE_WELCOME_EMAIL = 'welcome_email';

/**
* @var string
*/
const TEMPLATE_BLOCKED_ACCOUNT = 'blocked_account';

/**
* @var string
*/
const TEMPLATE_STOLEN_CREDENTIALS = 'stolen_credentials';

/**
* @var string
*/
const TEMPLATE_ENROLLMENT_EMAIL = 'enrollment_email';

/**
* @var string
*/
const TEMPLATE_CHANGE_PASSWORD = 'change_password';

/**
* @var string
*/
const TEMPLATE_PASSWORD_RESET = 'password_reset';

/**
* @var string
*/
const TEMPLATE_MFA_OOB_CODE = 'mfa_oob_code';

/**
* Get an email template by name.
* See docs @link below for valid names and fields.
* Requires scope: read:email_templates.
*
* @param string $templateName - the email template name to get.
* @param string $templateName - the email template name to get (see constants defined for this class).
*
* @return array
*
Expand All @@ -38,7 +85,7 @@ public function get($templateName)
* See docs @link below for valid names, fields, and possible responses.
* Requires scope: update:email_templates.
*
* @param string $templateName - the email template name to patch.
* @param string $templateName - the email template name to patch (see constants defined for this class).
* @param array $data - an array of data to update.
*
* @return array - updated data for the template name provided.
Expand All @@ -60,18 +107,49 @@ public function patch($templateName, $data)
* See docs @link below for valid names and fields.
* Requires scope: create:email_templates.
*
* @param array $data
* An array of data to use for the new email, including a valid `template`.
* See docs link below for required fields.
* @param string $template - the template name to create (see constants defined for this class).
* @param boolean $enabled - is the email template enabled?
* @param string $from - the email address the email should come from.
* @param string $subject - the email subject.
* @param string $body - the email body in the syntax indicated below.
* @param string $syntax - the email body syntax to use.
* @param string $resultUrl - URL where a click-through should land.
* @param int $urlLifetime - URL lifetime, in seconds.
*
* @return mixed|string
*
* @throws \Exception - if a 200 response was not returned from the API.
*
* @link https://auth0.com/docs/api/management/v2#!/Email_Templates/post_email_templates
*/
public function create($data)
{
public function create(
$template,
$enabled,
$from,
$subject,
$body,
$syntax = 'liquid',
$resultUrl = '',
$urlLifetime = 0
) {
// Required fields
$data = [
'template' => (string) $template,
'enabled' => (bool) $enabled,
'from' => (string) $from,
'subject' => (string) $subject,
'body' => (string) $body,
'syntax' => (string) $syntax,
];

if (! empty($resultUrl)) {
$data['resultUrl'] = filter_var($resultUrl, FILTER_SANITIZE_URL);
}

if (! empty($urlLifetime)) {
$data['urlLifetimeInSeconds'] = (int) $urlLifetime;
}

return $this->apiClient->method('post')
->addPath('email-templates')
->withBody(json_encode($data))
Expand Down
65 changes: 45 additions & 20 deletions tests/API/Management/EmailTemplatesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Auth0\Tests\API\Management;

use Auth0\SDK\API\Management;
use Auth0\SDK\API\Management\EmailTemplates;
use Auth0\Tests\API\ApiTests;
use GuzzleHttp\Exception\ClientException;

Expand All @@ -13,18 +14,26 @@
*/
class EmailTemplateTest extends ApiTests
{

const EMAIL_TEMPLATE_NAME = 'enrollment_email';
/**
* Email template to test
*
* @var string
*/
const EMAIL_TEMPLATE_NAME = EmailTemplates::TEMPLATE_ENROLLMENT_EMAIL;

/**
* Management API token with scopes read:email_templates, create:email_templates, update:email_templates
* Management API token with scopes:
* - read:email_templates
* - create:email_templates
* - update:email_templates
* - read:email_provider
*
* @var string
*/
protected static $token;

/**
* Valid tenant domain
* Valid tenant domain set in project .env file as `DOMAIN`
*
* @var string
*/
Expand All @@ -42,7 +51,14 @@ class EmailTemplateTest extends ApiTests
*
* @var array
*/
protected static $gotEmail;
protected static $gotEmail = [];

/**
* If the email template was not found, this is the error code
*
* @var bool
*/
protected static $setUpEmailError = null;

/**
* Can this email template be created?
Expand All @@ -62,17 +78,19 @@ public static function setUpBeforeClass()

self::$domain = $env['DOMAIN'];
self::$token = self::getTokenStatic($env, [
'email_templates' => [
'actions' => ['create', 'read', 'update']
]
'email_templates' => [ 'actions' => ['create', 'read', 'update'] ],
'email_provider' => [ 'actions' => ['read'] ],
]);

self::$api = new Management(self::$token, self::$domain);

try {
// Try to get the email template specified.
self::$gotEmail = self::$api->emailTemplates->get(self::EMAIL_TEMPLATE_NAME);
} catch (ClientException $e) {
if (404 === $e->getCode()) {
self::$setUpEmailError = $e->getCode();
if (404 === self::$setUpEmailError) {
// Could not find the email template so it can/must be created
self::$mustCreate = true;
}
}
Expand All @@ -83,25 +101,32 @@ public static function setUpBeforeClass()
*/
protected function assertPreConditions()
{
$this->assertNotEmpty(self::$token);
$this->assertInstanceOf(Management::class, self::$api);
// Need to have an email provider setup for the tenant to perform this test.
try {
self::$api->emails->getEmailProvider();
} catch (\Exception $e) {
$this->markTestSkipped('Need to specify an email provider in the dashboard > Emails > Provider');
}

// If we don't have an email template and can't create, something sent wrong in self::setUpBeforeClass().
if (! self::$mustCreate && empty(self::$gotEmail)) {
$this->markTestSkipped(
'Email template '. self::EMAIL_TEMPLATE_NAME . ' not found with error ' . self::$setUpEmailError
);
}
}

/**
* Test if we got an email template, test create if we didn't
*
* @throws \Exception
*/
public function testGotAnEmail()
{
if (self::$mustCreate) {
self::$gotEmail = self::$api->emailTemplates->create([
'template' => self::EMAIL_TEMPLATE_NAME,
'body' => '<!doctype html><html><body><h1>Hi!</h1></body></html>',
'from' => 'test@' . self::$domain,
'subject' => 'Test email',
'syntax' => 'liquid',
'urlLifetimeInSeconds' => 0,
'enabled' => false,
]);
$from_email = 'test@' . self::$domain;
self::$gotEmail = self::$api->emailTemplates->create(self::EMAIL_TEMPLATE_NAME, $from_email);
$this->assertEquals($from_email, self::$gotEmail['from']);
}

$this->assertEquals(self::EMAIL_TEMPLATE_NAME, self::$gotEmail['template']);
Expand Down

0 comments on commit 9310228

Please sign in to comment.