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

Automated tests for discord bot sample app #141

Merged
merged 12 commits into from
Nov 28, 2023
Merged
48 changes: 46 additions & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,52 @@
name: Test
on:
push:
pull_request:
permissions:
contents: read

jobs:
test-discord-bot:
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
matrix:
node: [16]
os: [ubuntu-20.04]
name: Discord Bot Sample App
steps:
## Set up Stargate
- name: disable and stop mono-xsp4.service
run: |
sudo kill -9 $(sudo lsof -t -i:8084)
- uses: actions/checkout@2541b1294d2704b0964813337f33b291d3f8596b
with:
repository: stargate/stargate-mongoose
path: stargate-mongoose
ref: main
- name: Set up JSON API
run: |
chmod +x bin/start_json_api.sh
bin/start_json_api.sh
working-directory: stargate-mongoose
- name: Wait for services
run: |
while ! nc -z localhost 8181; do sleep 1; done
while ! nc -z localhost 8081; do sleep 1; done
working-directory: stargate-mongoose
## Set up and run discord bot sample app tests
- uses: actions/checkout@2541b1294d2704b0964813337f33b291d3f8596b # v3.0.2
- name: Setup node
uses: actions/setup-node@eeb10cff27034e7acf239c5d29f62154018672fd # v3.1.1
with:
node-version: ${{ matrix.node }}

- run: npm install
working-directory: discord-bot
- run: npm run lint
working-directory: discord-bot
- run: npm test
working-directory: discord-bot
test-ecommerce:
runs-on: ${{ matrix.os }}
strategy:
Expand All @@ -26,11 +68,11 @@ jobs:
- name: Set up JSON API
run: |
chmod +x bin/start_json_api.sh
bin/start_json_api.sh -t v2.0.13 -j v1.0.0-ALPHA-9
bin/start_json_api.sh
working-directory: stargate-mongoose
- name: Wait for services
run: |
while ! nc -z localhost 8080; do sleep 1; done
while ! nc -z localhost 8181; do sleep 1; done
while ! nc -z localhost 8081; do sleep 1; done
working-directory: stargate-mongoose
## Set up and run ecommerce sample app tests
Expand All @@ -42,5 +84,7 @@ jobs:

- run: npm install
working-directory: netlify-functions-ecommerce
- run: npm run lint
working-directory: netlify-functions-ecommerce
- run: npm test
working-directory: netlify-functions-ecommerce
4 changes: 2 additions & 2 deletions api-compatibility.versions
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
stargate_version=v2.1.0-ALPHA-6
json_api_version=v1.0.0-ALPHA-12
stargate_version=v2.1.0-BETA-2
json_api_version=v1.0.0-BETA-3
4 changes: 4 additions & 0 deletions discord-bot/.env.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
JSON_API_URL=http://127.0.0.1:8181/v1/discordbot_test
JSON_API_AUTH_USERNAME=cassandra
JSON_API_AUTH_PASSWORD=cassandra
JSON_API_AUTH_URL=http://localhost:8081/v1/auth
1 change: 0 additions & 1 deletion discord-bot/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ async function run() {
authUrl: process.env.JSON_API_AUTH_URL
};
}
await mongoose.connect(uri, jsonApiConnectOptions);
console.log('Connecting to', uri);
await mongoose.connect(uri, jsonApiConnectOptions);
// Login to Discord with your client's token
Expand Down
7 changes: 5 additions & 2 deletions discord-bot/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"main": "index.js",
"scripts": {
"lint": "eslint .",
"test": "echo \"Error: no test specified\" && exit 1",
"test": "mocha test/*.js",
"start": "node ./index.js"
},
"author": "",
Expand All @@ -16,11 +16,14 @@
"dependencies": {
"@discordjs/rest": "2.2.0",
"@masteringjs/eslint-config": "0.0.1",
"@sapphire/framework": "4.8.2",
"discord.js": "14.14.1",
"dotenv": "16.3.1",
"eslint": "8.54.0",
"mongoose": "7.x",
"stargate-mongoose": "0.3.0"
},
"devDependencies": {
"mocha": "10.2.0",
"sinon": "17.0.1"
}
}
21 changes: 21 additions & 0 deletions discord-bot/test/countdocuments.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
'use strict';

const Bot = require('../models/bot');
const assert = require('assert');
const { describe, it } = require('mocha');
const countdocuments = require('../commands/countdocuments');
const sinon = require('sinon');

describe('countdocuments', function() {
it('returns the number of bot documents', async function() {
await Bot.deleteMany({});
await Bot.create({ name: 'test' });

const interaction = {
reply: sinon.stub()
};
await countdocuments.execute(interaction);
assert.ok(interaction.reply.calledOnce);
assert.deepEqual(interaction.reply.getCalls()[0].args, [1]);
});
});
24 changes: 24 additions & 0 deletions discord-bot/test/createdocument.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
'use strict';

const Bot = require('../models/bot');
const assert = require('assert');
const { describe, it } = require('mocha');
const createdocument = require('../commands/createdocument');
const sinon = require('sinon');

