# fake-spotify-api

> Fake version of the Spotify API for testing against

Latest version **0.3.2** (published 2018-12-29) · MIT license · 0 weekly downloads

## Install

```sh
npm install fake-spotify-api
pnpm add fake-spotify-api
yarn add fake-spotify-api
bun add fake-spotify-api
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.3.2 |
| Published | 2018-12-29 |
| First published | 2018-12-29 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 11 |
| Unpacked size | 78.1 KB |
| Known vulnerabilities | 0 (+2 in 2 direct dependencies) |
| Install scripts | no |
| Author | Leon |
| Maintainers | leonyotk |

## Links

- npm: https://www.npmjs.com/package/fake-spotify-api
- npm.io page: https://npm.io/package/fake-spotify-api

## Dependencies (11)

- [joi](https://npm.io/package/joi.md) ^14.3.0
- [uuid](https://npm.io/package/uuid.md) ^3.3.2
- [dotenv](https://npm.io/package/dotenv.md) ^6.2.0
- [express](https://npm.io/package/express.md) ^4.16.4
- [inversify](https://npm.io/package/inversify.md) ^5.0.1
- [body-parser](https://npm.io/package/body-parser.md) ^1.18.3
- [cookie-parser](https://npm.io/package/cookie-parser.md) ^1.4.3
- [@types/express](https://npm.io/package/@types/express.md) ^4.16.0
- [reflect-metadata](https://npm.io/package/reflect-metadata.md) ^0.1.12
- [express-joi-validation](https://npm.io/package/express-joi-validation.md) ^0.3.0
- [inversify-express-utils](https://npm.io/package/inversify-express-utils.md) ^6.2.0

## Recent versions

- 0.3.2 (latest) — 2018-12-29
- 0.3.1 — 2018-12-29
- 0.3.0 — 2018-12-29
- 0.2.0 — 2018-12-29
- 0.1.0 — 2018-12-29

## README

# Fake Spotify API

A fake version of the Spotify API to allow: 
 - Testing without logging in to a real Spotify account (e.g. Automated testing as part of a CI/CD pipeline)
 - Testing error cases easily (e.g. User no longer exists, token expired)

## Usage

### Install

You'll need [npm](https://www.npmjs.com/get-npm) installed. Run `npm install`.

### Use in Chai tests

Run `npm install chai --save-dev` and `npm install chai-http --save dev`. You can then run the app as part of your tests using:

````typescript
import chai from 'chai';
import chaiHttp from 'chai-http';

import { fakeSpotifyApi } from 'fake-spotify-api'; 

chai.use(chaiHttp);
const request = chai.request;

describe('Some API', () => {
  let server: ChaiHttp.Agent;

  beforeEach(async () => {
    // Startup a server using the Fake Spotify API. Use the agent so that we can use the sp_ac cookie created in signup to automatically authorize
    server = request.agent(fakeSpotifyApi).keepOpen();
    // Create a user. Note that this is not possible with the real Spotify API - you'd have to go through the signup process on the website.
    await server.post('/signup').type('form').send({
        display_name: 'Test',
        email: 'test@example.com',
        password: 'secret',
    }); // You can use the response to this to get the user ID
    // Call the authorize endpoint, which replies with a redirect to the supplied location, adding a the token details.
    await server.get(`/authorize`).query({
      client_id: '1',
      redirect_uri: encodeURIComponent('http://localhost/'),
      response_type: 'token',
    }).redirects(0); // You can use the headers.location property of the response to get the token_type and access_token
  });

  // ...Add your tests here

  afterEach(async () => {
    // Shutdown the server so that we don't leave lots of these lying around
    server.close();
  });
});
````

See the [end2end tests folder](tests/end2end/) for examples.

### As an application

Run `npm start`. 

For environment variables see [.env](.env)

### Endpoints

There are two types of endpoint:
 - _Spotify_ - Ones that the real Spotify API provides (e.g. GET /v1/me/playlists)
 - _Helper_ - Ones that the real Spotify API does not provide (e.g. POST /signup). These make it easier to test edge cases and create standalone tests.

You should be careful not use the _helper_ endpoints outside of your tests.

## Isn't there already a mock of the Spotify API?

All I've found is:
 - https://github.com/0xNF/mockify - This doesn't yet implement any of the Spotify API.

## Is it a mock, stub, spy, dummy, service virtualisation, fake, etc?

There are lots of different definitions for these. I've used the one from https://martinfowler.com/articles/mocksArentStubs.html

## Limitations

Alot. 

- Only implements the authorize endpoint and some of the playlist and user APIs.  
- Does not use scopes provided in /authorize
- Unable to set default token expiry time for a user
- Unable to say a user will reject a authorization request
- Does not persist data outside of memory

## Development

### IDE

I've used [vscode](https://code.visualstudio.com/download) with the following plugins:
 - DotENV
 - Git History
 - Mocha sidebar
 - npm
 - Prettier
 - Switch to test
 - TSLint
 - TypeScript Hero

And the following in my User Settings json:

````json
  "createTest.srcFolder": "src",
  "createTest.testFolder": "tests/unit",
  "createTest.testFileExtension": ".spec.ts",
  "createTest.testFileTemplate": [
    "import ${moduleName} from '${modulePath}';",
    "import { expect } from 'chai';",
    "",
    "describe('${moduleName}', () => {",
    "  it('does something', () => {",
    "  ",
    "  });",
    "});"
  ],
  "mocha.requires": ["ts-node/register"],
  "mocha.files.glob": "./tests/**/*.spec.ts",
    "javascript.preferences.quoteStyle": "single",
  "prettier.singleQuote": true,
  "prettier.tabWidth": 2,
  "prettier.trailingComma": "es5",
  "prettier.tslintIntegration": true,
  "typescript.preferences.quoteStyle": "single",
  "typescriptHero.imports.organizeOnSave": true,
  "editor.tabSize": 2
  ````

### Dependencies

#### Dev and production
 - [body-parser](https://www.npmjs.com/package/body-parser) - How to parse the body of requests
 - [cookie-parser](https://www.npmjs.com/package/cookie-parser) - Easy access to cookies from express
 - [dotenv](https://www.npmjs.com/package/dotenv) - Set environment variables locally
 - [express](https://www.npmjs.com/package/express) - Minimal web framework
 - [express-joi-validation](https://www.npmjs.com/package/express-joi-validation) - Add [middleware](https://expressjs.com/en/guide/using-middleware.html) to express to validate calls to the API
 - [inversify](https://www.npmjs.com/package/inversify) - Dependency injection
 - [inversify-express-utils](https://www.npmjs.com/package/inversify-express-utils) - Dependency injection plugged in to express.
 - [joi](https://www.npmjs.com/package/joi) - Easy to code validation
 - [reflect-metadata](https://www.npmjs.com/package/reflect-metadata) - Used by inversify to add meta data to classes/functions
 - [uuid](https://www.npmjs.com/package/uuid) - Generate UUIDs

#### Dev-only
 - [chai](https://www.npmjs.com/package/chai) - Testing library
 - [chai-http](https://www.npmjs.com/package/chai-http) - Plugin for Chai to allow a server to be started and tested against.
 - [env-test](https://www.npmjs.com/package/env-test) - Automatically make tests run with `process.env.NODE_ENV = "test"`
 - [mocha](https://www.npmjs.com/package/mocha) - Testing framework
 - [mocha-parallel-tests](https://www.npmjs.com/package/mocha-parallel-tests) - Allow each test file to be run in parallel to lower total test time.
 - [nodemon](https://www.npmjs.com/package/nodemon) - Monitor files for changes and automatically rebuild the project
 - [ts-lint](https://www.npmjs.com/package/ts-lint) - Make sure Typescript is formatted correctly for good readability and code that is less error prone.
 - [ts-node](https://www.npmjs.com/package/ts-node) - Run node without having to compile to JS each time (works well with nodemon)
 - [tslint](https://www.npmjs.com/package/tslint) - See ts-lint above. Can't remember why I've installed both of these
 - [typescript](https://www.npmjs.com/package/typescript) - Add types to Javascript. Less error-prone and easier to work with (helps the IDE out with auto-completion)

### Getting started

The best place to start is in the [src/controllers](src/controllers) folder. This is where incoming requests are handled. 

### Testing

Run `npm run test` to run all the tests

### Watch

Run `npm run watch` to run the application and watch for changes in source files, which will be automatically built in to the application.

### Notes

 - Add `/* tslint:disable:no-unused-expression */` to the top of test files. I've tried to disable this for test files but failed. I tried:
    - [no-unused-expression-chai](https://www.npmjs.com/package/tslint-no-unused-expression-chai)
    - [stack overflow](https://stackoverflow.com/questions/37558795/nice-way-to-get-rid-of-no-unused-expressions-linter-error-with-chai)

---
_Source: https://npm.io/package/fake-spotify-api · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
