From a71bd0763d5f08f679586fdf93e39d53190cc256 Mon Sep 17 00:00:00 2001 From: Pavel Mineev Date: Tue, 7 Jan 2020 23:40:25 +0300 Subject: [PATCH] Allow voting for anonymous users --- frontend/app/common/static_store.ts | 1 + frontend/app/common/types.ts | 1 + .../app/components/comment/comment.test.tsx | 27 +++++++++++++++++++ frontend/app/components/comment/comment.tsx | 4 +-- frontend/app/testUtils/index.ts | 1 + 5 files changed, 32 insertions(+), 2 deletions(-) diff --git a/frontend/app/common/static_store.ts b/frontend/app/common/static_store.ts index f01295ea52..5c464722fd 100644 --- a/frontend/app/common/static_store.ts +++ b/frontend/app/common/static_store.ts @@ -27,6 +27,7 @@ export const StaticStore: StaticStoreType = { readonly_age: 0, max_image_size: 0, simple_view: false, + anon_vote: false, }, query: querySettings as QuerySettingsType, }; diff --git a/frontend/app/common/types.ts b/frontend/app/common/types.ts index 58ed9a8d58..125486b4a9 100644 --- a/frontend/app/common/types.ts +++ b/frontend/app/common/types.ts @@ -112,6 +112,7 @@ export interface Config { readonly_age: number; max_image_size: number; simple_view: boolean; + anon_vote: boolean; } export interface RemarkConfig { diff --git a/frontend/app/components/comment/comment.test.tsx b/frontend/app/components/comment/comment.test.tsx index 1ddfc38ec3..00d683e733 100644 --- a/frontend/app/components/comment/comment.test.tsx +++ b/frontend/app/components/comment/comment.test.tsx @@ -32,6 +32,33 @@ const DefaultProps: Partial = { describe('', () => { describe('voting', () => { + it('should be disabled for an anonymous user', () => { + const props = { ...DefaultProps, user: { id: 'anonymous_1' } } as Props; + const wrapper = shallow(); + const voteButtons = wrapper.find('.comment__vote'); + + expect(voteButtons.length).toEqual(2); + + voteButtons.forEach(button => { + expect(button.prop('aria-disabled')).toEqual('true'); + expect(button.prop('title')).toEqual("Anonymous users can't vote"); + }); + }); + + it('should be enabled for an anonymous user when it was allowed from server', () => { + StaticStore.config.anon_vote = true; + + const props = { ...DefaultProps, user: { id: 'anonymous_1' } } as Props; + const wrapper = shallow(); + const voteButtons = wrapper.find('.comment__vote'); + + expect(voteButtons.length).toEqual(2); + + voteButtons.forEach(button => { + expect(button.prop('aria-disabled')).toEqual('false'); + }); + }); + it('disabled on user info widget', () => { const element = mount(); diff --git a/frontend/app/components/comment/comment.tsx b/frontend/app/components/comment/comment.tsx index 1576ed1263..bfd5501248 100644 --- a/frontend/app/components/comment/comment.tsx +++ b/frontend/app/components/comment/comment.tsx @@ -354,7 +354,7 @@ export class Comment extends Component { if (this.isCurrentUser()) return "Can't vote for your own comment"; if (StaticStore.config.positive_score && this.props.data.score < 1) return 'Only positive score allowed'; if (this.isGuest()) return 'Sign in to vote'; - if (this.isAnonymous()) return "Anonymous users can't vote"; + if (this.isAnonymous() && !StaticStore.config.anon_vote) return "Anonymous users can't vote"; return null; }; @@ -367,7 +367,7 @@ export class Comment extends Component { if (this.props.data.delete) return "Can't vote for deleted comment"; if (this.isCurrentUser()) return "Can't vote for your own comment"; if (this.isGuest()) return 'Sign in to vote'; - if (this.isAnonymous()) return "Anonymous users can't vote"; + if (this.isAnonymous() && !StaticStore.config.anon_vote) return "Anonymous users can't vote"; return null; }; diff --git a/frontend/app/testUtils/index.ts b/frontend/app/testUtils/index.ts index a5bd673313..1177b38740 100644 --- a/frontend/app/testUtils/index.ts +++ b/frontend/app/testUtils/index.ts @@ -22,5 +22,6 @@ beforeEach(() => { readonly_age: 100, version: 'jest-test', simple_view: false, + anon_vote: false, }; });