package GameNationBackEnd.Security;

import GameNationBackEnd.Repositories.UserRepository;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.json.JacksonTester;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.context.WebApplicationContext;

import java.io.IOException;

/**
 * Created by lucas on 23/11/2016.
 */

@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class AuhenticationTest {

    @Autowired
    private WebApplicationContext webApplicationContext;

    @Autowired
    private TestRestTemplate restTemplate;

    @Autowired
    private UserRepository userRepository;

    HttpHeaders headers = new HttpHeaders();

    @Before
    public void setup() {
        headers.setContentType(MediaType.APPLICATION_JSON);
        restTemplate.getRestTemplate().getMessageConverters().add(new MappingJackson2HttpMessageConverter());
        restTemplate.getRestTemplate().getMessageConverters().add(new StringHttpMessageConverter());
    }

    @Test
    public void getAuthenticationToken() throws IOException {
        MultiValueMap<String, String> postParams = new LinkedMultiValueMap<String, String>();
        postParams.add("username", "test");
        postParams.add("password", "test");
        postParams.add("grant_type", "password");

//        restTemplate.postForEntity()

        String res = restTemplate
                .withBasicAuth("web-gamenation", "123456")
                .postForObject("/oauth/token", postParams, String.class);
    }

    class OAuthRequest {
        public String username;
        public String password;
        public String grant_type;

        public String GetUsername() {return username;}
        public String GetPassword() {return password;}
        public String GetGrant_type() {return grant_type;}
    }

    class OAuthResponse {
        String access_token;
        String token_type;
        String refresh_token;
        String expires_in;
        String scope;
    }
}

@@ -109,16 +109,41 @@
 Before;%0A
+import org.junit.Ignore;%0A
 import o
@@ -880,32 +880,95 @@
 .MultiValueMap;%0A
+import org.springframework.web.client.ResourceAccessException;%0A
 import org.sprin
@@ -1827,16 +1827,20 @@
 ionToken
+Test
 () throw
@@ -2310,463 +2310,450 @@
     
-class OAuthRequest %7B%0A        public String username;%0A        public String password;%0A        public String grant_type;%0A%0A        public String GetUsername() %7Breturn username;%7D%0A        public String GetPassword() %7Breturn password;%7D%0A        public String GetGrant_type() %7Breturn grant_type;%7D
+@Ignore%0A    @Test(expected = ResourceAccessException.class)%0A    public void unauthorizedTest() %7B%0A        MultiValueMap%3CString, String%3E postParams = new LinkedMultiValueMap%3CString, String%3E();%0A        postParams.add(%22name%22, %22testgame%22);
 %0A    
-%7D%0A%0A
     
-class OAuthResponse %7B%0A        String access_token;%0A        String token_type;%0A        String refresh_token;%0A        String expires_in;%0A        String scope
+postParams.add(%22image%22, %22testimage%22);%0A        postParams.add(%22description%22, %22aweosome game%22);%0A%0A        String res = restTemplate%0A                .postForObject(%22/api/games%22, postParams, String.class)
 ;%0A  

Add unauthorized test, ignored atm