1
1
import { expect , jest , test } from '@jest/globals'
2
2
import { Change , Changes } from '../src/schemas'
3
-
4
- let getInvalidLicenseChanges : Function
3
+ import { getInvalidLicenseChanges } from '../src/licenses'
5
4
6
5
const npmChange : Change = {
7
6
manifest : 'package.json' ,
@@ -30,7 +29,7 @@ const rubyChange: Change = {
30
29
name : 'actionsomething' ,
31
30
version : '3.2.0' ,
32
31
package_url :
'pkg:gem/[email protected] ' ,
33
- license : 'BSD' ,
32
+ license : 'BSD-3-Clause ' ,
34
33
source_repository_url : 'github.com/some-repo' ,
35
34
scope : 'runtime' ,
36
35
vulnerabilities : [
@@ -100,29 +99,32 @@ jest.mock('octokit', () => {
100
99
101
100
beforeEach ( async ( ) => {
102
101
jest . resetModules ( )
103
- jest . doMock ( 'spdx-satisfies' , ( ) => {
104
- // mock spdx-satisfies return value
105
- // true for BSD, false for all others
106
- return jest . fn ( ( license : string , _ : string ) : boolean => license === 'BSD' )
107
- } )
108
- // eslint-disable-next-line @typescript-eslint/no-require-imports
109
- ; ( { getInvalidLicenseChanges} = require ( '../src/licenses' ) )
110
102
} )
111
103
112
104
test ( 'it adds license outside the allow list to forbidden changes' , async ( ) => {
113
- const changes : Changes = [ npmChange , rubyChange ]
105
+ const changes : Changes = [
106
+ npmChange , // MIT license
107
+ rubyChange // BSD license
108
+ ]
109
+
114
110
const { forbidden} = await getInvalidLicenseChanges ( changes , {
115
- allow : [ 'BSD' ]
111
+ allow : [ 'BSD-3-Clause ' ]
116
112
} )
113
+
117
114
expect ( forbidden [ 0 ] ) . toBe ( npmChange )
118
115
expect ( forbidden . length ) . toEqual ( 1 )
119
116
} )
120
117
121
118
test ( 'it adds license inside the deny list to forbidden changes' , async ( ) => {
122
- const changes : Changes = [ npmChange , rubyChange ]
119
+ const changes : Changes = [
120
+ npmChange , // MIT license
121
+ rubyChange // BSD license
122
+ ]
123
+
123
124
const { forbidden} = await getInvalidLicenseChanges ( changes , {
124
- deny : [ 'BSD' ]
125
+ deny : [ 'BSD-3-Clause ' ]
125
126
} )
127
+
126
128
expect ( forbidden [ 0 ] ) . toBe ( rubyChange )
127
129
expect ( forbidden . length ) . toEqual ( 1 )
128
130
} )
@@ -133,7 +135,7 @@ test('it does not add license outside the allow list to forbidden changes if it
133
135
{ ...rubyChange , change_type : 'removed' }
134
136
]
135
137
const { forbidden} = await getInvalidLicenseChanges ( changes , {
136
- allow : [ 'BSD' ]
138
+ allow : [ 'BSD-3-Clause ' ]
137
139
} )
138
140
expect ( forbidden ) . toStrictEqual ( [ ] )
139
141
} )
@@ -144,7 +146,7 @@ test('it does not add license inside the deny list to forbidden changes if it is
144
146
{ ...rubyChange , change_type : 'removed' }
145
147
]
146
148
const { forbidden} = await getInvalidLicenseChanges ( changes , {
147
- deny : [ 'BSD' ]
149
+ deny : [ 'BSD-3-Clause ' ]
148
150
} )
149
151
expect ( forbidden ) . toStrictEqual ( [ ] )
150
152
} )
@@ -156,23 +158,18 @@ test('it adds license outside the allow list to forbidden changes if it is in bo
156
158
{ ...rubyChange , change_type : 'removed' }
157
159
]
158
160
const { forbidden} = await getInvalidLicenseChanges ( changes , {
159
- allow : [ 'BSD' ]
161
+ allow : [ 'BSD-3-Clause ' ]
160
162
} )
161
163
expect ( forbidden ) . toStrictEqual ( [ npmChange ] )
162
164
} )
163
165
164
166
test ( 'it adds all licenses to unresolved if it is unable to determine the validity' , async ( ) => {
165
- jest . resetModules ( ) // reset module set in before
166
- jest . doMock ( 'spdx-satisfies' , ( ) => {
167
- return jest . fn ( ( _first : string , _second : string ) => {
168
- throw new Error ( 'Some Error' )
169
- } )
170
- } )
171
- // eslint-disable-next-line @typescript-eslint/no-require-imports
172
- ; ( { getInvalidLicenseChanges} = require ( '../src/licenses' ) )
173
- const changes : Changes = [ npmChange , rubyChange ]
167
+ const changes : Changes = [
168
+ { ...npmChange , license : 'Foo' } ,
169
+ { ...rubyChange , license : 'Bar' }
170
+ ]
174
171
const invalidLicenses = await getInvalidLicenseChanges ( changes , {
175
- allow : [ 'BSD ' ]
172
+ allow : [ 'Apache-2.0 ' ]
176
173
} )
177
174
expect ( invalidLicenses . forbidden . length ) . toEqual ( 0 )
178
175
expect ( invalidLicenses . unlicensed . length ) . toEqual ( 0 )
@@ -182,7 +179,7 @@ test('it adds all licenses to unresolved if it is unable to determine the validi
182
179
test ( 'it does not filter out changes that are on the exclusions list' , async ( ) => {
183
180
const changes : Changes = [ pipChange , npmChange , rubyChange ]
184
181
const licensesConfig = {
185
- allow : [ 'BSD' ] ,
182
+ allow : [ 'BSD-3-Clause ' ] ,
186
183
licenseExclusions :
[ 'pkg:pypi/[email protected] ' , 'pkg:npm/[email protected] ' ]
187
184
}
188
185
const invalidLicenses = await getInvalidLicenseChanges (
@@ -198,7 +195,7 @@ test('it does not fail when the packages dont have a valid PURL', async () => {
198
195
199
196
const changes : Changes = [ emptyPurlChange , npmChange , rubyChange ]
200
197
const licensesConfig = {
201
- allow : [ 'BSD' ] ,
198
+ allow : [ 'BSD-3-Clause ' ] ,
202
199
licenseExclusions :
[ 'pkg:pypi/[email protected] ' , 'pkg:npm/[email protected] ' ]
203
200
}
204
201
@@ -212,16 +209,18 @@ test('it does not fail when the packages dont have a valid PURL', async () => {
212
209
test ( 'it does filters out changes if they are not on the exclusions list' , async ( ) => {
213
210
const changes : Changes = [ pipChange , npmChange , rubyChange ]
214
211
const licensesConfig = {
215
- allow : [ 'BSD' ] ,
212
+ allow : [ 'BSD-3-Clause ' ] ,
216
213
licenseExclusions : [
217
214
218
215
219
216
]
220
217
}
218
+
221
219
const invalidLicenses = await getInvalidLicenseChanges (
222
220
changes ,
223
221
licensesConfig
224
222
)
223
+
225
224
expect ( invalidLicenses . forbidden . length ) . toEqual ( 2 )
226
225
expect ( invalidLicenses . forbidden [ 0 ] ) . toBe ( pipChange )
227
226
expect ( invalidLicenses . forbidden [ 1 ] ) . toBe ( npmChange )
0 commit comments