Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for a personal Akismet API key and site #195

Closed
wants to merge 15 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<img src="logo.png" width="300">

# Staticman [![coverage](https://img.shields.io/badge/coverage-55%25-red.svg?style=flat?style=flat-square)](https://github.com/eduardoboucas/staticman) [![Build Status](https://travis-ci.org/eduardoboucas/staticman.svg?branch=master)](https://travis-ci.org/eduardoboucas/staticman) [![JavaScript Style Guide](https://img.shields.io/badge/code_style-standard-brightgreen.svg)](https://standardjs.com)
# Staticman [![coverage](https://img.shields.io/badge/coverage-55%2525-red.svg?style=flat?style=flat-square)](https://github.com/eduardoboucas/staticman) [![Build Status](https://travis-ci.org/eduardoboucas/staticman.svg?branch=master)](https://travis-ci.org/eduardoboucas/staticman) [![JavaScript Style Guide](https://img.shields.io/badge/code_style-standard-brightgreen.svg)](https://standardjs.com)

> Static sites with superpowers

Expand Down
10 changes: 8 additions & 2 deletions lib/Staticman.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,11 @@ Staticman.prototype._applyGeneratedFields = function (data) {

break

case 'ip':
data[field] = this.ip

break

case 'slugify':
if (typeof options.field === 'string') {
data[field] = slugify(data[options.field]).toLowerCase()
Expand Down Expand Up @@ -115,13 +120,14 @@ Staticman.prototype._checkForSpam = function (fields) {

return new Promise((resolve, reject) => {
const akismet = akismetApi.client({
apiKey: config.get('akismet.apiKey'),
blog: config.get('akismet.site')
apiKey: this.siteConfig.get('akismet.apiKey') || config.get('akismet.apiKey'),
blog: this.siteConfig.get('akismet.site') || config.get('akismet.site')
})

akismet.checkSpam({
user_ip: this.ip,
user_agent: this.userAgent,
permalink: this.options && this.options.origin,
comment_type: this.siteConfig.get('akismet.type'),
comment_author: fields[this.siteConfig.get('akismet.author')],
comment_author_email: fields[this.siteConfig.get('akismet.authorEmail')],
Expand Down
9 changes: 8 additions & 1 deletion server.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,15 @@ StaticmanAPI.prototype.initialiseBruteforceProtection = function () {

StaticmanAPI.prototype.initialiseCORS = function () {
this.server.use((req, res, next) => {
res.header('Access-Control-Allow-Origin', '*')
res.header('Access-Control-Allow-Origin', req.headers.origin || '*')
res.header('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept')
res.header('Access-Control-Allow-Credentials', 'true')
res.header('Access-Control-Allow-Methods', 'GET, POST')

if (req.query.__amp_source_origin) {
res.header('AMP-Access-Control-Allow-Source-Origin', req.query.__amp_source_origin)
res.header('Access-Control-Expose-Headers', 'AMP-Access-Control-Allow-Source-Origin')
}

next()
})
Expand Down
11 changes: 11 additions & 0 deletions siteConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,17 @@ const schema = {
format: Boolean,
default: false
},
site: {
doc: 'URL of an Akismet account used for spam checking.',
docExample: 'http://yourdomain.com',
format: String,
default: ''
},
apiKey: {
doc: 'API key to be used with Akismet.',
format: 'EncryptedString',
default: ''
},
author: {
doc: 'Name of the field to be used as the entry\'s author in Akistmet',
format: String,
Expand Down
38 changes: 38 additions & 0 deletions test/unit/lib/Staticman.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const config = require('./../../../config')
const errorHandler = require('./../../../lib/ErrorHandler')
const RSA = require('./../../../lib/RSA')
const frontMatter = require('front-matter')
const md5 = require('md5')
const moment = require('moment')
Expand Down Expand Up @@ -245,6 +246,10 @@ describe('Staticman interface', () => {

test('makes a request to the Akismet API sending the correct data', () => {
const fields = mockHelpers.getFields()
const options = {
origin: 'http://testsite.com'
}

const mockCheckSpamFn = jest.fn((options, callback) => {
callback(null, false)
})
Expand All @@ -264,6 +269,7 @@ describe('Staticman interface', () => {
mockConfig.set('akismet.authorEmail', 'email')
mockConfig.set('akismet.authorUrl', 'url')
mockConfig.set('akismet.content', 'message')
staticman.options = options
staticman.siteConfig = mockConfig

return staticman._checkForSpam(fields).then(response => {
Expand All @@ -277,6 +283,7 @@ describe('Staticman interface', () => {

expect(mockCheckSpamFn).toHaveBeenCalledTimes(1)
expect(mockCheckSpamFn.mock.calls[0][0]).toEqual({
permalink: options.origin,
comment_type: 'comment',
comment_author: fields.name,
comment_author_email: fields.email,
Expand All @@ -287,6 +294,37 @@ describe('Staticman interface', () => {
})
})

test('overrides Akismet apiKey and site if they exist in site config', () => {
const fields = mockHelpers.getFields()
const mockCheckSpamFn = jest.fn((options, callback) => {
callback(null, false)
})
const mockClientFn = jest.fn(options => ({
checkSpam: mockCheckSpamFn
}))

jest.mock('akismet', () => ({
client: mockClientFn
}))

const Staticman = require('./../../../lib/Staticman')
const staticman = new Staticman(mockParameters)

const akismetApiKey = '123456789'

mockConfig.set('akismet.enabled', true)
mockConfig.set('akismet.apiKey', RSA.encrypt(akismetApiKey))
mockConfig.set('akismet.site', 'http://mytestsite.com')
staticman.siteConfig = mockConfig

return staticman._checkForSpam(fields).then(response => {
expect(mockClientFn.mock.calls[0][0]).toEqual({
apiKey: akismetApiKey,
blog: mockConfig.get('akismet.site')
})
})
})

test('throws an error if the Akismet API call fails', () => {
const akismetError = new Error('Akismet error')
const fields = mockHelpers.getFields()
Expand Down