Skip to content

Commit

Permalink
Initial cucumber tests
Browse files Browse the repository at this point in the history
  • Loading branch information
allouis committed Jul 15, 2024
1 parent c952c9e commit 3f1d11a
Show file tree
Hide file tree
Showing 7 changed files with 889 additions and 10 deletions.
1 change: 1 addition & 0 deletions cucumber.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default `--format-options '{"snippetInterface": "synchronous"}'`
19 changes: 18 additions & 1 deletion docker-compose.testing.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,27 @@ services:
- MYSQL_PORT=3306
- MYSQL_DATABASE=activitypub
- NODE_ENV=testing
command: yarn test:all
command: node --import tsx --watch src/app.ts
depends_on:
mysql-testing:
condition: service_healthy
healthcheck:
test: "wget --spider http://0.0.0.0:8080/ping"
interval: 1s
retries: 120

cucumber-tests:
build: .
volumes:
- ./features:/opt/activitypub/features
- ./cucumber.js:/opt/activitypub/cucumber.js
environment:
- NODE_ENV=testing
command: yarn run cucumber-js
depends_on:
activitypub:
condition: service_healthy


mysql-testing:
image: mysql:lts
Expand Down
15 changes: 15 additions & 0 deletions features/handle-create-article.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
Feature: Create(Article)
We want to handle Create(Article) activities in the Inbox

Scenario: We recieve a Create(Article) activity from someone we follow
Given a valid "Create(Article)" activity
Given the actor is "known"
When it is sent to the Inbox
Then the request is accepted
Then the activity is in the Inbox

Scenario: We recieve a Create(Article) activity from someone we don't follow
Given a valid "Create(Article)" activity
Given the actor is "unknown"
When it is sent to the Inbox
Then the request is rejected
144 changes: 144 additions & 0 deletions features/step_definitions/stepdefs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
import assert from 'assert';
import { Given, When, Then } from '@cucumber/cucumber';

const activites = {
Create: {
'@context': [
'https://www.w3.org/ns/activitystreams',
'https://w3id.org/security/data-integrity/v1',
],
'type': 'Create',
'id': 'https://www.site.com/create/1',
'to': 'as:Public',
'cc': 'https://www.site.com/followers',
},
};

const objects = {
Article: {
'type': 'Article',
'id': 'https://www.site.com/article/1',
'to': 'as:Public',
'cc': 'https://www.site.com/followers',
'url': 'https://www.site.com/article/1',
'content': '<p>This is a test article</p>',
'published': '2020-04-20T04:20:00Z',
'attributedTo': 'https://site.com/user'
},
Note: {
'type': 'Note',
'id': 'https://www.site.com/note/1',
'to': 'as:Public',
'cc': 'https://www.site.com/followers',
'url': 'https://www.site.com/note/1',
'content': '<p>This is a test note</p>',
'published': '2020-04-20T04:20:00Z',
'attributedTo': 'https://site.com/user'
}
};

