/*
 * Copyright 2002-2020 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package example;

import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlForm;
import com.gargoylesoftware.htmlunit.html.HtmlInput;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import com.gargoylesoftware.htmlunit.html.HtmlSubmitInput;
import org.junit.jupiter.api.Test;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.mock.web.MockHttpSession;
import org.springframework.test.web.servlet.MockMvc;

import static org.assertj.core.api.Assertions.assertThat;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.model;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.redirectedUrl;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

@SpringBootTest
@AutoConfigureMockMvc
public class Saml2LoginApplicationITests {

	static final String SIGNED_RESPONSE = "[encoded data, 10384 characters]";

	static final Map<String, List<Object>> USER_ATTRIBUTES = new LinkedHashMap<>();

	static {
		USER_ATTRIBUTES.put("uid", Arrays.asList("testuser@spring.security.saml"));
		USER_ATTRIBUTES.put("eduPersonAffiliation", Arrays.asList("member", "user"));
		USER_ATTRIBUTES.put("emailAddress", Arrays.asList("testuser@spring.security.saml"));
	}

	@Autowired
	MockMvc mvc;

	@Autowired
	WebClient webClient;

	@Test
	void indexWhenSamlResponseThenShowsUserInformation() throws Exception {
		HttpSession session = this.mvc.perform(get("http://localhost:8080/")).andExpect(status().is3xxRedirection())
				.andExpect(redirectedUrl("http://localhost:8080/saml2/authenticate/one")).andReturn().getRequest()
				.getSession();

		this.mvc.perform(post("http://localhost:8080/login/saml2/sso/one").param("SAMLResponse", SIGNED_RESPONSE)
				.session((MockHttpSession) session)).andExpect(redirectedUrl("http://localhost:8080/"));

		this.mvc.perform(get("http://localhost:8080/").session((MockHttpSession) session))
				.andExpect(model().attribute("emailAddress", "testuser@spring.security.saml"))
				.andExpect(model().attribute("userAttributes", USER_ATTRIBUTES));
	}

	@Test
	void authenticationAttemptWhenValidThenShowsUserEmailAddress() throws Exception {
		HtmlPage assertingParty = this.webClient.getPage("/");
		HtmlForm form = assertingParty.getFormByName("f");
		HtmlInput username = form.getInputByName("username");
		HtmlInput password = form.getInputByName("password");
		HtmlSubmitInput submit = assertingParty.getHtmlElementById("submit_button");
		username.setValueAttribute("user");
		password.setValueAttribute("password");
		HtmlPage relyingParty = submit.click();
		assertThat(relyingParty.asText()).contains("You're email address is testuser@spring.security.saml");
	}

}
