1+ var rewire = require ( 'rewire' ) ;
2+ var assert = require ( 'assert' ) ;
3+
4+ var ld = rewire ( '../index.js' ) ;
5+
6+ match_target = ld . __get__ ( 'match_target' ) ;
7+ match_user = ld . __get__ ( 'match_user' ) ;
8+ sanitize_user = ld . __get__ ( 'sanitize_user' ) ;
9+
10+ describe ( 'match_target' , function ( ) {
11+ it ( 'should match users based on top-level attributes' , function ( ) {
12+ var u = { key : 'foo' , firstName : 'alice' } ;
13+ var t = {
14+ attribute : 'firstName' ,
15+ op : 'in' ,
16+ values : [ 'alice' , 'bob' ]
17+ }
18+ assert ( match_target ( t , u ) ) ;
19+ } ) ;
20+ it ( 'should not match users based on non-matching top-level attributes' , function ( ) {
21+ var u = { key : 'foo' , firstName : 'clarisse' } ;
22+ var t = {
23+ attribute : 'firstName' ,
24+ op : 'in' ,
25+ values : [ 'alice' , 'bob' ]
26+ }
27+ assert ( ! match_target ( t , u ) ) ;
28+ } ) ;
29+ it ( 'should match users based single-value custom string attributes' , function ( ) {
30+ var u = {
31+ key : 'foo' ,
32+ custom : {
33+ favoriteColor : 'green'
34+ }
35+ } ;
36+ var t = {
37+ attribute : 'favoriteColor' ,
38+ op : 'in' ,
39+ values : [ 'green' , 'red' ]
40+ }
41+ assert ( match_target ( t , u ) ) ;
42+ } ) ;
43+ it ( 'should not match users without single-value custom attributes' , function ( ) {
44+ var u = {
45+ key : 'foo' ,
46+ custom : {
47+ favoriteDog : 'labrador'
48+ }
49+ } ;
50+ var t = {
51+ attribute : 'favoriteColor' ,
52+ op : 'in' ,
53+ values : [ 'green' , 'red' ]
54+ }
55+ assert ( ! match_target ( t , u ) ) ;
56+ } ) ;
57+ it ( 'should not match users with non-matching single-value custom attributes' , function ( ) {
58+ var u = {
59+ key : 'foo' ,
60+ custom : {
61+ favoriteDog : 'labrador'
62+ }
63+ } ;
64+ var t = {
65+ attribute : 'favoriteColor' ,
66+ op : 'in' ,
67+ values : [ 'green' , 'red' ]
68+ }
69+ assert ( ! match_target ( t , u ) ) ;
70+ } ) ;
71+ it ( 'should match users based intersecting list custom string attributes' , function ( ) {
72+ var u = {
73+ key : 'foo' ,
74+ custom : {
75+ favoriteColor : [ 'green' , 'blue' ]
76+ }
77+ } ;
78+ var t = {
79+ attribute : 'favoriteColor' ,
80+ op : 'in' ,
81+ values : [ 'green' , 'red' ]
82+ } ;
83+ assert ( match_target ( t , u ) ) ;
84+ } ) ;
85+ it ( 'should not match users based non-intersecting list custom string attributes' , function ( ) {
86+ var u = {
87+ key : 'foo' ,
88+ custom : {
89+ favoriteColor : [ 'purple' , 'blue' ]
90+ }
91+ } ;
92+ var t = {
93+ attribute : 'favoriteColor' ,
94+ op : 'in' ,
95+ values : [ 'green' , 'red' ]
96+ } ;
97+ assert ( ! match_target ( t , u ) ) ;
98+ } ) ;
99+ } ) ;
100+
101+ describe ( 'match_user' , function ( ) {
102+ it ( 'should match the user when the key matches' , function ( ) {
103+ var u = { key : 'foo' , firstName : 'alice' } ;
104+ var v = {
105+ value : true ,
106+ weight : 0 ,
107+ userTarget : {
108+ attribute : 'key' ,
109+ op : 'in' ,
110+ values : [ 'bar' , 'foo' ]
111+ } ,
112+ targets : [ ]
113+ } ;
114+ assert ( match_user ( v , u ) ) ;
115+ } ) ;
116+ it ( 'should not match the user when the key does not match' , function ( ) {
117+ var u = { key : 'foo' , firstName : 'alice' } ;
118+ var v = {
119+ value : true ,
120+ weight : 0 ,
121+ userTarget : {
122+ attribute : 'key' ,
123+ op : 'in' ,
124+ values : [ 'bar' , 'fiz' ]
125+ } ,
126+ targets : [ ]
127+ } ;
128+ assert ( ! match_user ( v , u ) ) ;
129+ } ) ;
130+ it ( 'should not match the user when the user target is empty' , function ( ) {
131+ var u = { key : 'foo' , firstName : 'alice' } ;
132+ var v = {
133+ value : true ,
134+ weight : 0 ,
135+ userTarget : {
136+ attribute : 'key' ,
137+ op : 'in' ,
138+ values : [ ]
139+ } ,
140+ targets : [ ]
141+ } ;
142+ assert ( ! match_user ( v , u ) ) ;
143+ } ) ;
144+ it ( 'should not match the user when the user target is missing' , function ( ) {
145+ var u = { key : 'foo' , firstName : 'alice' } ;
146+ var v = {
147+ value : true ,
148+ weight : 0 ,
149+ targets : [ ]
150+ } ;
151+ assert ( ! match_user ( v , u ) ) ;
152+ } ) ;
153+ } ) ;
154+
155+ describe ( 'sanitize_user' , function ( ) {
156+ it ( 'should do nothing when the key is already a string' , function ( ) {
157+ var u = { key : 'foo' , firstName : 'alice' } ;
158+ var u0 = { key : 'foo' , firstName : 'alice' } ;
159+ sanitize_user ( u ) ;
160+ assert . deepStrictEqual ( u0 , u ) ;
161+ } ) ;
162+ it ( 'should coerce a numeric key to a string' , function ( ) {
163+ var u = { key : 33 , firstName : 'alice' } ;
164+ var u0 = { key : '33' , firstName : 'alice' } ;
165+ sanitize_user ( u ) ;
166+ assert . deepStrictEqual ( u0 , u ) ;
167+ } ) ;
168+ it ( 'should coerce a boolean key to a string' , function ( ) {
169+ var u = { key : true , firstName : 'alice' } ;
170+ var u0 = { key : 'true' , firstName : 'alice' } ;
171+ sanitize_user ( u ) ;
172+ assert . deepStrictEqual ( u0 , u ) ;
173+ } ) ;
174+ it ( 'should not blow up if the key is missing' , function ( ) {
175+ var u = { firstName : 'alice' } ;
176+ var u0 = { firstName : 'alice' } ;
177+ sanitize_user ( u ) ;
178+ assert . deepStrictEqual ( u0 , u ) ;
179+ } ) ;
180+ } ) ;
0 commit comments