Unverified Commit de7043b4 authored by Robert Knight's avatar Robert Knight Committed by GitHub

Merge pull request #943 from hypothesis/fix-isloggedin-bug

Use token instead of store.profile to calc isLoggedIn
parents 2ea51788 f55b5ae9
...@@ -27,6 +27,7 @@ function groups( ...@@ -27,6 +27,7 @@ function groups(
serviceUrl, serviceUrl,
session, session,
settings, settings,
auth,
features features
) { ) {
const svc = serviceConfig(settings); const svc = serviceConfig(settings);
...@@ -158,17 +159,23 @@ function groups( ...@@ -158,17 +159,23 @@ function groups(
}; };
const profileGroupsApi = api.profile.groups.read(profileParams); const profileGroupsApi = api.profile.groups.read(profileParams);
const listGroupsApi = api.groups.list(params); const listGroupsApi = api.groups.list(params);
return Promise.all([profileGroupsApi, listGroupsApi]).then( return Promise.all([
([myGroups, featuredGroups]) => profileGroupsApi,
combineGroups(myGroups, featuredGroups) listGroupsApi,
); auth.tokenGetter(),
]).then(([myGroups, featuredGroups, token]) => [
combineGroups(myGroups, featuredGroups),
token,
]);
} else { } else {
// Fetch groups from the API. // Fetch groups from the API.
return api.groups.list(params); return api.groups
.list(params, null, { includeMetadata: true })
.then(({ data, token }) => [data, token]);
} }
}) })
.then(groups => { .then(([groups, token]) => {
const isLoggedIn = store.profile().userid !== null; const isLoggedIn = token !== null;
const directLinkedAnnotation = settings.annotations; const directLinkedAnnotation = settings.annotations;
return filterGroups(groups, isLoggedIn, directLinkedAnnotation); return filterGroups(groups, isLoggedIn, directLinkedAnnotation);
}) })
......
...@@ -36,6 +36,7 @@ const dummyGroups = [ ...@@ -36,6 +36,7 @@ const dummyGroups = [
]; ];
describe('groups', function() { describe('groups', function() {
let fakeAuth;
let fakeFeatures; let fakeFeatures;
let fakeStore; let fakeStore;
let fakeIsSidebar; let fakeIsSidebar;
...@@ -48,6 +49,9 @@ describe('groups', function() { ...@@ -48,6 +49,9 @@ describe('groups', function() {
let sandbox; let sandbox;
beforeEach(function() { beforeEach(function() {
fakeAuth = {
tokenGetter: sinon.stub().returns('1234'),
};
fakeFeatures = { fakeFeatures = {
flagEnabled: sinon.stub().returns(false), flagEnabled: sinon.stub().returns(false),
}; };
...@@ -58,7 +62,6 @@ describe('groups', function() { ...@@ -58,7 +62,6 @@ describe('groups', function() {
searchUris: ['http://example.org'], searchUris: ['http://example.org'],
focusedGroup: null, focusedGroup: null,
groups: [], groups: [],
profile: sinon.stub().returns({ userid: 'userid' }),
}, },
{ {
focusGroup: sinon.stub(), focusGroup: sinon.stub(),
...@@ -79,9 +82,6 @@ describe('groups', function() { ...@@ -79,9 +82,6 @@ describe('groups', function() {
}, },
} }
); );
fakeStore.profile = () => {
return { userid: 'userid' };
};
fakeSession = sessionWithThreeGroups(); fakeSession = sessionWithThreeGroups();
fakeIsSidebar = true; fakeIsSidebar = true;
fakeLocalStorage = { fakeLocalStorage = {
...@@ -114,7 +114,12 @@ describe('groups', function() { ...@@ -114,7 +114,12 @@ describe('groups', function() {
}, },
}, },
groups: { groups: {
list: sandbox.stub().returns(Promise.resolve(dummyGroups)), list: sandbox.stub().returns(
Promise.resolve({
data: dummyGroups,
token: '1234',
})
),
}, },
profile: { profile: {
groups: { groups: {
...@@ -140,6 +145,7 @@ describe('groups', function() { ...@@ -140,6 +145,7 @@ describe('groups', function() {
fakeServiceUrl, fakeServiceUrl,
fakeSession, fakeSession,
fakeSettings, fakeSettings,
fakeAuth,
fakeFeatures fakeFeatures
); );
} }
...@@ -189,6 +195,9 @@ describe('groups', function() { ...@@ -189,6 +195,9 @@ describe('groups', function() {
it('sends `expand` parameter when community-groups feature flag is enabled', function() { it('sends `expand` parameter when community-groups feature flag is enabled', function() {
const svc = service(); const svc = service();
fakeApi.groups.list.returns(
Promise.resolve([{ id: 'groupa', name: 'GroupA' }])
);
fakeFeatures.flagEnabled.withArgs('community_groups').returns(true); fakeFeatures.flagEnabled.withArgs('community_groups').returns(true);
return svc.load().then(() => { return svc.load().then(() => {
...@@ -268,7 +277,12 @@ describe('groups', function() { ...@@ -268,7 +277,12 @@ describe('groups', function() {
it('injects a defalt organization if group is missing an organization', function() { it('injects a defalt organization if group is missing an organization', function() {
const svc = service(); const svc = service();
const groups = [{ id: '39r39f', name: 'Ding Dong!' }]; const groups = [{ id: '39r39f', name: 'Ding Dong!' }];
fakeApi.groups.list.returns(Promise.resolve(groups)); fakeApi.groups.list.returns(
Promise.resolve({
token: '1234',
data: groups,
})
);
return svc.load().then(groups => { return svc.load().then(groups => {
assert.isObject(groups[0].organization); assert.isObject(groups[0].organization);
assert.hasAllKeys(groups[0].organization, ['id', 'logo']); assert.hasAllKeys(groups[0].organization, ['id', 'logo']);
...@@ -303,10 +317,12 @@ describe('groups', function() { ...@@ -303,10 +317,12 @@ describe('groups', function() {
groups.push({ name: 'BioPub', id: 'biopub' }); groups.push({ name: 'BioPub', id: 'biopub' });
} }
fakeStore.profile = () => { fakeApi.groups.list.returns(
return { userid: loggedIn ? 'userid' : null }; Promise.resolve({
}; token: loggedIn ? '1234' : null,
fakeApi.groups.list.returns(Promise.resolve(groups)); data: groups,
})
);
return svc.load().then(groups => { return svc.load().then(groups => {
const publicGroupShown = groups.some(g => g.id === '__world__'); const publicGroupShown = groups.some(g => g.id === '__world__');
...@@ -359,7 +375,12 @@ describe('groups', function() { ...@@ -359,7 +375,12 @@ describe('groups', function() {
{ name: 'DEF', id: 'def456', groupid: null }, { name: 'DEF', id: 'def456', groupid: null },
]; ];
fakeApi.groups.list.returns(Promise.resolve(groups)); fakeApi.groups.list.returns(
Promise.resolve({
token: '1234',
data: groups,
})
);
return svc.load().then(groups => { return svc.load().then(groups => {
let displayedGroups = groups.map(g => g.id); let displayedGroups = groups.map(g => g.id);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment