1
- import {
2
- commitSafetyChecks ,
3
- isTimestampOlder
4
- } from '../../src/functions/commit-safety-checks'
1
+ import { commitSafetyChecks } from '../../src/functions/commit-safety-checks'
5
2
import { COLORS } from '../../src/functions/colors'
6
3
import * as core from '@actions/core'
7
4
5
+ jest . mock ( '../../src/functions/is-timestamp-older' , ( ) => ( {
6
+ isTimestampOlder : jest . fn ( )
7
+ } ) )
8
+ import { isTimestampOlder } from '../../src/functions/is-timestamp-older'
9
+
8
10
const debugMock = jest . spyOn ( core , 'debug' ) . mockImplementation ( ( ) => { } )
9
11
const infoMock = jest . spyOn ( core , 'info' ) . mockImplementation ( ( ) => { } )
10
12
const warningMock = jest . spyOn ( core , 'warning' ) . mockImplementation ( ( ) => { } )
@@ -55,14 +57,12 @@ beforeEach(() => {
55
57
} )
56
58
57
59
test ( 'checks a commit and finds that it is safe (date)' , async ( ) => {
60
+ isTimestampOlder . mockReturnValue ( false )
58
61
expect ( await commitSafetyChecks ( context , data ) ) . toStrictEqual ( {
59
62
message : 'success' ,
60
63
status : true ,
61
64
isVerified : false
62
65
} )
63
- expect ( debugMock ) . toHaveBeenCalledWith (
64
- '2024-10-15T12:00:00Z is not older than 2024-10-15T11:00:00Z'
65
- )
66
66
expect ( debugMock ) . toHaveBeenCalledWith ( 'isVerified: false' )
67
67
expect ( debugMock ) . toHaveBeenCalledWith (
68
68
`🔑 commit does not contain a verified signature but ${ COLORS . highlight } commit signing is not required${ COLORS . reset } - ${ COLORS . success } OK${ COLORS . reset } `
@@ -72,6 +72,7 @@ test('checks a commit and finds that it is safe (date)', async () => {
72
72
} )
73
73
74
74
test ( 'checks a commit and finds that it is safe (date + verification)' , async ( ) => {
75
+ isTimestampOlder . mockReturnValue ( false )
75
76
data . inputs . commit_verification = true
76
77
data . commit . verification = {
77
78
verified : true ,
@@ -85,16 +86,14 @@ test('checks a commit and finds that it is safe (date + verification)', async ()
85
86
status : true ,
86
87
isVerified : true
87
88
} )
88
- expect ( debugMock ) . toHaveBeenCalledWith (
89
- '2024-10-15T12:00:00Z is not older than 2024-10-15T11:00:00Z'
90
- )
91
89
expect ( debugMock ) . toHaveBeenCalledWith ( 'isVerified: true' )
92
90
expect ( infoMock ) . toHaveBeenCalledWith (
93
91
`🔑 commit signature is ${ COLORS . success } valid${ COLORS . reset } `
94
92
)
95
93
} )
96
94
97
95
test ( 'checks a commit and finds that it is not safe (date)' , async ( ) => {
96
+ isTimestampOlder . mockReturnValue ( true )
98
97
data . commit . author . date = '2024-10-15T12:00:01Z'
99
98
100
99
expect ( await commitSafetyChecks ( context , data ) ) . toStrictEqual ( {
@@ -103,13 +102,11 @@ test('checks a commit and finds that it is not safe (date)', async () => {
103
102
status : false ,
104
103
isVerified : false
105
104
} )
106
- expect ( debugMock ) . toHaveBeenCalledWith (
107
- '2024-10-15T12:00:00Z is older than 2024-10-15T12:00:01Z'
108
- )
109
105
expect ( debugMock ) . toHaveBeenCalledWith ( 'isVerified: false' )
110
106
} )
111
107
112
108
test ( 'checks a commit and finds that it is not safe (verification)' , async ( ) => {
109
+ isTimestampOlder . mockReturnValue ( false )
113
110
data . inputs . commit_verification = true
114
111
data . commit . verification = {
115
112
verified : false ,
@@ -124,9 +121,6 @@ test('checks a commit and finds that it is not safe (verification)', async () =>
124
121
status : false ,
125
122
isVerified : false
126
123
} )
127
- expect ( debugMock ) . toHaveBeenCalledWith (
128
- '2024-10-15T12:00:00Z is not older than 2024-10-15T11:00:00Z'
129
- )
130
124
expect ( debugMock ) . toHaveBeenCalledWith ( 'isVerified: false' )
131
125
expect ( warningMock ) . toHaveBeenCalledWith (
132
126
`🔑 commit signature is ${ COLORS . error } invalid${ COLORS . reset } `
@@ -136,6 +130,10 @@ test('checks a commit and finds that it is not safe (verification)', async () =>
136
130
} )
137
131
138
132
test ( 'checks a commit and finds that it is not safe (verification time) even though it is verified - rejected due to timestamp' , async ( ) => {
133
+ // First call: commit_created_at check (should be false), second call: verified_at check (should be true)
134
+ isTimestampOlder
135
+ . mockImplementationOnce ( ( ) => false )
136
+ . mockImplementationOnce ( ( ) => true )
139
137
data . inputs . commit_verification = true
140
138
data . commit . verification = {
141
139
verified : true ,
@@ -150,9 +148,6 @@ test('checks a commit and finds that it is not safe (verification time) even tho
150
148
status : false ,
151
149
isVerified : true
152
150
} )
153
- expect ( debugMock ) . toHaveBeenCalledWith (
154
- '2024-10-15T12:00:00Z is not older than 2024-10-15T11:00:00Z'
155
- )
156
151
expect ( debugMock ) . toHaveBeenCalledWith ( 'isVerified: true' )
157
152
expect ( infoMock ) . toHaveBeenCalledWith (
158
153
`🔑 commit signature is ${ COLORS . success } valid${ COLORS . reset } `
@@ -162,6 +157,12 @@ test('checks a commit and finds that it is not safe (verification time) even tho
162
157
} )
163
158
164
159
test ( 'raises an error if the date format is invalid' , async ( ) => {
160
+ // Simulate isTimestampOlder throwing
161
+ isTimestampOlder . mockImplementation ( ( ) => {
162
+ throw new Error (
163
+ 'Invalid date format. Please ensure the dates are valid UTC timestamps.'
164
+ )
165
+ } )
165
166
data . commit . author . date = '2024-10-15T12:00:uhoh'
166
167
await expect ( commitSafetyChecks ( context , data ) ) . rejects . toThrow (
167
168
'Invalid date format. Please ensure the dates are valid UTC timestamps.'
@@ -184,6 +185,7 @@ test('throws if commit.author.date is missing', async () => {
184
185
} )
185
186
186
187
test ( 'rejects a deployment if commit.verification.verified_at is null and commit_verification is true' , async ( ) => {
188
+ isTimestampOlder . mockReturnValue ( false )
187
189
data . inputs . commit_verification = true
188
190
data . commit . verification = {
189
191
verified : true ,
@@ -201,6 +203,7 @@ test('rejects a deployment if commit.verification.verified_at is null and commit
201
203
} )
202
204
203
205
test ( 'rejects a deployment if commit.verification.verified_at is missing and commit_verification is true' , async ( ) => {
206
+ isTimestampOlder . mockReturnValue ( false )
204
207
data . inputs . commit_verification = true
205
208
data . commit . verification = {
206
209
verified : true ,
@@ -216,23 +219,8 @@ test('rejects a deployment if commit.verification.verified_at is missing and com
216
219
} )
217
220
} )
218
221
219
- test ( 'isTimestampOlder throws if timestampA is missing' , ( ) => {
220
- expect ( ( ) => isTimestampOlder ( undefined , '2024-10-15T11:00:00Z' ) ) . toThrow (
221
- 'One or both timestamps are missing or empty.'
222
- )
223
- } )
224
-
225
- test ( 'isTimestampOlder throws if timestampB is missing' , ( ) => {
226
- expect ( ( ) => isTimestampOlder ( '2024-10-15T11:00:00Z' , undefined ) ) . toThrow (
227
- 'One or both timestamps are missing or empty.'
228
- )
229
- } )
230
-
231
- test ( 'isTimestampOlder throws if both timestamps are invalid' , ( ) => {
232
- expect ( ( ) => isTimestampOlder ( 'bad' , 'bad' ) ) . toThrow ( / I n v a l i d d a t e f o r m a t / )
233
- } )
234
-
235
222
test ( 'isTimestampOlder covers else branch (not older)' , async ( ) => {
223
+ isTimestampOlder . mockReturnValue ( false )
236
224
const context = { payload : { comment : { created_at : '2024-10-15T12:00:00Z' } } }
237
225
const data = {
238
226
sha : 'abc123' ,
@@ -249,7 +237,5 @@ test('isTimestampOlder covers else branch (not older)', async () => {
249
237
inputs : { commit_verification : false }
250
238
}
251
239
await commitSafetyChecks ( context , data )
252
- expect ( debugMock ) . toHaveBeenCalledWith (
253
- '2024-10-15T12:00:00Z is not older than 2024-10-15T11:00:00Z'
254
- )
240
+ expect ( debugMock ) . toHaveBeenCalledWith ( 'isVerified: false' )
255
241
} )
0 commit comments