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

Add integration tests #161

Merged
merged 6 commits into from
Jul 31, 2023
Merged
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
9 changes: 9 additions & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,12 @@ jobs:
- name: Run tests
run: |
npm run test:coverage

- name: Run integration tests
env:
TUMBLR_OAUTH_CONSUMER_KEY: ${{ secrets.TUMBLR_OAUTH_CONSUMER_KEY }}
TUMBLR_OAUTH_CONSUMER_SECRET: ${{ secrets.TUMBLR_OAUTH_CONSUMER_SECRET }}
TUMBLR_OAUTH_TOKEN: ${{ secrets.TUMBLR_OAUTH_TOKEN }}
TUMBLR_OAUTH_TOKEN_SECRET: ${{ secrets.TUMBLR_OAUTH_TOKEN_SECRET }}
run: |
npm run test:integration
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

- **Breaking** Support Node.js versions `>=16`

### Added

- Integration test suites using real API.

## [3.0.0] - 2020-07-28

## [2.0.0] - 2018-06-13
Expand Down
51 changes: 51 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,54 @@ problems, please open an issue. We also actively welcome pull requests.

By contributing to tumblr.js you agree that your contributions will be licensed under its Apache 2.0
license.

## Testing

First install dependencies by running `npm ci`.

### Unit tests

Unit tests can be found under the `test/` directory. Run them tests as follows:

```sh
npm run test
```

### Integration tests

There are integration tests that will query the API. They can be found under the `integration/`
directory.

They require OAuth1 application credentials. To get valid credentials, visit the
[Tumblr OAuth Applications page](https://www.tumblr.com/oauth/apps).

**Never share your credentials. They're secret!**

Part of the suite can be run with just the `consumer_key`, which is required:

```sh
TUMBLR_OAUTH_CONSUMER_KEY='--- valid consumer_key ---' \
npm run test:integration
```

To run the full suite, you must provide complete OAuth1 credentials. These full credentials can be
found by visiting the [Tumblr API Console](https://api.tumblr.com/console/calls/user/info) and
entering your OAuth Application consumer credentials. _Note:_ You may need to set
`https://api.tumblr.com/console/calls/user/info` as the default callback URL for you application.
It's recommended to create a dedicated application for testing this library.

Provide the full OAuth1 credentials as environment variables and run the integration test suite:

```sh
TUMBLR_OAUTH_CONSUMER_KEY='--- valid consumer_key ---' \
TUMBLR_OAUTH_CONSUMER_SECRET='--- valid consumer_secret ---' \
TUMBLR_OAUTH_TOKEN='--- valid token ---' \
TUMBLR_OAUTH_TOKEN_SECRET='--- valid token_key ---' \
npm run test:integration
```

The above tests query the API to read data, they won't make any changes.

There are additional tests that _will write or modify data_. Those tests are disabled by default and
need to be enabled by changes to the test file. Be careful when running those tests and be sure the
credentials are for a test account.
61 changes: 61 additions & 0 deletions integration/read-only.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import { env } from 'node:process';
import { Client } from 'tumblr.js';
import { assert } from 'chai';
import { test } from 'mocha';

if (!env.TUMBLR_OAUTH_CONSUMER_KEY) {
throw new Error('Must provide TUMBLR_OAUTH_CONSUMER_KEY environment variable');
}

describe('consumer_key (api_key) only requests', () => {
/** @type {Client} */
let client;
before(() => {
client = new Client({
consumer_key: env.TUMBLR_OAUTH_CONSUMER_KEY,
});
client.returnPromises();
});

test('fetches blogInfo("staff")', async () => {
const resp = await client.blogInfo('staff');
assert.isOk(resp);
assert.equal(resp.blog.name, 'staff');
assert.equal(resp.blog.uuid, 't:0aY0xL2Fi1OFJg4YxpmegQ');
});
});

const OAUTH1_ENV_VARS = [
'TUMBLR_OAUTH_CONSUMER_SECRET',
'TUMBLR_OAUTH_TOKEN',
'TUMBLR_OAUTH_CONSUMER_SECRET',
];

describe('oauth1 requests', () => {
/** @type {Client} */
let client;
before(function () {
if (!OAUTH1_ENV_VARS.every((envVarName) => Boolean(env[envVarName]))) {
console.warn(
`To run full oauth1 tests provide environment vars ${OAUTH1_ENV_VARS.join(', ')}`,
);
this.skip();
}

client = new Client({
consumer_key: env.TUMBLR_OAUTH_CONSUMER_KEY,
consumer_secret: env.TUMBLR_OAUTH_CONSUMER_SECRET,
token: env.TUMBLR_OAUTH_TOKEN,
token_secret: env.TUMBLR_OAUTH_TOKEN_SECRET,
});
client.returnPromises();
});

test('fetches userInfo()', async () => {
const resp = await client.userInfo();
assert.isOk(resp);
assert.typeOf(resp.user.name, 'string');
assert.typeOf(resp.user.blogs, 'array');
assert.typeOf(resp.user.blogs[0].uuid, 'string');
});
});
51 changes: 51 additions & 0 deletions integration/write.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { env } from 'node:process';
import { Client } from 'tumblr.js';
import { assert } from 'chai';
import { test } from 'mocha';

if (
!env.TUMBLR_OAUTH_CONSUMER_KEY ||
!env.TUMBLR_OAUTH_CONSUMER_SECRET ||
!env.TUMBLR_OAUTH_TOKEN ||
!env.TUMBLR_OAUTH_TOKEN_SECRET
) {
throw new Error('Must provide all Oauth1 environment variables');
}

describe('oauth1 write requests', () => {
/** @type {Client} */
let client;
before(function () {
if (!env.CI) {
console.warn(
'This test suite uses the API to make changes. Modify the test suite to enabled it.',
);
this.skip();
}

client = new Client({
consumer_key: env.TUMBLR_OAUTH_CONSUMER_KEY,
consumer_secret: env.TUMBLR_OAUTH_CONSUMER_SECRET,
token: env.TUMBLR_OAUTH_TOKEN,
token_secret: env.TUMBLR_OAUTH_TOKEN_SECRET,
});
client.returnPromises();
});

test('creates a post', async () => {
const userResp = await client.userInfo();

assert.isOk(userResp);
const blogName = userResp.user.blogs[0].name;

assert.isOk(
await client.createPost(blogName, {
type: 'text',
format: 'markdown',
title: `Automated test post ${new Date().toISOString()}`,
body: 'This post was automatically generated by the tumblr.js tests.\n\n[The official JavaScript client library for the Tumblr API.](https://github.com/tumblr/tumblr.js)',
tags: `tumblr.js-test,tumblr.js-version-${client.version}`,
}),
);
});
});
6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,18 @@
"version": "3.0.0",
"description": "Official JavaScript client for the Tumblr API",
"main": "./lib/tumblr",
"types": "./lib/tumblr.d.ts",
"type": "commonjs",
"exports": {
".": "./lib/tumblr.js"
},
"scripts": {
"autoformat": "prettier --write .",
"format-check": "prettier --check .",
"generate-docs": "rm -rf gh-pages && node_modules/.bin/jsdoc -c jsdoc.json",
"gh-pages": "git subtree push --prefix=gh-pages origin gh-pages",
"test": "mocha",
"test:coverage": "nyc mocha",
"test:integration": "mocha ./integration",
"lint": "eslint --report-unused-disable-directives lib test"
},
"engines": {
Expand Down