Coder Social home page Coder Social logo

Comments (4)

ch4mpy avatar ch4mpy commented on June 14, 2024

Hi @v20gupta , may I ask you your procedure to create this issue? I thought I had set-up issues auto assignement, but apparently there is a path I missed.

Keycloak security does work on service layer. Unit tests would not pass otherwise.

Could you please provide me with a complete sample?
I suspect your test configuration to be broken. I suspect @ExtendWith(MockitoExtension.class) to be guilty. Maybe should you use SpringExtension for spring magic to happen?

from spring-addons.

v20gupta avatar v20gupta commented on June 14, 2024

Hi @ch4mpy , I was trying to unit test the keycloak security at the Service layer but my test case are passing even if I have not provide them the role.

@Service
public class AirportService {

	@Autowired
	private AirportDao airportDao;

	private final AirportMapper airportMapper = Mappers.getMapper( AirportMapper.class );

	@PreAuthorize(hasRole('std_airport_create'))
	public AirportRespDto createAirport(AirportReqDto airportReqDto) {
		Airport airport = airportMapper.airportDtoToAirport(airportReqDto);
		return airportMapper.airportToAirportDto(airportDao.save(airport));
	}
}

ServiceTesting:

@ExtendWith(SpringExtension.class)
@Import({AirportServiceTest.TestConfig.class})
class AirportServiceTest {

    @MockBean
    private AirportDao airportDao;

    @Autowired
    private AirportService airportService;

    private AirportReqDto airportReqDto1;
    private AirportReqDto airportReqDto2;
    private Airport airport1;
    private Airport airport2;
    private List<Airport> airportList;
    private List<AirportRespDto> airportRespList;
    private AirportMapper airportMapper;

    @BeforeEach
    public void setUp() {
        airportMapper = Mappers.getMapper( AirportMapper.class );
        airportList = new ArrayList<>();
        airportReqDto1 = AirportReqDto.builder().name("DelhiAirport").country("IND")
                .code("IND01").build();
        airportReqDto2 = AirportReqDto.builder().name("MumbaiAirport").country("IND")
                .code("IND01").build();

        airport1 = airportMapper.airportDtoToAirport(airportReqDto1);
        airport2 = airportMapper.airportDtoToAirport(airportReqDto2);
        airportList.addAll(Stream.of(airport1, airport2).collect(Collectors.toList()));
        airportRespList = airportMapper.airportsToAirportsDto(airportList);


    }
    @AfterEach
    public void tearDown() {
        airportReqDto1 = airportReqDto2 = null;
        airport1 = airport2 = null;
        airportList = null;
    }

    @Test
    @WithMockKeycloakAuth(authorities = "ROLE_std_airport_rea", id = @IdTokenClaims(sub = "abc"))
    public void createAirport() {
        Mockito.when(airportDao.save(any())).thenReturn(airport1);
        airportService.createAirport(airportReqDto1);
        verify(airportDao,times(1)).save(any());
    }

    @TestConfiguration(proxyBeanMethods = false)
    @Import({ AirportService.class })
    public static class TestConfig {
        @Bean
        public GrantedAuthoritiesMapper authoritiesMapper() {
            return new SimpleAuthorityMapper();
        }

    }

}

from spring-addons.

ch4mpy avatar ch4mpy commented on June 14, 2024
  • you haven't answered my 1st question (how have you proceeded to create this issue?)
  • this is not a complete sample
  • markdown is not used properly which makes your comments hardly readable (I edited it, but plz learn markdown)
  • you have removed @EnableGlobalMethodSecurity(prePostEnabled = true) from your test conf. You shouldn't.

from spring-addons.

v20gupta avatar v20gupta commented on June 14, 2024

Thanks ch4mpy issue is resolved with adding @EnableGlobalMethodSecurity(prePostEnabled = true)

from spring-addons.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.