Skip to content

Commit 873ca6b

Browse files
committed
Added further test cases.
1 parent 32503bf commit 873ca6b

File tree

104 files changed

+5899
-243
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

104 files changed

+5899
-243
lines changed

app/src/main/java/org/dhis2/fhir/adapter/AppConfig.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,15 @@
2929
*/
3030

3131
import org.dhis2.fhir.adapter.converter.ZonedDateTimeToDateConverter;
32+
import org.dhis2.fhir.adapter.dhis.config.DhisEndpointConfig;
33+
import org.dhis2.fhir.adapter.dhis.security.DhisWebApiAuthenticationProvider;
34+
import org.dhis2.fhir.adapter.dhis.security.SecurityConfig;
35+
import org.springframework.boot.web.client.RestTemplateBuilder;
3236
import org.springframework.context.annotation.Bean;
3337
import org.springframework.context.annotation.Configuration;
3438
import org.springframework.format.FormatterRegistry;
3539
import org.springframework.http.HttpMethod;
36-
import org.springframework.validation.annotation.Validated;
40+
import org.springframework.security.authentication.dao.AbstractUserDetailsAuthenticationProvider;
3741
import org.springframework.web.servlet.config.annotation.CorsRegistry;
3842
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
3943

@@ -47,9 +51,15 @@
4751
* @author volsch
4852
*/
4953
@Configuration
50-
@Validated
5154
public class AppConfig
5255
{
56+
@Bean
57+
@Nonnull
58+
public AbstractUserDetailsAuthenticationProvider dhisWebApiAuthenticationProvider( @Nonnull RestTemplateBuilder restTemplateBuilder, @Nonnull DhisEndpointConfig dhisEndpointConfig, @Nonnull SecurityConfig securityConfig )
59+
{
60+
return new DhisWebApiAuthenticationProvider( restTemplateBuilder, dhisEndpointConfig, securityConfig );
61+
}
62+
5363
@Bean
5464
@Nonnull
5565
public WebMvcConfigurer mvcConfigurer()

app/src/main/java/org/dhis2/fhir/adapter/WebSecurityConfig.java

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,10 @@
2828
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2929
*/
3030

31-
import org.dhis2.fhir.adapter.dhis.config.DhisEndpointConfig;
32-
import org.dhis2.fhir.adapter.dhis.security.DhisWebApiAuthenticationProvider;
33-
import org.dhis2.fhir.adapter.dhis.security.SecurityConfig;
3431
import org.dhis2.fhir.adapter.fhir.security.AdapterAuthorities;
35-
import org.springframework.boot.web.client.RestTemplateBuilder;
3632
import org.springframework.context.annotation.Configuration;
3733
import org.springframework.http.HttpMethod;
34+
import org.springframework.security.authentication.dao.AbstractUserDetailsAuthenticationProvider;
3835
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
3936
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
4037
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
@@ -56,17 +53,11 @@ public class WebSecurityConfig extends WebSecurityConfigurerAdapter
5653
{
5754
protected static final String DHIS_BASIC_REALM = "DHIS2";
5855

59-
private final RestTemplateBuilder restTemplateBuilder;
56+
private final AbstractUserDetailsAuthenticationProvider userDetailsAuthenticationProvider;
6057

61-
private final DhisEndpointConfig dhisEndpointConfig;
62-
63-
private final SecurityConfig securityConfig;
64-
65-
public WebSecurityConfig( @Nonnull RestTemplateBuilder restTemplateBuilder, @Nonnull DhisEndpointConfig dhisEndpointConfig, @Nonnull SecurityConfig securityConfig )
58+
public WebSecurityConfig( @Nonnull AbstractUserDetailsAuthenticationProvider userDetailsAuthenticationProvider )
6659
{
67-
this.restTemplateBuilder = restTemplateBuilder;
68-
this.dhisEndpointConfig = dhisEndpointConfig;
69-
this.securityConfig = securityConfig;
60+
this.userDetailsAuthenticationProvider = userDetailsAuthenticationProvider;
7061
}
7162

7263
@Override
@@ -92,9 +83,9 @@ protected void configure( @Nonnull HttpSecurity http ) throws Exception
9283
}
9384

9485
@Override
95-
protected void configure( AuthenticationManagerBuilder auth )
86+
protected void configure( AuthenticationManagerBuilder auth ) throws Exception
9687
{
97-
auth.authenticationProvider( new DhisWebApiAuthenticationProvider( restTemplateBuilder, dhisEndpointConfig, securityConfig ) );
88+
auth.authenticationProvider( userDetailsAuthenticationProvider );
9889
}
9990

10091
@Override

app/src/test/java/org/dhis2/fhir/adapter/AbstractAppTest.java

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
import org.springframework.test.context.TestPropertySource;
5757
import org.springframework.test.web.client.MockRestServiceServer;
5858
import org.springframework.test.web.servlet.MockMvc;
59+
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
5960
import org.springframework.web.client.RestTemplate;
6061

6162
import javax.annotation.Nonnull;
@@ -64,7 +65,6 @@
6465
import java.util.Objects;
6566

6667
import static com.github.tomakehurst.wiremock.client.WireMock.*;
67-
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
6868
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
6969

7070
/**
@@ -153,7 +153,7 @@ public abstract class AbstractAppTest
153153
private long resourceDlQueueCount;
154154

155155
protected void notifyResource( @Nonnull FhirResourceType resourceType, @Nullable String resourceSearchResponse,
156-
@Nullable String resourceId, @Nullable String resourceResponse ) throws Exception
156+
@Nullable String resourceId, @Nullable String resourceResponse, boolean payload ) throws Exception
157157
{
158158
if ( previousResourceSearchStubMapping != null )
159159
{
@@ -178,10 +178,23 @@ protected void notifyResource( @Nonnull FhirResourceType resourceType, @Nullable
178178
.withBody( resourceResponse ) ) );
179179
}
180180

181-
mockMvc.perform( post( "/remote-fhir-rest-hook/{subscriptionId}/{subscriptionResourceId}",
182-
testConfiguration.getRemoteSubscriptionId(), testConfiguration.getRemoteSubscriptionResourceId( resourceType ) )
183-
.header( "Authorization", TestConfiguration.ADAPTER_AUTHORIZATION ) )
184-
.andExpect( status().isOk() );
181+
if ( payload )
182+
{
183+
Assert.assertNotNull( resourceId );
184+
Assert.assertNotNull( resourceResponse );
185+
mockMvc.perform( MockMvcRequestBuilders.put( "/remote-fhir-rest-hook/{subscriptionId}/{subscriptionResourceId}/{resourceType}/{resourceId}",
186+
testConfiguration.getRemoteSubscriptionId(), testConfiguration.getRemoteSubscriptionResourceId( resourceType ),
187+
resourceType.getResourceTypeName(), resourceId ).content( resourceResponse ).contentType( FHIR_JSON_MEDIA_TYPE )
188+
.header( "Authorization", TestConfiguration.ADAPTER_AUTHORIZATION ) )
189+
.andExpect( status().isOk() );
190+
}
191+
else
192+
{
193+
mockMvc.perform( MockMvcRequestBuilders.post( "/remote-fhir-rest-hook/{subscriptionId}/{subscriptionResourceId}",
194+
testConfiguration.getRemoteSubscriptionId(), testConfiguration.getRemoteSubscriptionResourceId( resourceType ) )
195+
.header( "Authorization", TestConfiguration.ADAPTER_AUTHORIZATION ) )
196+
.andExpect( status().isOk() );
197+
}
185198
}
186199

187200
protected void waitForEmptyResourceQueue() throws Exception

app/src/test/java/org/dhis2/fhir/adapter/TestConfiguration.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
*/
3030

3131
import com.github.tomakehurst.wiremock.WireMockServer;
32+
import org.dhis2.fhir.adapter.dhis.security.SecurityConfig;
3233
import org.dhis2.fhir.adapter.fhir.metadata.model.FhirResourceType;
3334
import org.dhis2.fhir.adapter.fhir.metadata.model.SubscriptionType;
3435
import org.dhis2.fhir.adapter.fhir.security.SystemAuthenticationToken;
@@ -47,6 +48,7 @@
4748
import org.springframework.context.annotation.Bean;
4849
import org.springframework.context.annotation.Configuration;
4950
import org.springframework.context.annotation.Primary;
51+
import org.springframework.security.authentication.dao.AbstractUserDetailsAuthenticationProvider;
5052
import org.springframework.security.core.context.SecurityContextHolder;
5153

5254
import javax.annotation.Nonnull;
@@ -128,9 +130,10 @@ public String getDhis2UserAuthorization()
128130

129131
@Nonnull
130132
@Bean
131-
protected WireMockServer fhirMockServer()
133+
@Primary
134+
protected AbstractUserDetailsAuthenticationProvider testDhisWebApiAuthenticationProvider( @Nonnull SecurityConfig securityConfig )
132135
{
133-
return fhirMockServer;
136+
return new TestDhisWebApiAuthenticationProvider( securityConfig );
134137
}
135138

136139
@Nonnull
@@ -141,6 +144,13 @@ protected LockManager embeddedLockManager()
141144
return new EmbeddedLockManagerImpl();
142145
}
143146

