1
1
package io .securecodebox .persistence .defectdojo .service ;
2
2
3
3
import io .securecodebox .persistence .defectdojo .config .DefectDojoConfig ;
4
+ import io .securecodebox .persistence .defectdojo .service .DefaultImportScanService .MissingProxyAuthenticationConfig ;
5
+ import io .securecodebox .persistence .defectdojo .service .ImportScanService .ProxyConfigNames ;
6
+ import org .junit .jupiter .api .AfterEach ;
7
+ import org .junit .jupiter .api .BeforeEach ;
4
8
import org .junit .jupiter .api .Test ;
5
9
import org .springframework .http .HttpHeaders ;
6
10
11
+ import java .util .HashMap ;
12
+ import java .util .Map ;
13
+
7
14
import static org .junit .jupiter .api .Assertions .*;
8
15
import static org .hamcrest .MatcherAssert .assertThat ;
9
16
12
19
*/
13
20
class DefaultImportScanServiceTest {
14
21
private final DefectDojoConfig config = new DefectDojoConfig (
15
- "url" ,
16
- "apiKey" ,
17
- "username" ,
18
- 23 ,
19
- 42L
22
+ "url" ,
23
+ "apiKey" ,
24
+ "username" ,
25
+ 23 ,
26
+ 42L
20
27
);
21
28
private final DefaultImportScanService sut = new DefaultImportScanService (config );
29
+ /**
30
+ * Since System.getProperty() is an side effect we need to back up and restore it to isolate test cases.
31
+ */
32
+ private final Map <ProxyConfigNames , String > backup = new HashMap <>();
33
+
34
+ @ BeforeEach
35
+ void backupSystemProperties () {
36
+ backup .clear ();
37
+ backup .put (ProxyConfigNames .HTTP_PROXY_HOST , System .getProperty (ProxyConfigNames .HTTP_PROXY_HOST .getLiterat ()));
38
+ backup .put (ProxyConfigNames .HTTP_PROXY_PORT , System .getProperty (ProxyConfigNames .HTTP_PROXY_PORT .getLiterat ()));
39
+ backup .put (ProxyConfigNames .HTTP_PROXY_USER , System .getProperty (ProxyConfigNames .HTTP_PROXY_USER .getLiterat ()));
40
+ backup .put (ProxyConfigNames .HTTP_PROXY_PASSWORD , System .getProperty (ProxyConfigNames .HTTP_PROXY_PASSWORD .getLiterat ()));
41
+ }
42
+
43
+ @ AfterEach
44
+ void restoreSystemProperties () {
45
+ for (final var entry : backup .entrySet ()) {
46
+ final var name = entry .getKey ().getLiterat ();
47
+
48
+ if (null == entry .getValue ()) {
49
+ System .clearProperty (name );
50
+ } else {
51
+ System .setProperty (name , entry .getValue ());
52
+ }
53
+ }
54
+ }
22
55
23
56
@ Test
24
57
void constructorShouldThrowExceptionOnNullConfig () {
@@ -31,8 +64,112 @@ void constructorShouldThrowExceptionOnNullConfig() {
31
64
void createDefectDojoAuthorizationHeaders_apiKeyFromConfigShouldBePresentAsAuthHEader () {
32
65
final var authorizationHeaders = sut .createDefectDojoAuthorizationHeaders ();
33
66
assertAll (
34
- () -> assertEquals (1 , authorizationHeaders .size (), "Expected is exactly one authorization header!" ),
35
- () -> assertEquals ("Token apiKey" , authorizationHeaders .get (HttpHeaders .AUTHORIZATION ).get (0 ))
67
+ () -> assertEquals (1 , authorizationHeaders .size (), "Expected is exactly one authorization header!" ),
68
+ () -> assertEquals ("Token apiKey" , authorizationHeaders .get (HttpHeaders .AUTHORIZATION ).get (0 ))
36
69
);
37
70
}
71
+
72
+ @ Test
73
+ void shouldConfigureProxySettings_falseIfNeitherUserNorPasswordIsSet () {
74
+ System .clearProperty (ProxyConfigNames .HTTP_PROXY_USER .getLiterat ());
75
+ System .clearProperty (ProxyConfigNames .HTTP_PROXY_PASSWORD .getLiterat ());
76
+
77
+ assertFalse (sut .shouldConfigureProxySettings ());
78
+ }
79
+
80
+ @ Test
81
+ void shouldConfigureProxySettings_falseIfUserSetButPasswordNot () {
82
+ System .setProperty (ProxyConfigNames .HTTP_PROXY_USER .getLiterat (), "user" );
83
+ System .clearProperty (ProxyConfigNames .HTTP_PROXY_PASSWORD .getLiterat ());
84
+
85
+ assertFalse (sut .shouldConfigureProxySettings ());
86
+ }
87
+
88
+ @ Test
89
+ void shouldConfigureProxySettings_falseIfPasswordSetButUserNot () {
90
+ System .clearProperty (ProxyConfigNames .HTTP_PROXY_USER .getLiterat ());
91
+ System .setProperty (ProxyConfigNames .HTTP_PROXY_PASSWORD .getLiterat (), "password" );
92
+
93
+ assertFalse (sut .shouldConfigureProxySettings ());
94
+ }
95
+
96
+ @ Test
97
+ void shouldConfigureProxySettings_trueIfUserAndPasswordSet () {
98
+ System .setProperty (ProxyConfigNames .HTTP_PROXY_USER .getLiterat (), "user" );
99
+ System .setProperty (ProxyConfigNames .HTTP_PROXY_PASSWORD .getLiterat (), "password" );
100
+
101
+ assertTrue (sut .shouldConfigureProxySettings ());
102
+ }
103
+
104
+ @ Test
105
+ void createRequestFactoryWithProxyAuthConfig_throesExceptionIfUserNotSet () {
106
+ System .clearProperty (ProxyConfigNames .HTTP_PROXY_USER .getLiterat ());
107
+ System .setProperty (ProxyConfigNames .HTTP_PROXY_PASSWORD .getLiterat (), "password" );
108
+ System .setProperty (ProxyConfigNames .HTTP_PROXY_HOST .getLiterat (), "host" );
109
+ System .setProperty (ProxyConfigNames .HTTP_PROXY_PORT .getLiterat (), "4242" );
110
+
111
+ final var thrown = assertThrows (
112
+ MissingProxyAuthenticationConfig .class ,
113
+ sut ::createRequestFactoryWithProxyAuthConfig );
114
+
115
+ assertEquals ("Expected System property 'http.proxyUser' not set!" , thrown .getMessage ());
116
+ }
117
+
118
+ @ Test
119
+ void createRequestFactoryWithProxyAuthConfig_throesExceptionIfPasswordNotSet () {
120
+ System .setProperty (ProxyConfigNames .HTTP_PROXY_USER .getLiterat (), "user" );
121
+ System .clearProperty (ProxyConfigNames .HTTP_PROXY_PASSWORD .getLiterat ());
122
+ System .setProperty (ProxyConfigNames .HTTP_PROXY_HOST .getLiterat (), "host" );
123
+ System .setProperty (ProxyConfigNames .HTTP_PROXY_PORT .getLiterat (), "4242" );
124
+
125
+ final var thrown = assertThrows (
126
+ MissingProxyAuthenticationConfig .class ,
127
+ sut ::createRequestFactoryWithProxyAuthConfig );
128
+
129
+ assertEquals ("Expected System property 'http.proxyPassword' not set!" , thrown .getMessage ());
130
+ }
131
+
132
+ @ Test
133
+ void createRequestFactoryWithProxyAuthConfig_throesExceptionIfHostNotSet () {
134
+ System .setProperty (ProxyConfigNames .HTTP_PROXY_USER .getLiterat (), "user" );
135
+ System .setProperty (ProxyConfigNames .HTTP_PROXY_PASSWORD .getLiterat (), "password" );
136
+ System .clearProperty (ProxyConfigNames .HTTP_PROXY_HOST .getLiterat ());
137
+ System .setProperty (ProxyConfigNames .HTTP_PROXY_PORT .getLiterat (), "4242" );
138
+
139
+ final var thrown = assertThrows (
140
+ MissingProxyAuthenticationConfig .class ,
141
+ sut ::createRequestFactoryWithProxyAuthConfig );
142
+
143
+ assertEquals ("Expected System property 'http.proxyHost' not set!" , thrown .getMessage ());
144
+ }
145
+
146
+ @ Test
147
+ void createRequestFactoryWithProxyAuthConfig_throesExceptionIfPortNotSet () {
148
+ System .setProperty (ProxyConfigNames .HTTP_PROXY_USER .getLiterat (), "user" );
149
+ System .setProperty (ProxyConfigNames .HTTP_PROXY_PASSWORD .getLiterat (), "password" );
150
+ System .setProperty (ProxyConfigNames .HTTP_PROXY_HOST .getLiterat (), "host" );
151
+ System .clearProperty (ProxyConfigNames .HTTP_PROXY_PORT .getLiterat ());
152
+
153
+ final var thrown = assertThrows (
154
+ MissingProxyAuthenticationConfig .class ,
155
+ sut ::createRequestFactoryWithProxyAuthConfig );
156
+
157
+ assertEquals ("Expected System property 'http.proxyPort' not set!" , thrown .getMessage ());
158
+ }
159
+
160
+ @ Test
161
+ void createRequestFactoryWithProxyAuthConfig_throesExceptionIfPortIsNotInteger () {
162
+ System .setProperty (ProxyConfigNames .HTTP_PROXY_USER .getLiterat (), "user" );
163
+ System .setProperty (ProxyConfigNames .HTTP_PROXY_PASSWORD .getLiterat (), "password" );
164
+ System .setProperty (ProxyConfigNames .HTTP_PROXY_HOST .getLiterat (), "host" );
165
+ System .setProperty (ProxyConfigNames .HTTP_PROXY_PORT .getLiterat (), "FUBAR" );
166
+
167
+ final var thrown = assertThrows (
168
+ IllegalArgumentException .class ,
169
+ sut ::createRequestFactoryWithProxyAuthConfig );
170
+
171
+ assertEquals (
172
+ "Given port for proxy authentication configuration (property 'http.proxyPort') is not a valid number! Given value wa 'FUBAR'." ,
173
+ thrown .getMessage ());
174
+ }
38
175
}
0 commit comments