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

Organize the source files in the OAuth package #1430

Merged
merged 4 commits into from Feb 22, 2022

Conversation

seratch
Copy link
Member

@seratch seratch commented Feb 21, 2022

Summary

As a preparation for my forthcoming pull requests, this patch splits the big index.ts file into smaller pieces of code. This change improves the maintainability of both the main code and test code.

The changes in this PR are:

  • Moved many parts from index.ts
    • Moved ClearStateState from the file to ./src/state-stores/clear-state-store.ts
    • Moved other public interface from the file to ./src/state-stores/interface.ts
    • Moved InstallationStore from the file to ./src/stores/interface.ts
    • Also, moved many tests from ./src/index.spec.js to relevant spec files
    • Moved InstallProvider, CallbackOptions, InstallURLOptions, InstallProviderOptions, Installation, InstallationQuery to dedicated files
    • Added tests for all of the above
    • The exports in src/index.ts are fully backward-compatible
  • Added a new test for logger.ts

I will rebase #1425 after merging this PR.

Requirements (place an x in each [ ])

@seratch seratch added enhancement M-T: A feature request for new functionality pkg:oauth applies to `@slack/oauth-helper` labels Feb 21, 2022
@seratch seratch added this to the oauth@2.5.0 milestone Feb 21, 2022
@seratch seratch self-assigned this Feb 21, 2022
Copy link
Member Author

@seratch seratch left a comment

Choose a reason for hiding this comment

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

comments for reviewers

@@ -33,7 +33,7 @@
"build:clean": "shx rm -rf ./dist ./coverage ./.nyc_output",
"lint": "eslint --ext .ts src",
"test": "npm run lint && npm run test:mocha",
"test:mocha": "nyc mocha --config .mocharc.json src/*.spec.js",
"test:mocha": "nyc mocha --config .mocharc.json src/*.spec.js src/**/*.spec.js src/*.spec.ts src/**/*.spec.ts",
Copy link
Member Author

Choose a reason for hiding this comment

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

As I've added more tests including the ones in TypeScript, this change is necessary for running all

@@ -61,7 +63,7 @@
"eslint-plugin-node": "^11.1.0",
"mocha": "^9.1.0",
"nop": "^1.0.0",
"nyc": "^14.1.1",
"nyc": "^15.1.0",
Copy link
Member Author

Choose a reason for hiding this comment

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

for fixing npm audit warnings

import { MissingStateError } from './errors';

describe('CallbackOptions', async () => {
it('should have success and failure', async () => {
Copy link
Member Author

Choose a reason for hiding this comment

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

These tests are not so great but it's better than nothing

Copy link
Contributor

Choose a reason for hiding this comment

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

Indeed, only about ~62% of branches are tested in this package, so definitely room for improvement!

@@ -631,232 +630,4 @@ describe('OAuth', async () => {
assert.equal(fakeStateStore.verifyStateParam.callCount, 0);
});
});

describe('MemoryInstallationStore', async () => {
Copy link
Member Author

Choose a reason for hiding this comment

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

These deleted tests are moved to more relevant files.

// This is intentionally structurally identical to AuthorizeResult from App
// It is redefined so that this class remains loosely coupled to the rest
// of Bolt.
export interface AuthorizeResult {
Copy link
Member Author

Choose a reason for hiding this comment

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

We may want to have dedicated files for these interface. However, I didn't do so at this moment because these are used only in InstallProvider. If we want to make it more granular, we can do so in future changes.

Copy link
Member Author

Choose a reason for hiding this comment

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

Also, these are exported in index.ts as with before

Copy link
Member Author

Choose a reason for hiding this comment

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

Revisited this and I came to think that AuthorizeResult should be authorize-result.ts as external code relies on this interface.

assert.match(newLogger.name, /test-logger-name:\d+/);
assert.equal(newLogger.getLevel(), LogLevel.DEBUG);
});
it('should modify the passed logger', async () => {
Copy link
Member Author

Choose a reason for hiding this comment

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

To make this clear, I've added this test

isEnterpriseInstall: false,
}

// TODO: valid tests with org-wide installations
Copy link
Member Author

Choose a reason for hiding this comment

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

We should add more tests for org-wide installations and token rotation use cases

@@ -30,6 +30,7 @@
],
"exclude": [
"src/**/*.spec.js",
"src/**/*.spec.ts",
Copy link
Member Author

Choose a reason for hiding this comment

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

Added a few TS tests. They should be excluded in the package file. I've verified if this works by running npm pack.

});

it('should store the latest installation', async () => {
const installationStore = new FileInstallationStore({ baseDir: './tmp' });
Copy link
Member Author

Choose a reason for hiding this comment

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

Changed the base directory for outputs - previously the tests used to generate directories in current dir.

Copy link
Contributor

Choose a reason for hiding this comment

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

Not necessary as a change but another option is to use os.tmpDir()

Copy link
Member Author

Choose a reason for hiding this comment

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

Oh, nice! will update this part.

Copy link
Contributor

@filmaj filmaj left a comment

Choose a reason for hiding this comment

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

LGTM! Ran it locally. Also much easier now to figure out where the missing code coverage is (via npm it && nyc report --reporter=text) - thanks a lot for doing this.

import { MissingStateError } from './errors';

describe('CallbackOptions', async () => {
it('should have success and failure', async () => {
Copy link
Contributor

Choose a reason for hiding this comment

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

Indeed, only about ~62% of branches are tested in this package, so definitely room for improvement!

});

it('should store the latest installation', async () => {
const installationStore = new FileInstallationStore({ baseDir: './tmp' });
Copy link
Contributor

Choose a reason for hiding this comment

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

Not necessary as a change but another option is to use os.tmpDir()

@seratch
Copy link
Member Author

seratch commented Feb 22, 2022

Thanks for your review @filmaj !

@seratch seratch merged commit 6f8fa31 into slackapi:main Feb 22, 2022
@seratch seratch deleted the organize-oauth-package branch February 22, 2022 22:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement M-T: A feature request for new functionality pkg:oauth applies to `@slack/oauth-helper`
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants