// Path: mujina-sp/src/test/java/mujina/AbstractIntegrationTest.java
// @RunWith(SpringRunner.class)
// @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
// public abstract class AbstractIntegrationTest {
// 
//   @Autowired
//   protected SpConfiguration spConfiguration;
// 
//   @LocalServerPort
//   protected int serverPort;
// 
//   @Before
//   public void before() throws Exception {
//     RestAssured.port = serverPort;
//     given()
//       .header("Content-Type", "application/json")
//       .post("/api/reset")
//       .then()
//       .statusCode(SC_OK);
//   }
// 
//   protected CookieFilter login() throws IOException {
//     CookieFilter cookieFilter = new CookieFilter();
// 
//     String html = given()
//       .filter(cookieFilter)
//       .get("/login")
//       .getBody().asString();
// 
//     Matcher matcher = Pattern.compile("name=\"SAMLRequest\" value=\"(.*?)\"").matcher(html);
//     matcher.find();
//     String samlRequest = new String(Base64.getDecoder().decode(matcher.group(1)));
// 
//     //Now mimic a response message
//     String samlResponse = getIdPSAMLResponse(samlRequest);
//     given()
//       .formParam("SAMLResponse", Base64.getEncoder().encodeToString(samlResponse.getBytes()))
//       .filter(cookieFilter)
//       .post("/saml/SSO")
//       .then()
//       .statusCode(SC_MOVED_TEMPORARILY);
// 
//     return cookieFilter;
//   }
// 
//   private String getIdPSAMLResponse(String saml) throws IOException {
//     Matcher matcher = Pattern.compile("ID=\"(.*?)\"").matcher(saml);
//     assertTrue(matcher.find());
// 
//     //We need the ID of the original request to mimic the real IdP authnResponse
//     String inResponseTo = matcher.group(1);
// 
//     ZonedDateTime date = ZonedDateTime.now();
//     String now = date.format(DateTimeFormatter.ISO_INSTANT);
//     String samlResponse = IOUtils.toString(new ClassPathResource("saml_response.xml").getInputStream(), Charset.defaultCharset());
// 
//     samlResponse = samlResponse
//       .replaceAll("@@IssueInstant@@", now)
//       .replaceAll("@@InResponseTo@@", inResponseTo);
//     return samlResponse;
//   }
// 
// }
// 
// Path: mujina-idp/src/main/java/mujina/api/AuthenticationMethod.java
// public enum AuthenticationMethod {
//   ALL, USER
// }


import io.restassured.filter.cookie.CookieFilter;
import mujina.AbstractIntegrationTest;
import mujina.api.AuthenticationMethod;
import org.junit.Test;
import org.springframework.test.context.TestPropertySource;
import static io.restassured.RestAssured.given;
import static org.apache.http.HttpStatus.SC_MOVED_TEMPORARILY;
import static org.apache.http.HttpStatus.SC_OK;
import static org.hamcrest.core.StringContains.containsString;
package mujina.idp;



@TestPropertySource(properties = {"idp.expires:" + (Integer.MAX_VALUE / 2 - 1), "idp.clock_skew: " + (Integer.MAX_VALUE / 2 - 1)})
public class AuthenticationProviderTest extends AbstractIntegrationTest {

  @Test
  public void authenticateAuthMethodAll() throws Exception {

    given()
      .body(AuthenticationMethod.ALL)
      .header("Content-Type", "application/json")
      .put("/api/authmethod")
      .then()
      .statusCode(SC_OK);

    CookieFilter cookieFilter = login("qwerty", "", SC_MOVED_TEMPORARILY);


    given()
      .filter(cookieFilter)
      .get("/user.html")
// Path: mujina-sp/src/test/java/mujina/AbstractIntegrationTest.java
// @RunWith(SpringRunner.class)
// @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
// public abstract class AbstractIntegrationTest {
// 
//   @Autowired
//   protected SpConfiguration spConfiguration;
// 
//   @LocalServerPort
//   protected int serverPort;
// 
//   @Before
//   public void before() throws Exception {
//     RestAssured.port = serverPort;
//     given()
//       .header("Content-Type", "application/json")
//       .post("/api/reset")
//       .then()
//       .statusCode(SC_OK);
//   }
// 
//   protected CookieFilter login() throws IOException {
//     CookieFilter cookieFilter = new CookieFilter();
// 
//     String html = given()
//       .filter(cookieFilter)
//       .get("/login")
//       .getBody().asString();
// 
//     Matcher matcher = Pattern.compile("name=\"SAMLRequest\" value=\"(.*?)\"").matcher(html);
//     matcher.find();
//     String samlRequest = new String(Base64.getDecoder().decode(matcher.group(1)));
// 
//     //Now mimic a response message
//     String samlResponse = getIdPSAMLResponse(samlRequest);
//     given()
//       .formParam("SAMLResponse", Base64.getEncoder().encodeToString(samlResponse.getBytes()))
//       .filter(cookieFilter)
//       .post("/saml/SSO")
//       .then()
//       .statusCode(SC_MOVED_TEMPORARILY);
// 
//     return cookieFilter;
//   }
// 
//   private String getIdPSAMLResponse(String saml) throws IOException {
//     Matcher matcher = Pattern.compile("ID=\"(.*?)\"").matcher(saml);
//     assertTrue(matcher.find());
// 
//     //We need the ID of the original request to mimic the real IdP authnResponse
//     String inResponseTo = matcher.group(1);
// 
//     ZonedDateTime date = ZonedDateTime.now();
//     String now = date.format(DateTimeFormatter.ISO_INSTANT);
//     String samlResponse = IOUtils.toString(new ClassPathResource("saml_response.xml").getInputStream(), Charset.defaultCharset());
// 
//     samlResponse = samlResponse
//       .replaceAll("@@IssueInstant@@", now)
//       .replaceAll("@@InResponseTo@@", inResponseTo);
//     return samlResponse;
//   }
// 
// }
// 
// Path: mujina-idp/src/main/java/mujina/api/AuthenticationMethod.java
// public enum AuthenticationMethod {
//   ALL, USER
// }


// Path: mujina-idp/src/test/java/mujina/idp/AuthenticationProviderTest.java
import io.restassured.filter.cookie.CookieFilter;
import mujina.AbstractIntegrationTest;
import mujina.api.AuthenticationMethod;
import org.junit.Test;
import org.springframework.test.context.TestPropertySource;
import static io.restassured.RestAssured.given;
import static org.apache.http.HttpStatus.SC_MOVED_TEMPORARILY;
import static org.apache.http.HttpStatus.SC_OK;
import static org.hamcrest.core.StringContains.containsString;

package mujina.idp;



@TestPropertySource(properties = {"idp.expires:" + (Integer.MAX_VALUE / 2 - 1), "idp.clock_skew: " + (Integer.MAX_VALUE / 2 - 1)})
public class AuthenticationProviderTest extends AbstractIntegrationTest {

  @Test
  public void authenticateAuthMethodAll() throws Exception {

    given()
      .body(AuthenticationMethod.ALL)
      .header("Content-Type", "application/json")
      .put("/api/authmethod")
      .then()
      .statusCode(SC_OK);

    CookieFilter cookieFilter = login("qwerty", "", SC_MOVED_TEMPORARILY);


    given()
      .filter(cookieFilter)
      .get("/user.html")
      .then()