package au.gov.ga.geodesy.gws.systemtest;

import static io.restassured.RestAssured.given;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.io.Resource;
import org.springframework.http.HttpStatus;
import org.testng.annotations.Test;

/**
 * System tests for secure upload of GeodesyML site log documents.
 */
public class AuthorizationSystemTest extends BaseSystemTest {

    private static final Logger log = LoggerFactory.getLogger(AuthorizationSystemTest.class);

    @Test
    public void uploadUnauthenticated() throws Exception {
        Resource alic = SystemTestResources.siteLog("alic*");
        log.info("Uploading " + alic.getURL() + " to " + getConfig().getWebServicesUrl());
        given()
            .body(readResource(alic))
            .when()
            .post(getConfig().getWebServicesUrl() + "/siteLogs/upload")
            .then()
            .statusCode(HttpStatus.UNAUTHORIZED.value());
    }

    @Test
    public void uploadUnauthorized() throws Exception {
        Resource alic = SystemTestResources.siteLog("alic*");
        log.info("Uploading " + alic.getURL() + " to " + getConfig().getWebServicesUrl());
        given()
            .header("Authorization", "Bearer " + super.userAToken())
            .body(readResource(alic))
            .when()
            .post(getConfig().getWebServicesUrl() + "/siteLogs/upload")
            .then()
            .statusCode(HttpStatus.FORBIDDEN.value());
    }

    @Test
    public void uploadAuthorized() throws Exception {
        Resource ade1 = SystemTestResources.siteLog("ade1*");
        log.info("Uploading " + ade1.getURL() + " to " + getConfig().getWebServicesUrl());
        given()
            .header("Authorization", "Bearer " + super.userAToken())
            .body(readResource(ade1))
            .when()
            .post(getConfig().getWebServicesUrl() + "/siteLogs/upload")
            .then()
            .statusCode(HttpStatus.CREATED.value());
    }
}

@@ -85,16 +85,45 @@
 given;%0A%0A
+import java.io.IOException;%0A%0A
 import o
@@ -1532,26 +1532,141 @@
  uploadA
-uthorized(
+de1() throws Exception %7B%0A        upload(%22ade1%22, super.userAToken());%0A    %7D%0A%0A    private void upload(String siteId, String jwt
 ) throws
@@ -1658,32 +1658,34 @@
 ing jwt) throws 
+IO
 Exception %7B%0A    
@@ -1697,20 +1697,23 @@
 esource 
-ade1
+siteLog
  = Syste
@@ -1735,21 +1735,26 @@
 siteLog(
-%22ade1
+siteId + %22
 *%22);%0A   
@@ -1782,20 +1782,23 @@
 ing %22 + 
-ade1
+siteLog
 .getURL(
@@ -1909,34 +1909,19 @@
 rer %22 + 
-super.userAToken()
+jwt
 )%0A      
@@ -1945,20 +1945,23 @@
 esource(
-ade1
+siteLog
 ))%0A     

Refactor system test to support more site logs