package fm.pattern.tokamak.authorization;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.when;

import org.assertj.core.api.Assertions;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.springframework.security.authentication.AnonymousAuthenticationToken;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.oauth2.provider.OAuth2Authentication;

public class OAuth2AuthorizationContextTest {

	@Mock
	private SecurityContext context;

	@Mock
	private OAuth2Authentication authentication;

	@Mock
	private AnonymousAuthenticationToken anonymousAuthentication;

	@Before
	public void before() {
		MockitoAnnotations.initMocks(this);

		when(context.getAuthentication()).thenReturn(authentication);
		SecurityContextHolder.setContext(context);
	}

	@Test
	public void shouldBeAuthenticatedWhenTheUnderlyingAuthenticationIsAuthenticated() {
		when(authentication.isAuthenticated()).thenReturn(true);

		OAuth2AuthorizationContext ctx = new OAuth2AuthorizationContext();
		Assertions.assertThat(ctx.isAuthenticated()).isTrue();
	}

	@Test
	public void shouldNotBeAuthenticatedWhenTheUnderlyingAuthenticationIsNotAuthenticated() {
		when(authentication.isAuthenticated()).thenReturn(false);

		OAuth2AuthorizationContext ctx = new OAuth2AuthorizationContext();
		assertThat(ctx.isAuthenticated()).isFalse();
	}

	@Test
	public void shouldNotBeAuthenticatedWhenTheUnderlyingAuthenticationIsNull() {
		when(context.getAuthentication()).thenReturn(null);
		SecurityContextHolder.setContext(context);

		OAuth2AuthorizationContext ctx = new OAuth2AuthorizationContext();
		assertThat(ctx.isAuthenticated()).isFalse();
	}

	@Test
	public void shouldNotBeAuthenticatedWhenTheUnderlyingAuthenticationIsNotAnOAuth2Authentication() {
		when(context.getAuthentication()).thenReturn(anonymousAuthentication);
		SecurityContextHolder.setContext(context);

		OAuth2AuthorizationContext ctx = new OAuth2AuthorizationContext();
		assertThat(ctx.isAuthenticated()).isFalse();
	}

}

@@ -579,16 +579,83 @@
 ication;
+%0Aimport org.springframework.security.oauth2.provider.OAuth2Request;
 %0A%0Apublic
@@ -862,16 +862,56 @@
 ation;%0A%0A
+%09@Mock%0A%09private OAuth2Request request;%0A%0A
 %09@Before
@@ -2300,10 +2300,379 @@
 ();%0A%09%7D%0A%0A
+%09@Test%0A%09public void shouldReturnAnEmptySetOfScopesIfTheAuthenticationHasNoScopes() %7B%0A%09%09when(authentication.isAuthenticated()).thenReturn(true);%0A%09%09when(authentication.getOAuth2Request()).thenReturn(request);%0A%0A%09%09OAuth2AuthorizationContext ctx = new OAuth2AuthorizationContext();%0A%09%09assertThat(ctx.isAuthenticated()).isTrue();%0A%09%09assertThat(ctx.getScopes()).hasSize(0);%0A%09%7D%0A%0A
 %7D%0A

Test updates