|
@@ -1,6 +1,10 @@
|
1
|
1
|
'use strict';
|
2
|
2
|
|
3
|
|
-const should = require('should');
|
|
3
|
+const path = require('path'),
|
|
4
|
+ should = require('should'),
|
|
5
|
+ sinon = require('sinon');
|
|
6
|
+
|
|
7
|
+const models = require(path.resolve('./models'));
|
4
|
8
|
|
5
|
9
|
const agentFactory = require('./agent'),
|
6
|
10
|
dbHandle = require('./handle-database');
|
|
@@ -424,7 +428,6 @@ describe('read lists of ideas', () => {
|
424
|
428
|
beforeEach(async () => {
|
425
|
429
|
const data = {
|
426
|
430
|
users: 6,
|
427
|
|
- tags: 6,
|
428
|
431
|
verifiedUsers: [0, 1, 2, 3, 4],
|
429
|
432
|
ideas: [[{}, 0], [{}, 0],[{}, 1],[{}, 2],[{}, 2],[{}, 2],[{}, 3]]
|
430
|
433
|
};
|
|
@@ -520,6 +523,12 @@ describe('read lists of ideas', () => {
|
520
|
523
|
.expect(400);
|
521
|
524
|
});
|
522
|
525
|
|
|
526
|
+ it('[too many users] 400', async () => {
|
|
527
|
+ await agent
|
|
528
|
+ .get('/ideas?filter[creators]=user1,user2,user3,user4,user5,user6,user7,user8,user9,user190,user11')
|
|
529
|
+ .expect(400);
|
|
530
|
+ });
|
|
531
|
+
|
523
|
532
|
it('[invalid pagination] 400', async () => {
|
524
|
533
|
await agent
|
525
|
534
|
.get(`/ideas?filter[creators]=${user2.username},${user3.username}&page[offset]=1&page[limit]=21`)
|
|
@@ -552,22 +561,8 @@ describe('read lists of ideas', () => {
|
552
|
561
|
beforeEach(async () => {
|
553
|
562
|
const data = {
|
554
|
563
|
users: 6,
|
555
|
|
- tags: 6,
|
556
|
564
|
verifiedUsers: [0, 1, 2, 3, 4],
|
557
|
|
- ideas: [[{}, 0], [{}, 0],[{}, 1],[{}, 2],[{}, 2],[{}, 2],[{}, 3]],
|
558
|
|
- userTag: [
|
559
|
|
- [0,0,'',5],[0,1,'',4],[0,2,'',3],[0,4,'',1],
|
560
|
|
- [1,1,'',4],[1,3,'',2],
|
561
|
|
- [2,5,'',2]
|
562
|
|
- ],
|
563
|
|
- ideaTags: [
|
564
|
|
- [0,0],[0,1],[0,2],
|
565
|
|
- [1,1],[1,2],
|
566
|
|
- [2,1],[2,2],[2,4],
|
567
|
|
- [4,0],[4,1],[4,2],[4,3],[4,4],
|
568
|
|
- [5,2],[5,3],
|
569
|
|
- [6,3]
|
570
|
|
- ],
|
|
565
|
+ ideas: Array(7).fill([]),
|
571
|
566
|
ideaComments: [[0, 0],[0, 1], [0,2],[0,2], [0,4], [1,1], [1,2], [2,1], [2,2], [3,4] ]
|
572
|
567
|
};
|
573
|
568
|
|
|
@@ -662,6 +657,12 @@ describe('read lists of ideas', () => {
|
662
|
657
|
.expect(400);
|
663
|
658
|
});
|
664
|
659
|
|
|
660
|
+ it('[too many users] 400', async () => {
|
|
661
|
+ await agent
|
|
662
|
+ .get('/ideas?filter[commentedBy]=user1,user2,user3,user4,user5,user6,user7,user8,user9,user190,user11')
|
|
663
|
+ .expect(400);
|
|
664
|
+ });
|
|
665
|
+
|
665
|
666
|
it('[invalid pagination] 400', async () => {
|
666
|
667
|
await agent
|
667
|
668
|
.get(`/ideas?filter[commentedBy]=${user2.username},${user3.username}&page[offset]=1&page[limit]=21`)
|
|
@@ -685,30 +686,16 @@ describe('read lists of ideas', () => {
|
685
|
686
|
});
|
686
|
687
|
});
|
687
|
688
|
|
688
|
|
- describe('GET /ideas?filter[highlyVoted]', () => {
|
|
689
|
+ describe('GET /ideas?filter[highlyVoted]=voteSumBottomLimit', () => {
|
689
|
690
|
let user0;
|
690
|
691
|
// create and save testing data
|
691
|
692
|
beforeEach(async () => {
|
692
|
693
|
const primarys = 'ideas';
|
693
|
694
|
const data = {
|
694
|
695
|
users: 6,
|
695
|
|
- tags: 6,
|
696
|
696
|
verifiedUsers: [0, 1, 2, 3, 4],
|
697
|
|
- ideas: [[{}, 0], [{}, 0],[{}, 1],[{}, 2],[{}, 2],[{}, 2],[{}, 3]],
|
698
|
|
- userTag: [
|
699
|
|
- [0,0,'',5],[0,1,'',4],[0,2,'',3],[0,4,'',1],
|
700
|
|
- [1,1,'',4],[1,3,'',2],
|
701
|
|
- [2,5,'',2]
|
702
|
|
- ],
|
703
|
|
- ideaTags: [
|
704
|
|
- [0,0],[0,1],[0,2],
|
705
|
|
- [1,1],[1,2],
|
706
|
|
- [2,1],[2,2],[2,4],
|
707
|
|
- [4,0],[4,1],[4,2],[4,3],[4,4],
|
708
|
|
- [5,2],[5,3],
|
709
|
|
- [6,3]
|
710
|
|
- ],
|
711
|
|
- // odeas with votes: 3:3, 1:3, 5:1, 2:1, 0:0, 6: -1, 4:-2
|
|
697
|
+ ideas: Array(7).fill([]),
|
|
698
|
+ // ideas with votes: 3:3, 1:3, 5:1, 2:1, 0:0, 6: -1, 4:-2
|
712
|
699
|
votes: [
|
713
|
700
|
[0, [primarys, 0], -1],
|
714
|
701
|
[1, [primarys, 0], 1],
|
|
@@ -830,4 +817,193 @@ describe('read lists of ideas', () => {
|
830
|
817
|
});
|
831
|
818
|
});
|
832
|
819
|
});
|
|
820
|
+
|
|
821
|
+ describe('GET /ideas?filter[trending]', () => {
|
|
822
|
+ let user0,
|
|
823
|
+ user1,
|
|
824
|
+ user2,
|
|
825
|
+ user3,
|
|
826
|
+ user4,
|
|
827
|
+ user5,
|
|
828
|
+ user6,
|
|
829
|
+ user7,
|
|
830
|
+ user8,
|
|
831
|
+ idea1,
|
|
832
|
+ idea2,
|
|
833
|
+ idea3,
|
|
834
|
+ idea4,
|
|
835
|
+ idea5,
|
|
836
|
+ idea6;
|
|
837
|
+ const now = Date.now();
|
|
838
|
+ let sandbox;
|
|
839
|
+ const threeMonths = 7776000000;
|
|
840
|
+ const threeWeeks = 1814400000;
|
|
841
|
+ const oneWeek = 604800000;
|
|
842
|
+ const twoDays = 172800000;
|
|
843
|
+ // create and save testing data
|
|
844
|
+ beforeEach(async () => {
|
|
845
|
+ sandbox = sinon.sandbox.create();
|
|
846
|
+ const primarys = 'ideas';
|
|
847
|
+ const data = {
|
|
848
|
+ users: 10,
|
|
849
|
+ verifiedUsers: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
|
|
850
|
+ ideas: Array(11).fill([]),
|
|
851
|
+ // odeas with votes: 3:3, 1:3, 5:1, 2:1, 0:0, 6: -1, 4:-2
|
|
852
|
+ votes: [
|
|
853
|
+ [0, [primarys, 1], 1],
|
|
854
|
+ [0, [primarys, 2], 1],
|
|
855
|
+ [0, [primarys, 3], 1],
|
|
856
|
+ [1, [primarys, 3], 1],
|
|
857
|
+ [2, [primarys, 3], 1],
|
|
858
|
+ [0, [primarys, 4], 1],
|
|
859
|
+ [1, [primarys, 4], 1],
|
|
860
|
+ [2, [primarys, 4], 1],
|
|
861
|
+ [3, [primarys, 4], 1],
|
|
862
|
+ [4, [primarys, 4], 1],
|
|
863
|
+ [0, [primarys, 5], 1],
|
|
864
|
+ [1, [primarys, 5], 1],
|
|
865
|
+ [2, [primarys, 5], 1],
|
|
866
|
+ [3, [primarys, 5], 1],
|
|
867
|
+ [4, [primarys, 5], 1],
|
|
868
|
+ [5, [primarys, 5], 1],
|
|
869
|
+ [6, [primarys, 5], 1],
|
|
870
|
+ [0, [primarys, 6], 1],
|
|
871
|
+ [1, [primarys, 6], 1]
|
|
872
|
+ ]
|
|
873
|
+ };
|
|
874
|
+ // post initial data and oldest votes with date three monts ago without two days
|
|
875
|
+ sandbox.useFakeTimers(now - threeMonths + twoDays);
|
|
876
|
+ dbData = await dbHandle.fill(data);
|
|
877
|
+
|
|
878
|
+ [user0, user1, user2, user3, user4, user5, user6, user7, user8 ] = dbData.users;
|
|
879
|
+ [ , idea1, idea2, idea3, idea4, idea5, idea6] = dbData.ideas;
|
|
880
|
+
|
|
881
|
+ // create data to post with time: three weeks ago
|
|
882
|
+ const dataThreeWeeksAgo = {
|
|
883
|
+ votes: [
|
|
884
|
+ {from: user1.username, to: {type: primarys, id: idea1.id}, value: 1},
|
|
885
|
+ {from: user1.username, to: {type: primarys, id: idea2.id}, value: 1},
|
|
886
|
+ {from: user2.username, to: {type: primarys, id: idea2.id}, value: 1},
|
|
887
|
+ {from: user3.username, to: {type: primarys, id: idea2.id}, value: 1},
|
|
888
|
+ {from: user3.username, to: {type: primarys, id: idea3.id}, value: 1},
|
|
889
|
+ {from: user5.username, to: {type: primarys, id: idea4.id}, value: 1},
|
|
890
|
+ {from: user6.username, to: {type: primarys, id: idea4.id}, value: 1},
|
|
891
|
+ {from: user7.username, to: {type: primarys, id: idea4.id}, value: 1},
|
|
892
|
+ {from: user7.username, to: {type: primarys, id: idea5.id}, value: 1},
|
|
893
|
+ {from: user2.username, to: {type: primarys, id: idea6.id}, value: 1},
|
|
894
|
+ {from: user3.username, to: {type: primarys, id: idea6.id}, value: 1}
|
|
895
|
+ ]
|
|
896
|
+ };
|
|
897
|
+ // stub time to three weeks ago without two days
|
|
898
|
+ sandbox.clock.restore();
|
|
899
|
+ sandbox.useFakeTimers(now - threeWeeks + twoDays);
|
|
900
|
+ // add data to database hree weeks ago without two days
|
|
901
|
+ for(const i in dataThreeWeeksAgo.votes){
|
|
902
|
+ await models.vote.create(dataThreeWeeksAgo.votes[i]);
|
|
903
|
+ }
|
|
904
|
+
|
|
905
|
+ const dataOneWeekAgo = {
|
|
906
|
+ votes: [
|
|
907
|
+ {from: user2.username, to: {type: primarys, id: idea1.id}, value: 1},
|
|
908
|
+ {from: user3.username, to: {type: primarys, id: idea1.id}, value: 1},
|
|
909
|
+ {from: user4.username, to: {type: primarys, id: idea1.id}, value: 1},
|
|
910
|
+ {from: user5.username, to: {type: primarys, id: idea1.id}, value: 1},
|
|
911
|
+ {from: user6.username, to: {type: primarys, id: idea1.id}, value: 1},
|
|
912
|
+ {from: user7.username, to: {type: primarys, id: idea1.id}, value: 1},
|
|
913
|
+ {from: user8.username, to: {type: primarys, id: idea1.id}, value: 1},
|
|
914
|
+ {from: user4.username, to: {type: primarys, id: idea2.id}, value: 1},
|
|
915
|
+ {from: user5.username, to: {type: primarys, id: idea2.id}, value: 1},
|
|
916
|
+ {from: user6.username, to: {type: primarys, id: idea2.id}, value: 1},
|
|
917
|
+ {from: user7.username, to: {type: primarys, id: idea2.id}, value: 1},
|
|
918
|
+ {from: user8.username, to: {type: primarys, id: idea2.id}, value: 1},
|
|
919
|
+ {from: user4.username, to: {type: primarys, id: idea3.id}, value: 1},
|
|
920
|
+ {from: user5.username, to: {type: primarys, id: idea3.id}, value: 1},
|
|
921
|
+ {from: user6.username, to: {type: primarys, id: idea3.id}, value: 1},
|
|
922
|
+ {from: user7.username, to: {type: primarys, id: idea3.id}, value: 1},
|
|
923
|
+ {from: user8.username, to: {type: primarys, id: idea3.id}, value: 1},
|
|
924
|
+ {from: user8.username, to: {type: primarys, id: idea4.id}, value: 1},
|
|
925
|
+ {from: user8.username, to: {type: primarys, id: idea5.id}, value: 1},
|
|
926
|
+ {from: user4.username, to: {type: primarys, id: idea6.id}, value: 1},
|
|
927
|
+ {from: user5.username, to: {type: primarys, id: idea6.id}, value: 1}
|
|
928
|
+ ]
|
|
929
|
+ };
|
|
930
|
+ // stub time to one week ago without two days
|
|
931
|
+ sandbox.clock.restore();
|
|
932
|
+ sandbox.useFakeTimers( now - oneWeek + twoDays);
|
|
933
|
+ for(const i in dataOneWeekAgo.votes){
|
|
934
|
+ await models.vote.create(dataOneWeekAgo.votes[i]);
|
|
935
|
+ }
|
|
936
|
+ sandbox.clock.restore();
|
|
937
|
+ });
|
|
938
|
+ afterEach(async () => {
|
|
939
|
+ sandbox.restore();
|
|
940
|
+ });
|
|
941
|
+
|
|
942
|
+ context('logged in', () => {
|
|
943
|
+
|
|
944
|
+ beforeEach(() => {
|
|
945
|
+ agent = agentFactory.logged(user0);
|
|
946
|
+ });
|
|
947
|
+
|
|
948
|
+ context('valid data', () => {
|
|
949
|
+
|
|
950
|
+ it('[trending] 200 and return array of matched ideas', async () => {
|
|
951
|
+ // request
|
|
952
|
+ const response = await agent
|
|
953
|
+ .get('/ideas?filter[trending]')
|
|
954
|
+ .expect(200);
|
|
955
|
+ // without pagination, limit for ideas 5 we should find 5 ideas...
|
|
956
|
+ should(response.body).have.property('data').Array().length(5);
|
|
957
|
+
|
|
958
|
+ // sorted by trending rate
|
|
959
|
+ should(response.body.data.map(idea => idea.attributes.title))
|
|
960
|
+ .eql([1, 2, 3, 6, 4].map(no => `idea title ${no}`));
|
|
961
|
+
|
|
962
|
+ });
|
|
963
|
+
|
|
964
|
+ it('[trending with pagination] offset and limit the results', async () => {
|
|
965
|
+ const response = await agent
|
|
966
|
+ .get('/ideas?filter[trending]&page[offset]=1&page[limit]=3')
|
|
967
|
+ .expect(200);
|
|
968
|
+
|
|
969
|
+ // we should find 3 ideas
|
|
970
|
+ should(response.body).have.property('data').Array().length(3);
|
|
971
|
+
|
|
972
|
+ // sorted by trending rate
|
|
973
|
+ should(response.body.data.map(idea => idea.attributes.title))
|
|
974
|
+ .eql([2, 3, 6].map(no => `idea title ${no}`));
|
|
975
|
+ });
|
|
976
|
+
|
|
977
|
+ });
|
|
978
|
+
|
|
979
|
+ context('invalid data', () => {
|
|
980
|
+
|
|
981
|
+ it('[trending invalid query.filter.highlyRated] 400', async () => {
|
|
982
|
+ await agent
|
|
983
|
+ .get('/ideas?filter[trending]=string&page[offset]=1&page[limit]=3')
|
|
984
|
+ .expect(400);
|
|
985
|
+ });
|
|
986
|
+
|
|
987
|
+ it('[trending invalid query.filter.highlyRated] 400', async () => {
|
|
988
|
+ await agent
|
|
989
|
+ .get('/ideas?filter[trending]=1&page[offset]=1&page[limit]=3')
|
|
990
|
+ .expect(400);
|
|
991
|
+ });
|
|
992
|
+
|
|
993
|
+ it('[unexpected query params] 400', async () => {
|
|
994
|
+ await agent
|
|
995
|
+ .get('/ideas?filter[trending]&additional[param]=3&page[offset]=1&page[limit]=3')
|
|
996
|
+ .expect(400);
|
|
997
|
+ });
|
|
998
|
+ });
|
|
999
|
+ });
|
|
1000
|
+
|
|
1001
|
+ context('not logged in', () => {
|
|
1002
|
+ it('403', async () => {
|
|
1003
|
+ await agent
|
|
1004
|
+ .get('/ideas?filter[trending]')
|
|
1005
|
+ .expect(403);
|
|
1006
|
+ });
|
|
1007
|
+ });
|
|
1008
|
+ });
|
833
|
1009
|
});
|