| 
									
										
										
										
											2017-02-14 15:04:48 +03:00
										 |  |  | 'use strict'; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:06:15 -07:00
										 |  |  | const assert = require('assert'); | 
					
						
							|  |  |  | const async = require('async'); | 
					
						
							| 
									
										
										
										
											2017-02-14 15:04:48 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:06:15 -07:00
										 |  |  | const db = require('./mocks/databasemock'); | 
					
						
							|  |  |  | const meta = require('../src/meta'); | 
					
						
							|  |  |  | const User = require('../src/user'); | 
					
						
							|  |  |  | const Groups = require('../src/groups'); | 
					
						
							| 
									
										
										
										
											2017-02-14 15:04:48 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | describe('rewards', () => { | 
					
						
							| 
									
										
										
										
											2021-02-04 00:06:15 -07:00
										 |  |  | 	let adminUid; | 
					
						
							|  |  |  | 	let bazUid; | 
					
						
							|  |  |  | 	let herpUid; | 
					
						
							| 
									
										
										
										
											2017-02-14 15:04:48 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 	before((done) => { | 
					
						
							| 
									
										
										
										
											2017-02-14 15:04:48 +03:00
										 |  |  | 		// Create 3 users: 1 admin, 2 regular
 | 
					
						
							|  |  |  | 		async.series([ | 
					
						
							| 
									
										
										
										
											2018-09-30 13:42:33 -04:00
										 |  |  | 			async.apply(User.create, { username: 'foo' }), | 
					
						
							|  |  |  | 			async.apply(User.create, { username: 'baz' }), | 
					
						
							|  |  |  | 			async.apply(User.create, { username: 'herp' }), | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 		], (err, uids) => { | 
					
						
							| 
									
										
										
										
											2017-02-14 15:04:48 +03:00
										 |  |  | 			if (err) { | 
					
						
							|  |  |  | 				return done(err); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			adminUid = uids[0]; | 
					
						
							|  |  |  | 			bazUid = uids[1]; | 
					
						
							|  |  |  | 			herpUid = uids[2]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			async.series([ | 
					
						
							|  |  |  | 				function (next) { | 
					
						
							| 
									
										
										
										
											2017-02-18 15:05:36 -07:00
										 |  |  | 					Groups.join('administrators', adminUid, next); | 
					
						
							| 
									
										
										
										
											2017-02-14 15:04:48 +03:00
										 |  |  | 				}, | 
					
						
							|  |  |  | 				function (next) { | 
					
						
							| 
									
										
										
										
											2017-02-18 15:05:36 -07:00
										 |  |  | 					Groups.join('rewardGroup', adminUid, next); | 
					
						
							| 
									
										
										
										
											2017-02-17 19:31:21 -07:00
										 |  |  | 				}, | 
					
						
							| 
									
										
										
										
											2017-02-14 15:04:48 +03:00
										 |  |  | 			], done); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 	describe('rewards create', () => { | 
					
						
							| 
									
										
										
										
											2021-02-04 00:06:15 -07:00
										 |  |  | 		const socketAdmin = require('../src/socket.io/admin'); | 
					
						
							|  |  |  | 		const rewards = require('../src/rewards'); | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 		it('it should save a reward', (done) => { | 
					
						
							| 
									
										
										
										
											2021-02-04 00:06:15 -07:00
										 |  |  | 			const data = [ | 
					
						
							| 
									
										
										
										
											2017-02-14 15:04:48 +03:00
										 |  |  | 				{ | 
					
						
							|  |  |  | 					rewards: { groupname: 'Gamers' }, | 
					
						
							|  |  |  | 					condition: 'essentials/user.postcount', | 
					
						
							|  |  |  | 					conditional: 'greaterthan', | 
					
						
							|  |  |  | 					value: '10', | 
					
						
							|  |  |  | 					rid: 'essentials/add-to-group', | 
					
						
							|  |  |  | 					claimable: '1', | 
					
						
							|  |  |  | 					id: '', | 
					
						
							| 
									
										
										
										
											2017-02-17 19:31:21 -07:00
										 |  |  | 					disabled: false, | 
					
						
							|  |  |  | 				}, | 
					
						
							| 
									
										
										
										
											2017-02-14 15:04:48 +03:00
										 |  |  | 			]; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 			socketAdmin.rewards.save({ uid: adminUid }, data, (err) => { | 
					
						
							| 
									
										
										
										
											2017-02-14 15:04:48 +03:00
										 |  |  | 				assert.ifError(err); | 
					
						
							|  |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 		it('should check condition', (done) => { | 
					
						
							| 
									
										
										
										
											2017-02-14 15:04:48 +03:00
										 |  |  | 			function method(next) { | 
					
						
							|  |  |  | 				next(null, 1); | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2019-09-17 09:12:45 -04:00
										 |  |  | 			rewards.checkConditionAndRewardUser({ | 
					
						
							|  |  |  | 				uid: adminUid, | 
					
						
							|  |  |  | 				condition: 'essentials/user.postcount', | 
					
						
							|  |  |  | 				method: method, | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 			}, (err, data) => { | 
					
						
							| 
									
										
										
										
											2017-02-14 15:04:48 +03:00
										 |  |  | 				assert.ifError(err); | 
					
						
							|  |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | }); |