Mailspring/spec/stores/identity-store-spec.es6

76 lines
2.7 KiB
Text
Raw Normal View History

feat(usage): Add a FeatureUsageStore and move Identity to the DB Summary: This is a WIP Depends on D3799 on billing.nylas.com This adds a `FeatureUsageStore` which determines whether a feature can be used or not. It also allows us to record "using" a feature. Feature Usage is ultimately backed by the Nylas Identity and cached locally in the Identity object. Since feature usage is attached to the Nylas Identity, we move the whole Identity object (except for the ID) to the database. This includes a migration (with tests!) to move the Nylas Identity from the config into the Database. We still, however, need the Nylas ID to stay in the config so it can be synchronously accessed by the /browser process on bootup when determining what windows to show. It's also convenient to know what the Nylas ID is by looking at the config. There's logic (with tests!) to make sure these stay in sync. If you delete the Nylas ID from the config, it'll be the same as logging you out. The schema for the feature usage can be found in more detail on D3799. By the time it reaches Nylas Mail, the Nylas ID object has a `feature_usage` attribute that has each feature (keyed by the feature name) and information about the plans attached to it. The schema Nylas Mail sees looks like: ``` "feature_usage": { "snooze": { quota: 10, peroid: 'monthly', used_in_period: 8, feature_limit_name: 'Snooze Group A', }, } ``` See D3799 for more info about how these are generated. One final change that's in here is how Stores are loaded. Most of our core stores are loaded at require time, but now things like the IdentityStore need to do asynchronous things on activation. In reality most of our stores do this and it's a miracle it hasn't caused more problems! Now when stores activate we optionally look for an `activate` method and `await` for it. This was necessary so downstream classes (like the Onboarding Store), see a fully initialized IdentityStore by the time it's time to use them Test Plan: New tests! Reviewers: khamidou, juan, halla Reviewed By: juan Differential Revision: https://phab.nylas.com/D3808
2017-02-04 07:31:31 +08:00
import {ipcRenderer} from 'electron';
import {KeyManager, DatabaseTransaction, SendFeatureUsageEventTask} from 'nylas-exports'
import IdentityStore from '../../src/flux/stores/identity-store'
const TEST_NYLAS_ID = "icihsnqh4pwujyqihlrj70vh"
const TEST_TOKEN = "test-token"
describe("IdentityStore", function identityStoreSpec() {
beforeEach(() => {
this.identityJSON = {
valid_until: 1500093224,
firstname: "Nylas 050",
lastname: "Test",
free_until: 1500006814,
email: "nylas050test@evanmorikawa.com",
id: TEST_NYLAS_ID,
seen_welcome_page: true,
}
});
it("logs out of nylas identity properly", async () => {
IdentityStore._identity = this.identityJSON;
spyOn(NylasEnv.config, 'unset')
spyOn(KeyManager, "deletePassword")
spyOn(ipcRenderer, "send")
spyOn(DatabaseTransaction.prototype, "persistJSONBlob").andReturn(Promise.resolve())
const promise = IdentityStore._onLogoutNylasIdentity()
IdentityStore._onIdentityChanged(null)
return promise.then(() => {
expect(KeyManager.deletePassword).toHaveBeenCalled()
expect(ipcRenderer.send).toHaveBeenCalled()
expect(ipcRenderer.send.calls[0].args[1]).toBe("application:relaunch-to-initial-windows")
expect(DatabaseTransaction.prototype.persistJSONBlob).toHaveBeenCalled()
const ident = DatabaseTransaction.prototype.persistJSONBlob.calls[0].args[1]
expect(ident).toBe(null)
})
});
it("can log a feature usage event", () => {
spyOn(IdentityStore, "nylasIDRequest");
spyOn(IdentityStore, "saveIdentity");
IdentityStore._identity = this.identityJSON
IdentityStore._identity.token = TEST_TOKEN;
IdentityStore._onEnvChanged()
const t = new SendFeatureUsageEventTask("snooze");
t.performRemote()
const opts = IdentityStore.nylasIDRequest.calls[0].args[0]
expect(opts).toEqual({
method: "POST",
url: "https://billing.nylas.com/n1/user/feature_usage_event",
body: {
feature_name: 'snooze',
},
})
});
describe("returning the identity object", () => {
it("returns the identity as null if it looks blank", () => {
IdentityStore._identity = null;
expect(IdentityStore.identity()).toBe(null);
IdentityStore._identity = {};
expect(IdentityStore.identity()).toBe(null);
IdentityStore._identity = {token: 'bad'};
expect(IdentityStore.identity()).toBe(null);
});
it("returns a proper clone of the identity", () => {
IdentityStore._identity = {id: 'bar', deep: {obj: 'baz'}};
const ident = IdentityStore.identity();
IdentityStore._identity.deep.obj = 'changed';
expect(ident.deep.obj).toBe('baz');
});
});
});