describe('createdocument', function() {
it('inserts a new document', async function() {
await Bot.deleteMany({});

const interaction = {
reply: sinon.stub()
};
await createdocument.execute(interaction);
assert.ok(interaction.reply.calledOnce);
assert.deepEqual(interaction.reply.getCalls()[0].args, ['done!']);

const docs = await Bot.find();
assert.equal(docs.length, 1);
assert.equal(docs[0].name, 'I am a document');
});
});
17 changes: 17 additions & 0 deletions discord-bot/test/ping.test.js
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just for curiosity, is this test necessary?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I admit this test isn't exactly testing a very complex code path, but I figure it can be a good starting point for someone looking to write their own tests.

Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
'use strict';

const assert = require('assert');
const { describe, it } = require('mocha');
const ping = require('../commands/ping');
const sinon = require('sinon');

describe('ping', function() {
it('replies with "Pong!"', async function() {
const interaction = {
reply: sinon.stub()
};
await ping.execute(interaction);
assert.ok(interaction.reply.calledOnce);
assert.deepEqual(interaction.reply.getCalls()[0].args, ['Pong!']);
});
});
21 changes: 21 additions & 0 deletions discord-bot/test/setup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
'use strict';

require('dotenv').config({ path: `${__dirname}/../.env.test` });

const Bot = require('../models/bot');
const { before } = require('mocha');
const mongoose = require('../mongoose');

const uri = process.env.JSON_API_URL;
const jsonApiConnectOptions = {
username: process.env.JSON_API_AUTH_USERNAME,
password: process.env.JSON_API_AUTH_PASSWORD,
authUrl: process.env.JSON_API_AUTH_URL
};

before(async function() {
console.log('Connecting to', uri);
await mongoose.connect(uri, jsonApiConnectOptions);
await Bot.db.dropCollection('bots').catch(() => {});
await Bot.createCollection();
});
14 changes: 14 additions & 0 deletions netlify-functions-ecommerce/.env.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#Fill the Local JSON API related details only when NODE_ENV is set to 'jsonapi'
#Local JSON API URL for example: http://127.0.0.1:8181/v1/ecommerce_test where 'ecommerce_test' is the keyspace name
JSON_API_URL=http://127.0.0.1:8181/v1/ecommerce_test
#Auth URL for example: http://127.0.0.1:8081/v1/auth
JSON_API_AUTH_URL=http://127.0.0.1:8081/v1/auth
#Auth username and password
JSON_API_AUTH_USERNAME=cassandra
JSON_API_AUTH_PASSWORD=cassandra

#Fill in Stripe related details if you want to see Stripe integration.
#Otherwise the sample app will bypass Stripe.
STRIPE_SECRET_KEY=test-key
STRIPE_SUCCESS_URL=http://127.0.0.1:8888/order-confirmation
STRIPE_CANCEL_URL=http://127.0.0.1:8888/cart
1 change: 1 addition & 0 deletions netlify-functions-ecommerce/.eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
public
14 changes: 14 additions & 0 deletions netlify-functions-ecommerce/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
'use strict';

module.exports = {
extends: [
'@masteringjs'
],
parserOptions: {
ecmaVersion: 2020
},
env: {
node: true,
es6: true
}
};
2 changes: 1 addition & 1 deletion netlify-functions-ecommerce/connect.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ require('dotenv').config();
const mongoose = require('./mongoose');

require('./models');
const {createAstraUri} = require("stargate-mongoose");
const { createAstraUri } = require('stargate-mongoose');

let conn = null;

Expand Down
11 changes: 11 additions & 0 deletions netlify-functions-ecommerce/frontend/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
'use strict';

module.exports = {
env: {
browser: true
},
globals: {
Vue: true,
VueRouter: true
}
};
2 changes: 2 additions & 0 deletions netlify-functions-ecommerce/frontend/src/BaseComponent.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
'use strict';

module.exports = (html, css) => {
appendCSS(css);
return {
Expand Down
2 changes: 1 addition & 1 deletion netlify-functions-ecommerce/frontend/src/cart/cart.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ module.exports = app => app.component('cart', {
computed: {
cartTotal() {
return '$' + this.state.cart.items.reduce((sum, item) => {
return sum + (+(item.quantity * this.product(item).price).toFixed(2))
return sum + (+(item.quantity * this.product(item).price).toFixed(2));
}, 0).toFixed(2);
}
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ const handler = async(event) => {
return {
statusCode: 200,
body: JSON.stringify({ cart: cart, url: '/order-confirmation' })
}
};
}

const session = await stripe.checkout.sessions.create({
Expand Down
3 changes: 3 additions & 0 deletions netlify-functions-ecommerce/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
"webpack": "5.x"
},
"devDependencies": {
"@masteringjs/eslint-config": "0.0.1",
"axios": "1.6.2",
"eslint": "8.54.0",
"mocha": "10.2.0",
"netlify-cli": "17.6.0"
},
Expand All @@ -21,6 +23,7 @@
},
"scripts": {
"build": "node ./frontend/build",
"lint": "eslint .",
"seed": "node ./seed",
"start": "netlify dev --dir public --functions netlify/functions",
"test": "mocha ./test/*.test.js",
Expand Down
Loading
Loading