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';
|
2017-02-11 04:52:57 +08:00
|
|
|
import {Utils, KeyManager, DatabaseTransaction, SendFeatureUsageEventTask} from 'nylas-exports'
|
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 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,
|
2017-02-11 04:52:57 +08:00
|
|
|
feature_usage: {
|
|
|
|
feat: {
|
|
|
|
quota: 10,
|
|
|
|
used_in_period: 1,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
token: "secret token",
|
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
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2017-02-11 04:52:57 +08:00
|
|
|
describe("testing saveIdentity", () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
IdentityStore._identity = this.identityJSON;
|
|
|
|
spyOn(KeyManager, "deletePassword")
|
|
|
|
spyOn(KeyManager, "replacePassword")
|
|
|
|
spyOn(DatabaseTransaction.prototype, "persistJSONBlob").andReturn(Promise.resolve())
|
|
|
|
spyOn(ipcRenderer, "send")
|
|
|
|
spyOn(IdentityStore, "trigger")
|
|
|
|
});
|
|
|
|
|
|
|
|
it("logs out of nylas identity properly", async () => {
|
|
|
|
spyOn(NylasEnv.config, 'unset')
|
|
|
|
const promise = IdentityStore._onLogoutNylasIdentity()
|
|
|
|
IdentityStore._onIdentityChanged(null)
|
|
|
|
return promise.then(() => {
|
|
|
|
expect(KeyManager.deletePassword).toHaveBeenCalled()
|
|
|
|
expect(KeyManager.replacePassword).not.toHaveBeenCalled()
|
|
|
|
expect(ipcRenderer.send).toHaveBeenCalled()
|
|
|
|
expect(ipcRenderer.send.calls[0].args[1]).toBe("onIdentityChanged")
|
|
|
|
expect(DatabaseTransaction.prototype.persistJSONBlob).toHaveBeenCalled()
|
|
|
|
const ident = DatabaseTransaction.prototype.persistJSONBlob.calls[0].args[1]
|
|
|
|
expect(ident).toBe(null)
|
|
|
|
expect(IdentityStore.trigger).toHaveBeenCalled()
|
|
|
|
})
|
|
|
|
});
|
|
|
|
|
|
|
|
it("makes the Identity synchronously available for fetching right after saving the identity", async () => {
|
|
|
|
const used = () => {
|
|
|
|
return IdentityStore.identity().feature_usage.feat.used_in_period
|
|
|
|
}
|
|
|
|
expect(used()).toBe(1)
|
|
|
|
const t = new SendFeatureUsageEventTask('feat');
|
|
|
|
await t.performLocal()
|
|
|
|
expect(used()).toBe(2)
|
|
|
|
expect(ipcRenderer.send).not.toHaveBeenCalled()
|
|
|
|
expect(IdentityStore.trigger).toHaveBeenCalled()
|
|
|
|
});
|
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
|
|
|
});
|
|
|
|
|
2017-02-11 04:52:57 +08:00
|
|
|
|
2017-02-10 06:51:23 +08:00
|
|
|
it("can log a feature usage event", async () => {
|
2017-02-11 04:52:57 +08:00
|
|
|
spyOn(IdentityStore, "saveIdentity").andReturn(Promise.resolve());
|
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
|
|
|
spyOn(IdentityStore, "nylasIDRequest");
|
|
|
|
IdentityStore._identity = this.identityJSON
|
|
|
|
IdentityStore._identity.token = TEST_TOKEN;
|
|
|
|
IdentityStore._onEnvChanged()
|
|
|
|
const t = new SendFeatureUsageEventTask("snooze");
|
2017-02-10 06:51:23 +08:00
|
|
|
await t.performRemote()
|
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
|
|
|
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", () => {
|
2017-02-11 04:52:57 +08:00
|
|
|
beforeEach(() => {
|
|
|
|
spyOn(IdentityStore, "saveIdentity").andReturn(Promise.resolve());
|
|
|
|
});
|
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
|
|
|
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');
|
|
|
|
});
|
|
|
|
});
|
2017-02-11 04:52:57 +08:00
|
|
|
|
|
|
|
describe("_fetchIdentity", () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
IdentityStore._identity = this.identityJSON;
|
|
|
|
spyOn(IdentityStore, "saveIdentity")
|
|
|
|
spyOn(NylasEnv, "reportError")
|
|
|
|
spyOn(console, "error")
|
|
|
|
});
|
|
|
|
|
|
|
|
it("saves the identity returned", async () => {
|
|
|
|
const resp = Utils.deepClone(this.identityJSON);
|
|
|
|
resp.feature_usage.feat.quota = 5
|
|
|
|
spyOn(IdentityStore, "nylasIDRequest").andCallFake(() => {
|
|
|
|
return Promise.resolve(resp)
|
|
|
|
})
|
|
|
|
await IdentityStore._fetchIdentity();
|
|
|
|
expect(IdentityStore.nylasIDRequest).toHaveBeenCalled();
|
|
|
|
const options = IdentityStore.nylasIDRequest.calls[0].args[0]
|
|
|
|
expect(options.url).toMatch(/\/n1\/user/)
|
|
|
|
expect(IdentityStore.saveIdentity).toHaveBeenCalled()
|
|
|
|
const newIdent = IdentityStore.saveIdentity.calls[0].args[0]
|
|
|
|
expect(newIdent.feature_usage.feat.quota).toBe(5)
|
|
|
|
expect(NylasEnv.reportError).not.toHaveBeenCalled()
|
|
|
|
});
|
|
|
|
|
|
|
|
it("errors if the json is invalid", async () => {
|
|
|
|
spyOn(IdentityStore, "nylasIDRequest").andCallFake(() => {
|
|
|
|
return Promise.resolve({})
|
|
|
|
})
|
|
|
|
await IdentityStore._fetchIdentity();
|
|
|
|
expect(NylasEnv.reportError).toHaveBeenCalled()
|
|
|
|
expect(IdentityStore.saveIdentity).not.toHaveBeenCalled()
|
|
|
|
});
|
|
|
|
|
|
|
|
it("errors if the json doesn't match the ID", async () => {
|
|
|
|
const resp = Utils.deepClone(this.identityJSON);
|
|
|
|
resp.id = "THE WRONG ID"
|
|
|
|
spyOn(IdentityStore, "nylasIDRequest").andCallFake(() => {
|
|
|
|
return Promise.resolve(resp)
|
|
|
|
})
|
|
|
|
await IdentityStore._fetchIdentity();
|
|
|
|
expect(NylasEnv.reportError).toHaveBeenCalled()
|
|
|
|
expect(IdentityStore.saveIdentity).not.toHaveBeenCalled()
|
|
|
|
});
|
|
|
|
});
|
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
|
|
|
});
|