147+
@Nonnull
148+
@Bean
149+
protected WireMockServer fhirMockServer()
150+
{
151+
return fhirMockServer;
152+
}
153+
144154
@PostConstruct
145155
protected void postConstruct()
146156
{
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
package org.dhis2.fhir.adapter;
2+
3+
/*
4+
* Copyright (c) 2004-2018, University of Oslo
5+
* All rights reserved.
6+
*
7+
* Redistribution and use in source and binary forms, with or without
8+
* modification, are permitted provided that the following conditions are met:
9+
* Redistributions of source code must retain the above copyright notice, this
10+
* list of conditions and the following disclaimer.
11+
*
12+
* Redistributions in binary form must reproduce the above copyright notice,
13+
* this list of conditions and the following disclaimer in the documentation
14+
* and/or other materials provided with the distribution.
15+
* Neither the name of the HISP project nor the names of its contributors may
16+
* be used to endorse or promote products derived from this software without
17+
* specific prior written permission.
18+
*
19+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
20+
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21+
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22+
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
23+
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24+
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25+
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
26+
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27+
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28+
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29+
*/
30+
31+
import org.dhis2.fhir.adapter.dhis.security.AdapterUser;
32+
import org.dhis2.fhir.adapter.dhis.security.SecurityConfig;
33+
import org.springframework.security.authentication.BadCredentialsException;
34+
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
35+
import org.springframework.security.authentication.dao.AbstractUserDetailsAuthenticationProvider;
36+
import org.springframework.security.core.AuthenticationException;
37+
import org.springframework.security.core.authority.SimpleGrantedAuthority;
38+
import org.springframework.security.core.userdetails.UserDetails;
39+
40+
import javax.annotation.Nonnull;
41+
import java.util.Collections;
42+
43+
import static org.dhis2.fhir.adapter.fhir.security.AdapterAuthorities.*;
44+
45+
/**
46+
* Authentication provider that authenticates the user by
47+
* simulating user management of DHIS2.
48+
*
49+
* @author volsch
50+
*/
51+
public class TestDhisWebApiAuthenticationProvider extends AbstractUserDetailsAuthenticationProvider
52+
{
53+
public static final String CODE_MAPPING_USER = "code_mapping";
54+
55+
public static final String DATA_MAPPING_USER = "data_mapping";
56+
57+
public static final String ADMINISTRATION_USER = "administration";
58+
59+
public static final String ALL_USER = "all";
60+
61+
private final SecurityConfig securityConfig;
62+
63+
public TestDhisWebApiAuthenticationProvider( @Nonnull SecurityConfig securityConfig )
64+
{
65+
this.securityConfig = securityConfig;
66+
}
67+
68+
@Override
69+
protected void additionalAuthenticationChecks( UserDetails userDetails, UsernamePasswordAuthenticationToken authentication ) throws AuthenticationException
70+
{
71+
// all authentication checks have been performed by DHIS2
72+
}
73+
74+
@Override
75+
protected UserDetails retrieveUser( String username, UsernamePasswordAuthenticationToken authentication ) throws AuthenticationException
76+
{
77+
if ( !String.valueOf( authentication.getCredentials() ).equals( username + "_1" ) )
78+
{
79+
throw new BadCredentialsException( "Invalid username or password." );
80+
}
81+
82+
switch ( username )
83+
{
84+
case CODE_MAPPING_USER:
85+
return new AdapterUser( "kea2xl4zZfa", username, Collections.singleton( new SimpleGrantedAuthority( CODE_MAPPING_AUTHORITY_ROLE ) ) );
86+
case DATA_MAPPING_USER:
87+
return new AdapterUser( "kea2xl4zZfb", username, Collections.singleton( new SimpleGrantedAuthority( DATA_MAPPING_AUTHORITY_ROLE ) ) );
88+
case ADMINISTRATION_USER:
89+
return new AdapterUser( "kea2xl4zZfc", username, Collections.singleton( new SimpleGrantedAuthority( ADMINISTRATION_AUTHORITY_ROLE ) ) );
90+
case ALL_USER:
91+
return new AdapterUser( "kea2xl4zZfd", username, ALL_AUTHORITIES );
92+
default:
93+
throw new BadCredentialsException( "Invalid username or password." );
94+
}
95+
}
96+
}

0 commit comments

Comments
 (0)