Skip to content

Commit

Permalink
updating unit tests + docs
Browse files Browse the repository at this point in the history
  • Loading branch information
lleadbet committed Jun 14, 2021
1 parent 8f129fd commit 647a3b8
Show file tree
Hide file tree
Showing 58 changed files with 2,518 additions and 427 deletions.
174 changes: 174 additions & 0 deletions docs/mock-api.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
# mock-api

- [mock-api](#mock-api)
- [Description](#description)
- [generate](#generate)
- [start](#start)
- [mock namespace](#mock-namespace)
- [units namespace](#units-namespace)
- [auth namespace](#auth-namespace)

## Description

The `mock-api` product has two primary functions. The first is to generate so-called `units`- those are core building blocks on Twitch. These are, for now:

* Application Clients
* Categories
* Streams
* Subscriptions
* Tags
* Teams
* Users

The second is the actual server used to mock the endpoints. In the next iteration, you will be able to edit these and add further ones manually (for example, making a user with specific attributes), but for the beta we won't be providing this functionality as the current `generate` feature will make all of these (and more),


## generate

This command will generate a specified number of users with associated relationships (e.g. subscriptions/mods/blocks).

**Args**

None.

**Flags**

| Flag | Shorthand | Description | Example | Required? (Y/N) |
|-----------|-----------|-----------------------------------------------------------------------------|---------|-----------------|
| `--count` | `-c` | Number of users to generate (and associated relationships). Defaults to 10. | `-c 25` | N |


## start

The `start` function starts a new mock server for use with testing functionality. Currently, this replicates a large majority of the current API endpoints on the new API, but are ommitting:

* GET /analytics/extensions
* GET /analytics/games
* GET /extensions/transactions
* POST /eventsub/subscriptions
* DELETE /eventsub/subscriptions
* GET /eventsub/subscriptions
* GET /users/extensions/list
* GET /users/extensions
* PUT /users/extensions
* GET /webhooks/subscriptions

For many of these, we are exploring how to better integrate this with existing features (for example, allowing events to be triggered on unit creation or otherwise), and for others, the value is minimal compared to the docs. All other endpoints should be currently supported, however it is possible to be out of date- if so, [please raise an issue](https://github.com/twitchdev/twitch-cli/issues).

To access these endpoints, you will point your code to `http://localhost:<port>/mock`, where port is either the default port (8080) or one you specified using the flag. For example, to access the users endpoint:

```sh
curl -i -H "Accept: application/json" http://localhost:8080/mock/users
```

For information on accessing those endpoints, please see [the documentation on the Developer site](https://dev.twitch.tv/docs/api/reference).

In total, there are three namespaces (top-level folder) that are used:

### mock namespace

Example URL: `http://localhost:8080/mock/users`

This namespace houses all mock endpoints. For information on accessing those endpoints, please see [the documentation on the Developer site](https://dev.twitch.tv/docs/api/reference).

### units namespace

Example URL: `http://localhost:8080/units/users`

This endpoint gives an unauthenticated peek into the list of units in the database- used for debugging or finding units for testing. Endpoints include:

* GET /categories
* GET /clients
* GET /streams
* GET /subscriptions
* GET /tags
* GET /teams
* GET /users
* GET /videos

More will be added in the future.

### auth namespace

This endpoint is a light implementation of OAuth, without support for OIDC. These endpoints are used to generate either an app access token or user token. The two endpoints are below, with documentation and examples using cURL. All tokens expire after 24 hours.

**POST /authorize**

This endpoint generates a user token, similar to OAuth authorization code.

| Query Parameter | Description | Example | Required? (Y/N) |
|-----------------|----------------------------------------------------------------------|--------------------------|-----------------|
| `client_id` | Application client ID, which is output by the `generate` command. | `?client_id=1234` | Y |
| `client_secret` | Application client secret, which is output by the `generate` command | `?client_secret=1234` | Y |
| `grant_type` | Must be `user_token` | `?grant_type=user_token` | Y |
| `user_id` | User to get the token for. | `?user_id=1234` | Y |
| `scope` | Space seperated list of scopes to request for the given user. | `?scope=bits:read` | N |

The response is identical to the OAuth `authorization_code` with the omission of a refresh token.

Example request for user 78910 with no scopes:

```sh
curl -X POST http://localhost:8080/auth/authorize?client_id=123&client_secret=456&grant_type=user_token&user_id=78910
```

Example response:

```json
{
"access_token": "ff4231a5befca12",
"refresh_token": "",
"expires_in": 86399,
"scope": [],
"token_type": "bearer"
}
```

Docs: https://dev.twitch.tv/docs/authentication/getting-tokens-oauth#oauth-authorization-code-flow

**POST /token**

This endpoint generates an app access token using the `client_credentials` flow as documented.


| Query Parameter | Description | Example | Required? (Y/N) |
|-----------------|----------------------------------------------------------------------|--------------------------|-----------------|
| `client_id` | Application client ID, which is output by the `generate` command. | `?client_id=1234` | Y |
| `client_secret` | Application client secret, which is output by the `generate` command | `?client_secret=1234` | Y |
| `grant_type` | Must be `client_credentials` | `?grant_type=user_token` | Y |
| `scope` | Space seperated list of scopes to request for the given user. | `?scope=bits:read` | N |


The response is identical to the OAuth `client_credentials` flow with the omission of a refresh token.

Example request with no scopes:

```sh
curl -X POST http://localhost:8080/auth/token?client_id=123&client_secret=456&grant_type=client_credentials
```

Example response:

```json
{
"access_token": "4f5dce6cea626cb",
"refresh_token": "",
"expires_in": 86399,
"scope": [],
"token_type": "bearer"
}
```

Docs: https://dev.twitch.tv/docs/authentication/getting-tokens-oauth#oauth-client-credentials-flow

**Args**

None.

**Flags**

| Flag | Shorthand | Description | Example | Required? (Y/N) |
|----------|-----------|------------------------------------------|-----------|-----------------|
| `--port` | `-p` | Port number to use with the mock server. | `-p 8000` | N |


23 changes: 18 additions & 5 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,27 @@ go 1.14

require (
github.com/TylerBrock/colorjson v0.0.0-20200706003622-8a50f05110d2
github.com/fatih/color v1.10.0
github.com/fatih/color v1.12.0
github.com/fsnotify/fsnotify v1.4.9 // indirect
github.com/hokaccha/go-prettyjson v0.0.0-20201222001619-a42f9ac2ec8e // indirect
github.com/jmoiron/sqlx v1.3.3
github.com/jmoiron/sqlx v1.3.4
github.com/lunixbochs/vtclean v1.0.0 // indirect
github.com/magiconair/properties v1.8.5 // indirect
github.com/manifoldco/promptui v0.8.0
github.com/mattn/go-isatty v0.0.13 // indirect
github.com/mattn/go-sqlite3 v1.14.7
github.com/mitchellh/go-homedir v1.1.0
github.com/spf13/cobra v1.1.1
github.com/mitchellh/mapstructure v1.4.1 // indirect
github.com/pelletier/go-toml v1.9.2 // indirect
github.com/spf13/afero v1.6.0 // indirect
github.com/spf13/cast v1.3.1 // indirect
github.com/spf13/cobra v1.1.3
github.com/spf13/jwalterweatherman v1.1.0 // indirect
github.com/spf13/viper v1.7.1
github.com/stretchr/testify v1.6.1
golang.org/x/time v0.0.0-20190308202827-9d24e82272b4
github.com/stretchr/testify v1.7.0
golang.org/x/sys v0.0.0-20210611083646-a4fc73990273 // indirect
golang.org/x/text v0.3.6 // indirect
golang.org/x/time v0.0.0-20210611083556-38a9dc6acbc6
gopkg.in/ini.v1 v1.62.0 // indirect
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
)
Loading

0 comments on commit 647a3b8

Please sign in to comment.