-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Format emails to have better content
Also generate email screenshots while running tests
- Loading branch information
Showing
16 changed files
with
722 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,11 @@ PORT=3005 | |
|
||
# name of the app that sends the emails - featured in the message subject or body | ||
APP_NAME=sleepy.bike | ||
# provide logo to display on top of emails | ||
# it can be base64 encoded url, or path to an image - keep it small! | ||
# by default the logo doesn't show - it's one transparent pixel | ||
APP_LOGO= | ||
SUPPORT_EMAIL=[email protected] | ||
|
||
# server base url, e.g. to construct correct email verification link | ||
# this is the base url that end users see | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,3 +6,6 @@ node_modules | |
database.sqlite | ||
|
||
*.pem | ||
|
||
# Screenshots of emails from testing | ||
screenshots |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,21 @@ | ||
import 'dotenv/config' | ||
import SMTPTransport from 'nodemailer/lib/smtp-transport' | ||
import { getBase64Image } from '../utils' | ||
|
||
// the defaults work for tests. you should define your own | ||
// either via .env file, or via environment variables directly (depends on your setup) | ||
|
||
// server base url, e.g. to construct correct email verification links | ||
export const baseUrl = process.env.BASE_URL ?? 'http://localhost:3005' | ||
|
||
export const appName = process.env.APP_NAME ?? 'sleepy.bike' | ||
export const appName = process.env.APP_NAME ?? 'Sleepy.Bike' | ||
|
||
// default is 1 transparent PNG pixel | ||
export const appLogo = getBase64Image( | ||
process.env.APP_LOGO ?? | ||
'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNkYAAAAAYAAjCB0C8AAAAASUVORK5CYII=', | ||
) | ||
export const supportEmail = process.env.SUPPORT_EMAIL ?? '[email protected]' | ||
|
||
// identity under which the mailer is operating | ||
export const mailerCredentials = { | ||
|
@@ -28,7 +36,7 @@ const stringToBoolean = (value: string | undefined): boolean => { | |
} | ||
// SMTP transport for nodemailer (setup for sending emails) | ||
export const smtpTransportOptions: SMTPTransport.Options = { | ||
host: process.env.SMTP_TRANSPORT_HOST || undefined, | ||
host: process.env.SMTP_TRANSPORT_HOST || '0.0.0.0', | ||
port: process.env.SMTP_TRANSPORT_PORT | ||
? +process.env.SMTP_TRANSPORT_PORT | ||
: 1025, // default works for maildev | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,15 +2,17 @@ | |
<head> | ||
<meta charset='utf-8' /> | ||
<title>{{title}}</title> | ||
<style> | ||
/* Email styles go here */ body { font-family: Arial, sans-serif; } | ||
</style> | ||
</head> | ||
<body> | ||
<header><img | ||
alt='logo of {{appName}}' | ||
src={{appLogo}} | ||
height='32' | ||
/></header> | ||
{{{body}}} | ||
<footer> | ||
<p>Contact us at | ||
<a href='mailto:[email protected]'>[email protected]</a></p> | ||
<p>You can contact us at | ||
<a href='mailto:{{supportEmail}}'>{{supportEmail}}</a></p> | ||
</footer> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,16 @@ | ||
<p>Hello{{#if target.name}} {{/if}}{{target.name}}!</p> | ||
<p> | ||
Hello | ||
{{target.name}}!</p> | ||
<a href={{actor.id}}>{{actor.name}}</a> | ||
sent you a message from | ||
{{appName}}. | ||
<a href={{actor.id}}>{{#if | ||
actor.name | ||
}}{{actor.name}}{{else}}Somebody{{/if}}</a> | ||
sent you a message from | ||
{{appName}}. | ||
</p> | ||
|
||
<p>{{object.content}}</p> | ||
<blockquote>{{object.content}}</blockquote> | ||
|
||
<a href='https://sleepy.bike/messages/{{actor.id}}'>Reply on {{appName}}</a> | ||
<a | ||
href='https://sleepy.bike/messages/{{encodeURIComponent actor.id}}' | ||
class='action-button' | ||
>Reply on | ||
{{appName}}</a> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
.action-button { | ||
padding: 0.25rem 0.5rem; | ||
text-align: center; | ||
display: inline-block; | ||
border: 2px solid black; | ||
color: black; | ||
text-decoration: none; | ||
} | ||
|
||
.action-button:hover { | ||
background-color: #e6e6e6; | ||
} | ||
|
||
body { | ||
font-family: Arial, sans-serif; | ||
} | ||
|
||
footer { | ||
margin-top: 2rem; | ||
font-size: 0.9rem; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<p>To verify your email for | ||
{{appName}} | ||
notifications, please click the following link: | ||
</p> | ||
|
||
<p> | ||
<a href={{{emailVerificationLink}}} class='action-button'> | ||
Verify my email for | ||
{{appName}}</a> | ||
</p> | ||
|
||
<p>If the above link doesn't work, you can try copy-pasting the link directly | ||
into your browser:</p> | ||
|
||
<p>{{emailVerificationLink}}</p> | ||
|
||
<p>This email verification for | ||
{{appName}} | ||
was initialized by Solid identity | ||
{{actor}}. If you've received this message unexpectedly and you believe it to | ||
be an error, please ignore it, or contact us for support.</p> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import { expect } from 'chai' | ||
import fetch from 'cross-fetch' | ||
import { describe } from 'mocha' | ||
import { baseUrl } from '../config' | ||
import type { GoodBody } from '../controllers/notification' | ||
import { takeScreenshot, verifyEmail } from './helpers' | ||
import { | ||
authenticatedFetch, | ||
authenticatedFetch3, | ||
person, | ||
person3, | ||
} from './testSetup.spec' | ||
|
||
const email = '[email protected]' | ||
|
||
/** | ||
* Generate body for POST /notification | ||
*/ | ||
const getBody = ({ | ||
from, | ||
to, | ||
message, | ||
}: { | ||
from: string | ||
to: string | ||
message: string | ||
}): GoodBody => ({ | ||
'@context': 'https://www.w3.org/ns/activitystreams', | ||
type: 'Create', | ||
actor: { type: 'Person', id: from, name: 'PersonFromName' }, | ||
object: { type: 'Note', id: 'https://example', content: message }, | ||
target: { type: 'Person', id: to, name: 'PersonToName' }, | ||
}) | ||
|
||
describe('Generate screenshots of emails for visual testing', () => { | ||
// empty the maildev inbox | ||
beforeEach(async () => { | ||
await fetch('http://localhost:1080/email/all', { method: 'DELETE' }) | ||
}) | ||
|
||
beforeEach(async () => { | ||
// setup email for receiver | ||
await verifyEmail({ | ||
email, | ||
person: person3, | ||
authenticatedFetch: authenticatedFetch3, | ||
}) | ||
}) | ||
|
||
it('[everything ok] should generate screenshots of email verification and notification in ./screenshots/ folder', async () => { | ||
const response = await authenticatedFetch(`${baseUrl}/notification`, { | ||
method: 'post', | ||
headers: { 'content-type': 'application/ld+json' }, | ||
body: JSON.stringify( | ||
getBody({ from: person.webId, to: person3.webId, message: 'Hello!' }), | ||
), | ||
}) | ||
|
||
expect(response.status).to.equal(202) | ||
|
||
const emailResponse = await fetch('http://localhost:1080/email') | ||
const emails = await emailResponse.json() | ||
|
||
// one verification email and one notification | ||
expect(emails).to.have.length(2) | ||
|
||
for (const i in emails) { | ||
await takeScreenshot(emails[i], `email${i}`) | ||
} | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.