const actors = {
known: {
'id': 'https://site.com/user',
'url': 'https://site.com/user',
'name': 'Test Actor',
'type': 'Person',
'inbox': 'https://site.com/inbox',
'outbox': 'https://site.com/outbox',
'summary': 'A test actor for testing',
'followers': 'https://site.com/followers',
'following': 'https://site.com/following',
'published': '2024-02-21T00:00:00Z',
'preferredUsername': 'index',
'as:manuallyApprovesFollowers': false,
'https://w3id.org/security#publicKey': {
'id': 'https://www.site.com/user#main-key',
'type': 'https://w3id.org/security#Key',
'https://w3id.org/security#owner': {
'id': 'https://site.com/user'
},
'https://w3id.org/security#publicKeyPem': '-----BEGIN PUBLIC KEY-----\nMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAtSc3IqGjRaO3vcFdQ15D\nF90WVJC6tb2QwYBh9kQYVlQ1VhBiF6E4GK2okvyvukIL5PHLCgfQrfJmSiopk9Xo\n46Qri6rJbcPoWoZz/jWN0pfmU20hNuTQx6ebSoSkg6rHv1MKuy5LmDGLFC2ze3kU\nsY8u7X6TOBrifs/N+goLaH3+SkT2hZDKWJrmDyHzj043KLvXs/eiyu50M+ERoSlg\n70uO7QAXQFuLMILdy0UNJFM4xjlK6q4Jfbm4MC8QRG+i31AkmNvpY9JqCLqu0mGD\nBrdfJeN8PN+7DHW/Pzspf5RlJtlvBx1dS8Bxo2xteUyLGIaTZ9HZFhHc3IrmmKeW\naQIDAQAB\n-----END PUBLIC KEY-----\n'
}
},
unknown: {
'id': 'https://site.com/user',
'url': 'https://site.com/user',
'name': 'Test Actor',
'type': 'Person',
'inbox': 'https://site.com/inbox',
'outbox': 'https://site.com/outbox',
'summary': 'A test actor for testing',
'followers': 'https://site.com/followers',
'following': 'https://site.com/following',
'published': '2024-02-21T00:00:00Z',
'preferredUsername': 'index',
'as:manuallyApprovesFollowers': false,
'https://w3id.org/security#publicKey': {
'id': 'https://www.site.com/user#main-key',
'type': 'https://w3id.org/security#Key',
'https://w3id.org/security#owner': {
'id': 'https://site.com/user'
},
'https://w3id.org/security#publicKeyPem': '-----BEGIN PUBLIC KEY-----\nMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAtSc3IqGjRaO3vcFdQ15D\nF90WVJC6tb2QwYBh9kQYVlQ1VhBiF6E4GK2okvyvukIL5PHLCgfQrfJmSiopk9Xo\n46Qri6rJbcPoWoZz/jWN0pfmU20hNuTQx6ebSoSkg6rHv1MKuy5LmDGLFC2ze3kU\nsY8u7X6TOBrifs/N+goLaH3+SkT2hZDKWJrmDyHzj043KLvXs/eiyu50M+ERoSlg\n70uO7QAXQFuLMILdy0UNJFM4xjlK6q4Jfbm4MC8QRG+i31AkmNvpY9JqCLqu0mGD\nBrdfJeN8PN+7DHW/Pzspf5RlJtlvBx1dS8Bxo2xteUyLGIaTZ9HZFhHc3IrmmKeW\naQIDAQAB\n-----END PUBLIC KEY-----\n'
}
},
};

Given('a valid {string} activity', function (string) {
const [match, activity, object] = string.match(/(\w+)\((\w+)\)/) || [null]
if (!match) {
throw new Error(`Could not match ${string} to an activity`);
}
this.activity = activity;
this.object = object;
this.actor = 'known';
});

Given('the actor is {string}', function (string) {
this.actor = string;
});

When('it is sent to the Inbox', async function () {
if (!this.activity || !this.object || !this.actor) {
throw new Error(`Incomplete information for activity`);
}
const activity = activites[this.activity];
const object = objects[this.object];
const actor = actors[this.actor];

const payload = {
...activity,
...{object},
...{actor},
};

try {
this.response = await fetch('http://activitypub:8080/.ghost/activitypub/inbox/index', {
method: 'POST',
headers: {
'Content-Type': 'application/ld+json'
},
body: JSON.stringify(payload)
});
} catch (err) {
console.log(err);
}
});

Then('the request is rejected', function () {
assert(!this.response.ok);
});

Then('the request is accepted', function () {
assert(this.response.ok);
});

Then('the activity is in the Inbox', async function () {
const response = await fetch('http://activitypub:8080/.ghost/activitypub/inbox/index', {
headers: {
Accept: 'application/ld+json'
}
});
const inbox = await response.json();
console.log(inbox);
});
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"src"
],
"devDependencies": {
"@cucumber/cucumber": "^10.8.0",
"@types/mocha": "10.0.7",
"@types/node": "20.12.12",
"@types/uuid": "10.0.0",
Expand Down
6 changes: 6 additions & 0 deletions src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,12 @@ app.use(async (ctx, next) => {

/** Custom API routes */

app.get('/ping', (ctx) => {
return new Response('', {
status: 200
});
});

app.get('/.ghost/activitypub/inbox/:handle', inboxHandler);
app.post('/.ghost/activitypub/webhooks/post/published', postPublishedWebhook);
app.post('/.ghost/activitypub/actions/follow/:handle', followAction);
Expand Down
Loading

0 comments on commit 3f1d11a

Please sign in to comment.