package org.ligoj.app.resource.security;

import javax.transaction.Transactional;
import javax.ws.rs.core.Response;

import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.ligoj.app.AbstractServerTest;
import org.ligoj.app.iam.IamProvider;
import org.mockito.ArgumentMatchers;
import org.mockito.Mockito;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.core.Authentication;
import org.springframework.test.annotation.Rollback;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

/**
 * Test of {@link SecurityResource}
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:/META-INF/spring/application-context-test.xml")
@Transactional
@Rollback
public class SecurityResourceTest extends AbstractServerTest {

	@Test
	public void loginUnknownUser() throws Exception {
		thrown.expect(BadCredentialsException.class);
		final User user = new User();
		user.setName("any");
		user.setPassword("any");
		final IamProvider iamProvider = Mockito.mock(IamProvider.class);
		Mockito.when(iamProvider.authenticate(ArgumentMatchers.any(Authentication.class))).thenThrow(new BadCredentialsException("any"));
		final SecurityResource resource = new SecurityResource();
		applicationContext.getAutowireCapableBeanFactory().autowireBean(resource);
		resource.iamProvider = new IamProvider[] { iamProvider };
		resource.login(user);
	}

	@Test
	public void login() throws Exception {
		final IamProvider iamProvider = Mockito.mock(IamProvider.class);
		Mockito.when(iamProvider.authenticate(ArgumentMatchers.any(Authentication.class))).thenAnswer(i -> i.getArguments()[0]);
		final SecurityResource resource = new SecurityResource();
		applicationContext.getAutowireCapableBeanFactory().autowireBean(resource);
		resource.iamProvider = new IamProvider[] { iamProvider };
		final Response login = resource.login(new User("fdauganA", "Azerty01"));
		Assert.assertNotNull(login);
		Assert.assertEquals(204, login.getStatus());
	}

}

@@ -35,16 +35,47 @@
 urity;%0A%0A
+import java.util.Collections;%0A%0A
 import j
@@ -259,24 +259,77 @@
 ServerTest;%0A
+import org.ligoj.app.iam.IAuthenticationContributor;%0A
 import org.l
@@ -419,16 +419,71 @@
 ockito;%0A
+import org.springframework.context.ApplicationContext;%0A
 import o
@@ -1720,16 +1720,48 @@
 ption %7B%0A
+%09%09%0A%09%09// Mock the authentication%0A
 %09%09final 
@@ -1996,32 +1996,38 @@
 tyResource();%0A%09%09
+super.
 applicationConte
@@ -2144,16 +2144,459 @@
 vider %7D;
+%0A%0A%09%09// Mock the contributor%0A%09%09final IAuthenticationContributor contributor = Mockito.mock(IAuthenticationContributor.class);%0A%09%09final ApplicationContext applicationContext = Mockito.mock(ApplicationContext.class);%0A%09%09Mockito.when(applicationContext.getBeansOfType(IAuthenticationContributor.class))%0A%09%09%09%09.thenAnswer(i -%3E Collections.singletonMap(%22some%22, contributor));%0A%09%09resource.applicationContext = applicationContext;%0A%09%09%0A%09%09// Perform the login
 %0A%09%09final
@@ -2663,16 +2663,43 @@
 y01%22));%0A
+%09%09%0A%09%09// Check the response%0A
 %09%09Assert
@@ -2768,14 +2768,147 @@
 tus());%0A
+%09%09%0A%09%09// Check the contributor has been involed%0A%09%09Mockito.verify(contributor).accept(ArgumentMatchers.any(), ArgumentMatchers.any());%0A
 %09%7D%0A%0A%7D%0A

Test with no authentication contributors