Skip to content

Commit 98fba6b

Browse files
authored
fix: always reject submissions (#2952)
1 parent 48b8eef commit 98fba6b

File tree

4 files changed

+31
-477
lines changed

4 files changed

+31
-477
lines changed

__tests__/__snapshots__/submissions.ts.snap

Lines changed: 0 additions & 16 deletions
This file was deleted.

__tests__/submissions.ts

Lines changed: 6 additions & 323 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,4 @@
1-
import {
2-
ArticlePost,
3-
Source,
4-
User,
5-
Submission,
6-
SubmissionStatus,
7-
} from '../src/entity';
1+
import { User, Submission } from '../src/entity';
82
import {
93
disposeGraphQLTesting,
104
GraphQLTestClient,
@@ -14,14 +8,10 @@ import {
148
saveFixtures,
159
testMutationErrorCode,
1610
} from './helpers';
17-
import { subDays } from 'date-fns';
18-
import { sourcesFixture } from './fixture/source';
1911
import { SubmissionFailErrorMessage } from '../src/errors';
2012
import { DataSource } from 'typeorm';
2113
import createOrGetConnection from '../src/db';
22-
import { utcToZonedTime, zonedTimeToUtc } from 'date-fns-tz';
2314
import { badUsersFixture, usersFixture } from './fixture';
24-
import { DEFAULT_SUBMISSION_LIMIT } from '../src/config';
2515

2616
let con: DataSource;
2717
let state: GraphQLTestingState;
@@ -63,99 +53,16 @@ describe('query submissionAvailability', () => {
6353
}
6454
`;
6555

66-
it('should return default values if not logged in', async () => {
56+
it('should always return default values because of deprecation', async () => {
6757
loggedUser = '0';
6858

6959
const res = await client.query(QUERY);
70-
const limit = parseInt(
71-
process.env.SCOUT_SUBMISSION_LIMIT || DEFAULT_SUBMISSION_LIMIT,
72-
);
60+
7361
expect(res.errors).toBeFalsy();
74-
expect(res.data.submissionAvailability.limit).toEqual(limit);
62+
expect(res.data.submissionAvailability.limit).toEqual(0);
7563
expect(res.data.submissionAvailability.hasAccess).toEqual(false);
7664
expect(res.data.submissionAvailability.todaySubmissionsCount).toEqual(0);
7765
});
78-
79-
it('should return submissions count today, limit, and if has access', async () => {
80-
loggedUser = '1';
81-
const repo = con.getRepository(Submission);
82-
await repo.save([
83-
{ url: 'http://abc.com/1', userId: '1' },
84-
{
85-
url: 'http://abc.com/2',
86-
userId: '1',
87-
createdAt: subDays(new Date(), 1),
88-
},
89-
]);
90-
const res = await client.query(QUERY);
91-
const limit = parseInt(
92-
process.env.SCOUT_SUBMISSION_LIMIT || DEFAULT_SUBMISSION_LIMIT,
93-
);
94-
expect(res.errors).toBeFalsy();
95-
expect(res.data.submissionAvailability.limit).toEqual(limit);
96-
expect(res.data.submissionAvailability.hasAccess).toEqual(true);
97-
expect(res.data.submissionAvailability.todaySubmissionsCount).toEqual(1);
98-
});
99-
100-
it('should return submissions count today, limit, and if has access in consideration of timezone', async () => {
101-
loggedUser = '1';
102-
await con.getRepository(User).save({
103-
id: '2',
104-
name: 'Hansel',
105-
image: 'https://daily.dev/hansel.jpg',
106-
reputation: 250,
107-
});
108-
await con.getRepository(User).save({
109-
id: '3',
110-
name: 'Solevilla',
111-
image: 'https://daily.dev/solevilla.jpg',
112-
reputation: 250,
113-
});
114-
await con
115-
.getRepository(User)
116-
.update({ id: '1' }, { timezone: 'Europe/Oslo' });
117-
const repo = con.getRepository(Submission);
118-
await repo.save([
119-
{ url: 'http://abc.com/1', userId: '2' },
120-
{ url: 'http://abc.com/2', userId: '3' },
121-
122-
// 20:00 at UTC is 21:00 at Europe/Oslo (22:00 during summer)
123-
// So the submission should be counted as yesterday
124-
{
125-
url: 'http://abc.com/3',
126-
userId: '1',
127-
createdAt: subDays(new Date().setHours(20), 1),
128-
},
129-
130-
// 23:00 at UTC is 01:00 at Europe/Oslo (02:00 during summer)
131-
// So the submission should be counted as the next day
132-
{
133-
url: 'http://abc.com/4',
134-
userId: '1',
135-
createdAt: subDays(new Date().setHours(23), 1),
136-
},
137-
{
138-
url: 'http://abc.com/4.midnight',
139-
userId: '1',
140-
createdAt: zonedTimeToUtc(
141-
utcToZonedTime(new Date().setHours(23), 'Europe/Oslo'),
142-
'Europe/Oslo',
143-
),
144-
},
145-
{
146-
url: 'http://abc.com/5',
147-
userId: '1',
148-
},
149-
]);
150-
const res = await client.query(QUERY);
151-
const limit = parseInt(
152-
process.env.SCOUT_SUBMISSION_LIMIT || DEFAULT_SUBMISSION_LIMIT,
153-
);
154-
expect(res.errors).toBeFalsy();
155-
expect(res.data.submissionAvailability.limit).toEqual(limit);
156-
expect(res.data.submissionAvailability.hasAccess).toEqual(true);
157-
expect(res.data.submissionAvailability.todaySubmissionsCount).toEqual(2);
158-
});
15966
});
16067

16168
describe('mutation submitArticle', () => {
@@ -183,7 +90,7 @@ describe('mutation submitArticle', () => {
18390
'UNAUTHENTICATED',
18491
));
18592

186-
it('should invalidate if the url was requested already', async () => {
93+
it('should always return rejection because of deprecation', async () => {
18794
loggedUser = '1';
18895
const request = 'https://abc.com/article';
18996
const repo = con.getRepository(Submission);
@@ -194,234 +101,10 @@ describe('mutation submitArticle', () => {
194101
expect(res.data).toEqual({
195102
submitArticle: {
196103
result: 'rejected',
197-
reason: SubmissionFailErrorMessage.EXISTS_STARTED,
198-
post: null,
199-
submission: null,
200-
},
201-
});
202-
});
203-
204-
it('should invalidate if the user has reached limit', async () => {
205-
loggedUser = '1';
206-
const request = 'https://abc.com/article';
207-
const repo = con.getRepository(Submission);
208-
await repo.save(repo.create({ url: `${request}1`, userId: loggedUser }));
209-
await repo.save(repo.create({ url: `${request}2`, userId: loggedUser }));
210-
await repo.save(repo.create({ url: `${request}3`, userId: loggedUser }));
211-
await repo.save(repo.create({ url: `${request}4`, userId: loggedUser }));
212-
await repo.save(repo.create({ url: `${request}5`, userId: loggedUser }));
213-
214-
const res = await client.mutate(MUTATION, { variables: { url: request } });
215-
expect(res.errors).toBeFalsy();
216-
expect(res.data).toEqual({
217-
submitArticle: {
218-
result: 'rejected',
219-
reason: SubmissionFailErrorMessage.LIMIT_REACHED,
220-
post: null,
221-
submission: null,
222-
},
223-
});
224-
});
225-
226-
it('should not invalidate if the user has reached limit but is team member', async () => {
227-
loggedUser = '1';
228-
state = await initializeGraphQLTesting(
229-
(req) => new MockContext(con, loggedUser, [], req, true),
230-
);
231-
const request = 'https://abc.com/article';
232-
const repo = con.getRepository(Submission);
233-
await repo.save(repo.create({ url: `${request}1`, userId: loggedUser }));
234-
await repo.save(repo.create({ url: `${request}2`, userId: loggedUser }));
235-
await repo.save(repo.create({ url: `${request}3`, userId: loggedUser }));
236-
await repo.save(repo.create({ url: `${request}4`, userId: loggedUser }));
237-
await repo.save(repo.create({ url: `${request}5`, userId: loggedUser }));
238-
239-
const res = await state.client.mutate(MUTATION, {
240-
variables: { url: request },
241-
});
242-
expect(res.errors).toBeFalsy();
243-
const submission = await con
244-
.getRepository(Submission)
245-
.findOneByOrFail({ url: request });
246-
247-
expect(submission.status).toEqual(SubmissionStatus.Started);
248-
expect(res.data.submitArticle.submission).toEqual({
249-
id: submission.id,
250-
status: 'STARTED',
251-
userId: '1',
252-
});
253-
});
254-
255-
it('should invalidate if the user is ineligible for submission', async () => {
256-
loggedUser = '1';
257-
await con.getRepository(User).update({ id: '1' }, { reputation: 10 });
258-
const request = 'https://abc.com/article';
259-
const res = await client.mutate(MUTATION, { variables: { url: request } });
260-
expect(res.errors).toBeFalsy();
261-
expect(res.data).toEqual({
262-
submitArticle: {
263-
result: 'rejected',
264-
reason: SubmissionFailErrorMessage.ACCESS_DENIED,
265-
post: null,
266-
submission: null,
267-
},
268-
});
269-
});
270-
271-
it('should reject if the post already exists', async () => {
272-
loggedUser = '1';
273-
const request = 'http://p1.com';
274-
await saveFixtures(con, Source, sourcesFixture);
275-
await saveFixtures(con, ArticlePost, [
276-
{
277-
id: 'p1',
278-
shortId: 'sp1',
279-
title: 'Post 1',
280-
url: request,
281-
canonicalUrl: request,
282-
score: 0,
283-
sourceId: 'a',
284-
createdAt: new Date('2021-09-22T07:15:51.247Z'),
285-
tagsStr: 'javascript,webdev',
286-
},
287-
]);
288-
289-
const res = await client.mutate(MUTATION, { variables: { url: request } });
290-
expect(res.errors).toBeFalsy();
291-
expect(res.data).toEqual({
292-
submitArticle: {
293-
result: 'exists',
294-
reason: null,
295-
post: {
296-
id: 'p1',
297-
},
298-
submission: null,
299-
},
300-
});
301-
});
302-
303-
it('should reject if the post was deleted', async () => {
304-
loggedUser = '1';
305-
const request = 'http://p8.com';
306-
await saveFixtures(con, Source, sourcesFixture);
307-
await saveFixtures(con, ArticlePost, [
308-
{
309-
id: 'pdeleted',
310-
shortId: 'spdeleted',
311-
title: 'PDeleted',
312-
url: request,
313-
canonicalUrl: request,
314-
score: 0,
315-
sourceId: 'a',
316-
createdAt: new Date('2021-09-22T07:15:51.247Z'),
317-
tagsStr: 'javascript,webdev',
318-
deleted: true,
319-
},
320-
]);
321-
322-
const res = await client.mutate(MUTATION, { variables: { url: request } });
323-
expect(res.errors).toBeFalsy();
324-
expect(res.data).toEqual({
325-
submitArticle: {
326-
result: 'rejected',
327-
reason: SubmissionFailErrorMessage.POST_DELETED,
104+
reason: SubmissionFailErrorMessage.COMMUNITY_PICKS_DEPRECATED,
328105
post: null,
329106
submission: null,
330107
},
331108
});
332109
});
333-
334-
it('should reject if the post exist but not visible', async () => {
335-
loggedUser = '1';
336-
const request = 'http://p8.com';
337-
await saveFixtures(con, Source, sourcesFixture);
338-
await saveFixtures(con, ArticlePost, [
339-
{
340-
id: 'pdeleted',
341-
shortId: 'spdeleted',
342-
title: 'PDeleted',
343-
url: request,
344-
canonicalUrl: request,
345-
score: 0,
346-
sourceId: 'a',
347-
createdAt: new Date('2021-09-22T07:15:51.247Z'),
348-
tagsStr: 'javascript,webdev',
349-
visible: false,
350-
},
351-
]);
352-
353-
const res = await client.mutate(MUTATION, { variables: { url: request } });
354-
expect(res.errors).toBeFalsy();
355-
expect(res.data).toEqual({
356-
submitArticle: {
357-
result: 'rejected',
358-
reason: SubmissionFailErrorMessage.POST_DELETED,
359-
post: null,
360-
submission: null,
361-
},
362-
});
363-
});
364-
365-
it('should not allow invalid urls', async () => {
366-
loggedUser = '1';
367-
const request = 'test/sample/url';
368-
const res = await client.mutate(MUTATION, { variables: { url: request } });
369-
expect(res.errors).toBeFalsy();
370-
expect(res.data).toEqual({
371-
submitArticle: {
372-
result: 'rejected',
373-
reason: SubmissionFailErrorMessage.INVALID_URL,
374-
post: null,
375-
submission: null,
376-
},
377-
});
378-
});
379-
380-
it('should create a submission entity if the url is valid', async () => {
381-
loggedUser = '1';
382-
const request = 'https://daily.dev/amazing/article';
383-
const res = await client.mutate(MUTATION, { variables: { url: request } });
384-
expect(res.errors).toBeFalsy();
385-
const submission = await con
386-
.getRepository(Submission)
387-
.findOneBy({ url: request });
388-
expect(submission.status).toEqual(SubmissionStatus.Started);
389-
expect(res.data).toMatchSnapshot({
390-
submitArticle: {
391-
submission: {
392-
id: expect.any(String),
393-
},
394-
},
395-
});
396-
});
397-
398-
describe('vordr', () => {
399-
it('should set the correct vordr flags if the submission is from a good user', async () => {
400-
loggedUser = '1';
401-
const request = 'https://daily.dev/amazing/article';
402-
const res = await client.mutate(MUTATION, {
403-
variables: { url: request },
404-
});
405-
expect(res.errors).toBeFalsy();
406-
const submission = await con
407-
.getRepository(Submission)
408-
.findOneByOrFail({ url: request });
409-
expect(submission.status).toEqual(SubmissionStatus.Started);
410-
expect(submission.flags.vordr).toEqual(false);
411-
});
412-
413-
it('should set the correct vordr flags if the submission is from a bad user', async () => {
414-
loggedUser = 'vordr';
415-
const request = 'https://daily.dev/amazing/article';
416-
const res = await client.mutate(MUTATION, {
417-
variables: { url: request },
418-
});
419-
expect(res.errors).toBeFalsy();
420-
const submission = await con
421-
.getRepository(Submission)
422-
.findOneByOrFail({ url: request });
423-
expect(submission.status).toEqual(SubmissionStatus.Started);
424-
expect(submission.flags.vordr).toEqual(true);
425-
});
426-
});
427110
});

0 commit comments

Comments
 (0)