-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathangular-complexify.js
More file actions
172 lines (127 loc) · 6.22 KB
/
Copy pathangular-complexify.js
File metadata and controls
172 lines (127 loc) · 6.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
'use strict';
describe('Complexify provider', function() {
var complexify, complexifyProvider;
beforeEach(module('angular-complexify', function(ComplexifyProvider) {
complexifyProvider = ComplexifyProvider;
}));
beforeEach(inject(function($injector) {
complexify = $injector.get('Complexify');
}));
it('should be able to update options', function() {
expect(complexifyProvider.getOptions('minimumChars')).toBe(8);
expect(complexifyProvider.getOptions('strengthScaleFactor')).toBe(1);
expect(complexifyProvider.getOptions('bannedPasswords').length).toBe(882);
complexifyProvider.setOptions('minimumChars', 1);
complexifyProvider.setOptions('strengthScaleFactor', 2);
complexifyProvider.setOptions('bannedPasswords', ['foo', 'bar']);
expect(complexifyProvider.getOptions('minimumChars')).toBe(1);
expect(complexifyProvider.getOptions('strengthScaleFactor')).toBe(2);
expect(complexifyProvider.getOptions('bannedPasswords').length).toBe(2);
});
it('should return additional complexity for charset', function() {
expect(complexifyProvider.additionalComplexityForCharset('foo123', [0x0030, 0x0039])).toBe(10);
expect(complexifyProvider.additionalComplexityForCharset('foobar', [0x0030, 0x0039])).toBe(0);
});
it('should return true if value exists in array', function() {
expect(complexifyProvider.inBanlist('password')).toBe(true);
expect(complexifyProvider.inBanlist('321456987')).toBe(false);
});
it('should return evaluate security value', function() {
expect(complexifyProvider.evaluateSecurity().complexity).toBe(0);
expect(complexifyProvider.evaluateSecurity('password').complexity).toBe(0);
expect(complexifyProvider.evaluateSecurity('321456987').complexity).toBe(17.269388197455342);
expect(complexifyProvider.evaluateSecurity('abcfoobar').complexity).toBe(24.435724035161112);
expect(complexifyProvider.evaluateSecurity('abc123bar').complexity).toBe(0);
expect(complexifyProvider.evaluateSecurity('123fsd4tSDidUguvI4ebtvew').complexity).toBe(82.54268770090182);
});
});
describe('Complexify directive', function () {
var scope, element, compile;
beforeEach(module('angular-complexify'));
beforeEach(inject(function ($rootScope, $compile, $templateCache) {
scope = $rootScope.$new();
compile = $compile;
}));
it('should return percent complexity', function() {
scope.inputModel = 'abc123bar';
element = compile('<span complexify="inputModel"></span>')(scope);
scope.$digest();
expect(element.text()).toBe('0');
});
it('should return verbal complexity', function() {
scope.inputModel = 'abc123bar';
element = compile('<span complexify="inputModel" type="verbal"></span>')(scope);
scope.$digest();
expect(element.text()).toBe('Weak');
});
});
describe('Complexify validation directive', function () {
var scope, element, compile;
beforeEach(module('angular-complexify'));
beforeEach(inject(function ($rootScope, $compile) {
scope = $rootScope.$new();
compile = $compile;
}));
it('should start with valid and pristine', function() {
element = compile('<form name="form"><input name="password" type="text" ng-model="password" complexify-validate></form>')(scope);
scope.$digest();
expect(element.hasClass('ng-pristine')).toBeTruthy();
expect(element.hasClass('ng-valid')).toBeTruthy();
});
it('should set ng-invalid-password-complexity when password is not complex enough', function() {
element = compile('<form name="form"><input name="password" type="text" ng-model="password" complexify-validate></form>')(scope);
scope.$digest();
scope.form.password.$setViewValue('1234567890');
scope.$digest();
expect(element.hasClass('ng-pristine')).toBeFalsy();
expect(element.hasClass('ng-valid')).toBeFalsy();
expect(element.hasClass('ng-dirty')).toBeTruthy();
expect(element.hasClass('ng-invalid')).toBeTruthy();
expect(element.hasClass('ng-invalid-password-complexity')).toBeTruthy();
});
it('should remove ng-invalid-password-complexity when password is valid', function() {
element = compile('<form name="form"><input name="password" type="text" ng-model="password" complexify-validate></form>')(scope);
scope.$digest();
scope.form.password.$setViewValue('1234567890');
scope.$digest();
expect(element.hasClass('ng-pristine')).toBeFalsy();
expect(element.hasClass('ng-valid')).toBeFalsy();
expect(element.hasClass('ng-dirty')).toBeTruthy();
expect(element.hasClass('ng-invalid')).toBeTruthy();
expect(element.hasClass('ng-invalid-password-complexity')).toBeTruthy();
scope.form.password.$setViewValue('1a2b3c4d5e6f7g8h9i0Abdelieaelieaelie)(');
scope.$digest();
expect(element.hasClass('ng-valid')).toBeTruthy();
expect(element.hasClass('ng-valid-password-complexity')).toBeTruthy();
});
it('should be able to customize complexity', inject(function(Complexify) {
var passwordLessThan40 = '3d32J8dh8F',
passwordMoreThan40 = '8gD1gf9kDNi0Jf',
threshold = 40;
element = compile('<form name="form"><input name="password" type="text" ng-model="password" complexify-validate="' + threshold + '"></form>')(scope);
scope.$digest();
scope.form.password.$setViewValue(passwordLessThan40);
scope.$digest();
expect(element.hasClass('ng-pristine')).toBeFalsy();
expect(element.hasClass('ng-valid')).toBeFalsy();
expect(element.hasClass('ng-dirty')).toBeTruthy();
expect(element.hasClass('ng-invalid')).toBeTruthy();
expect(element.hasClass('ng-invalid-password-complexity')).toBeTruthy();
expect(Complexify(passwordLessThan40).complexity).toBeLessThan(threshold);
scope.form.password.$setViewValue(passwordMoreThan40);
scope.$digest();
expect(element.hasClass('ng-valid')).toBeTruthy();
expect(element.hasClass('ng-valid-password-complexity')).toBeTruthy();
expect(Complexify(passwordMoreThan40).complexity).toBeGreaterThan(threshold);
}));
});
describe('Complexify filter', function () {
var filter;
beforeEach(module('angular-complexify'));
beforeEach(inject(function (complexifyFilter) {
filter = complexifyFilter;
}));
it('should be greater than zero', function() {
expect(filter('1a2b3c4d5e6f7g8h9i')).toBeGreaterThan(0);
});
});