1
1
import { AppController } from './app.controller' ;
2
2
import { Test , TestingModule } from '@nestjs/testing' ;
3
- import { of , Subject , Subscription , throwError } from 'rxjs' ;
3
+ import {
4
+ firstValueFrom ,
5
+ lastValueFrom ,
6
+ of ,
7
+ Subject ,
8
+ Subscription ,
9
+ throwError ,
10
+ } from 'rxjs' ;
4
11
import { HttpService } from '@nestjs/axios' ;
5
12
import { BadRequestException , UnauthorizedException } from '@nestjs/common' ;
6
13
import { DeploymentInfo } from './deployment-info.dto' ;
7
14
import { ConfigModule } from '@nestjs/config' ;
8
15
import * as fs from 'fs' ;
9
16
10
17
let onSubscription : Subscription ;
11
- const lines = new Subject < string > ( ) ;
18
+ let lines : Subject < string > ;
12
19
jest . mock ( 'tail' , ( ) => {
13
20
// Mock Tail constructor
14
21
return {
15
22
Tail : jest . fn ( ) . mockReturnValue ( {
16
23
on : ( event , callback ) => ( onSubscription = lines . subscribe ( callback ) ) ,
17
- unwatch : ( ) => onSubscription . unsubscribe ( ) ,
24
+ unwatch : ( ) => {
25
+ console . log ( 'subscription' , onSubscription ) ;
26
+ onSubscription . unsubscribe ( ) ;
27
+ } ,
18
28
} ) ,
19
29
} ;
20
30
} ) ;
@@ -37,6 +47,7 @@ describe('AppController', () => {
37
47
} ;
38
48
39
49
beforeEach ( async ( ) => {
50
+ lines = new Subject ( ) ;
40
51
mockWs = { write : jest . fn ( ) , close : jest . fn ( ) } ;
41
52
jest . spyOn ( fs , 'createWriteStream' ) . mockReturnValue ( mockWs as any ) ;
42
53
mockHttp = {
@@ -67,45 +78,107 @@ describe('AppController', () => {
67
78
} ) ;
68
79
} ) ;
69
80
70
- it ( 'should throw bad request exception if data has wrong format' , ( done ) => {
71
- const invalidData = { ...deploymentData , name : 'with space' } ;
72
- // TODO: add an extensive list of invalid formats including attempts someone could pass to try and inject code?
81
+ it ( 'should throw bad request exception if name has wrong format' , async ( ) => {
82
+ passDeployment ( ) ;
83
+ function testName ( name : string ) {
84
+ return lastValueFrom ( controller . deployApp ( { ...deploymentData , name } ) ) ;
85
+ }
86
+ await expect ( testName ( 'with space' ) ) . rejects . toBeInstanceOf (
87
+ BadRequestException ,
88
+ ) ;
89
+ await expect ( testName ( 'withCapital' ) ) . resolves . toBeTruthy ( ) ;
90
+ await expect ( testName ( 'withSymbol?' ) ) . rejects . toBeInstanceOf (
91
+ BadRequestException ,
92
+ ) ;
93
+ await expect ( testName ( 'withNumber123' ) ) . resolves . toBeTruthy ( ) ;
94
+ await expect ( testName ( 'with-dash' ) ) . resolves . toBeTruthy ( ) ;
95
+ await expect ( testName ( 'with_underscore' ) ) . rejects . toBeInstanceOf (
96
+ BadRequestException ,
97
+ ) ;
98
+ } ) ;
73
99
74
- controller . deployApp ( invalidData ) . subscribe ( {
75
- error : ( err ) => {
76
- expect ( err ) . toBeInstanceOf ( BadRequestException ) ;
77
- done ( ) ;
78
- } ,
100
+ function passDeployment ( ) {
101
+ // automatically finish deployment
102
+ jest . spyOn ( lines , 'subscribe' ) . mockImplementation ( ( fn ) => {
103
+ setTimeout ( ( ) => fn ( 'DONE' ) ) ;
104
+ return { unsubscribe : ( ) => undefined } as any ;
79
105
} ) ;
106
+ }
107
+
108
+ it ( 'should throw bad request exception if username has wrong format' , async ( ) => {
109
+ passDeployment ( ) ;
110
+ function testName ( username : string ) {
111
+ return lastValueFrom (
112
+ controller . deployApp ( { ...deploymentData , username } ) ,
113
+ ) ;
114
+ }
115
+
116
+ await expect ( testName ( 'with space' ) ) . rejects . toBeInstanceOf (
117
+ BadRequestException ,
118
+ ) ;
119
+ await expect ( testName ( 'withCapital' ) ) . resolves . toBeTruthy ( ) ;
120
+ await expect ( testName ( 'withSymbol?' ) ) . rejects . toBeInstanceOf (
121
+ BadRequestException ,
122
+ ) ;
123
+ await expect ( testName ( 'withNumber123' ) ) . resolves . toBeTruthy ( ) ;
124
+ await expect ( testName ( 'with-dash' ) ) . resolves . toBeTruthy ( ) ;
125
+ await expect ( testName ( 'with_underscore' ) ) . rejects . toBeInstanceOf (
126
+ BadRequestException ,
127
+ ) ;
80
128
} ) ;
81
129
82
- it ( 'should throw error if ERROR is written to log' , ( done ) => {
83
- controller . deployApp ( deploymentData ) . subscribe ( {
84
- error : ( err : BadRequestException ) => {
85
- expect ( err ) . toBeInstanceOf ( BadRequestException ) ;
86
- expect ( err . message ) . toBe ( 'my custom error' ) ;
87
- // Ensure tail is properly "unwatched"
88
- expect ( onSubscription . closed ) . toBeTruthy ( ) ;
89
- done ( ) ;
90
- } ,
91
- } ) ;
130
+ it ( 'should throw bad request exception if email has wrong format' , async ( ) => {
131
+ passDeployment ( ) ;
132
+ function testMail ( email : string ) {
133
+ return lastValueFrom ( controller . deployApp ( { ...deploymentData , email } ) ) ;
134
+ }
135
+
136
+ await expect ( testMail ( 'testmail' ) ) . rejects . toBeInstanceOf (
137
+ BadRequestException ,
138
+ ) ;
139
+ await expect ( testMail ( 'test@mail' ) ) . rejects . toBeInstanceOf (
140
+ BadRequestException ,
141
+ ) ;
142
+ await expect ( testMail ( 'Test@[email protected] ' ) ) . rejects . toBeInstanceOf (
143
+ BadRequestException ,
144
+ ) ;
145
+ await expect ( testMail ( '[email protected] ' ) ) . resolves . toBeTruthy ( ) ;
146
+ await expect ( testMail ( '[email protected] ' ) ) . resolves . toBeTruthy ( ) ;
147
+ await expect ( testMail ( '[email protected] ' ) ) . resolves . toBeTruthy ( ) ;
148
+ await expect ( testMail ( '[email protected] ' ) ) . resolves . toBeTruthy ( ) ;
149
+ await expect ( testMail ( '[email protected] ' ) ) . resolves . toBeTruthy ( ) ;
150
+ } ) ;
92
151
152
+ it ( 'should throw error if ERROR is written to log' , async ( ) => {
153
+ const res = firstValueFrom ( controller . deployApp ( deploymentData ) ) ;
93
154
lines . next ( 'some logs' ) ;
94
155
lines . next ( 'ERROR my custom error' ) ;
95
- } ) ;
96
156
97
- it ( 'should write arguments to file' , ( done ) => {
98
- controller . deployApp ( deploymentData ) . subscribe ( ( ) => {
99
- expect ( mockWs . write ) . toHaveBeenCalledWith (
100
- 'test-name de [email protected] test-username test-base y n' ,
101
- ) ;
102
- expect ( mockWs . close ) . toHaveBeenCalled ( ) ;
157
+ try {
158
+ await res ;
159
+ } catch ( err ) {
160
+ expect ( err ) . toBeInstanceOf ( BadRequestException ) ;
161
+ expect ( err . message ) . toBe ( 'my custom error' ) ;
103
162
// Ensure tail is properly "unwatched"
104
163
expect ( onSubscription . closed ) . toBeTruthy ( ) ;
105
- done ( ) ;
106
- } ) ;
164
+ return ;
165
+ }
166
+
167
+ throw new Error ( 'No error thrown' ) ;
168
+ } ) ;
169
+
170
+ it ( 'should write arguments to file' , ( ) => {
171
+ const res = firstValueFrom ( controller . deployApp ( deploymentData ) ) ;
107
172
108
173
lines . next ( 'DONE' ) ;
174
+
175
+ expect ( res ) . resolves . toBeTruthy ( ) ;
176
+ expect ( mockWs . write ) . toHaveBeenCalledWith (
177
+ 'test-name de [email protected] test-username test-base y n' ,
178
+ ) ;
179
+ expect ( mockWs . close ) . toHaveBeenCalled ( ) ;
180
+ // Ensure tail is properly "unwatched"
181
+ expect ( onSubscription . closed ) . toBeTruthy ( ) ;
109
182
} ) ;
110
183
111
184
it ( 'should use the default locale if empty' , ( done ) => {
0 commit comments