Skip to content

Octokit hooks are not triggered for token requests #87

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

Open
ofhouse opened this issue Sep 3, 2022 · 0 comments
Open

Octokit hooks are not triggered for token requests #87

ofhouse opened this issue Sep 3, 2022 · 0 comments
Labels
Type: Bug Something isn't working as documented

Comments

@ofhouse
Copy link
Contributor

ofhouse commented Sep 3, 2022

What happened?

When using the authentication strategy together with @octokit/core and adding hooks, the hooks are not triggered for internal requests (e.g. to https://github.com/login/oauth/access_token):

import { Octokit } from '@octokit/core';
import { createOAuthUserAuth } from '@octokit/auth-oauth-user';
import fetchMock from 'fetch-mock';

describe('With Octokit', () => {
  test('hooks', async () => {
    const fm = fetchMock
      .sandbox()
      .postOnce('https://github.com/login/oauth/access_token', {
        body: {
          access_token: 'ghu_dummyToken',
          expires_in: 28800,
          refresh_token: 'ghr_dummyRefreshToken',
          refresh_token_expires_in: 15811200,
          scope: '',
          token_type: 'bearer',
        },
        headers: {
          date: new Date().toISOString(),
        },
      })
      .getOnce(
        {
          url: 'https://api.github.com/user',
          headers: { authorization: 'token ghu_dummyToken' },
        },
        {}
      );
    const octokit = new Octokit({
      authStrategy: createOAuthUserAuth,
      auth: {
        clientId: '1234567890abcdef1234',
        clientSecret: '1234567890abcdef1234567890abcdef12345678',
        code: 'code123',
      },
      request: {
        fetch: fm,
      },
    });

    const mockedErrorRequestHook = jest.fn();
    const mockedBeforeRequestHook = jest.fn();
    const mockedAfterRequestHook = jest.fn();
    const mockedWrapRequestHook = jest.fn((request, options) =>
      request(options)
    );
    octokit.hook.error('request', mockedErrorRequestHook);
    octokit.hook.before('request', mockedBeforeRequestHook);
    octokit.hook.after('request', mockedAfterRequestHook);
    octokit.hook.wrap('request', mockedWrapRequestHook);

    // Exchanges the code for the user access token authentication on first request
    // and caches the authentication for successive requests
    await octokit.request('GET /user');

    // First call to https://github.com/login/oauth/access_token
    // Second call to https://api.github.com/user
    expect(fm.calls().length).toBe(2); // passes
    expect(mockedErrorRequestHook).not.toHaveBeenCalled(); // passes
    expect(mockedBeforeRequestHook).toHaveBeenCalledTimes(2); // fails: 1
    expect(mockedAfterRequestHook).toHaveBeenCalledTimes(2); // fails: 1
    expect(mockedWrapRequestHook).toHaveBeenCalledTimes(2); // fails: 1
  });
});

x-ref: octokit/octokit.js#2294 (comment)

What did you expect to happen?

That the hooks are also called for the defined hooks, when internal requests are made.

@ofhouse ofhouse added the Type: Bug Something isn't working as documented label Sep 3, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Type: Bug Something isn't working as documented
Projects
None yet
Development

No branches or pull requests

1 participant