/*
 * Copyright 2016 Shredder121.
 *
 * 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
 *
 *      http://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 com.github.shredder121.gh_event_api.handler;

import static com.jayway.restassured.RestAssured.given;
import static com.jayway.restassured.http.ContentType.JSON;

import java.util.Map;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.boot.test.WebIntegrationTest;
import org.springframework.context.annotation.Bean;
import org.springframework.http.HttpStatus;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.github.shredder121.gh_event_api.GHEventApiServer;
import com.github.shredder121.gh_event_api.handler.create.CreateHandler;
import com.google.common.collect.ImmutableMap;

@RunWith(SpringJUnit4ClassRunner.class)
@WebIntegrationTest({"secret=secret", "spring.main.show-banner=false"})
@SpringApplicationConfiguration(classes = {HmacBehaviorTest.class, GHEventApiServer.class})
@DirtiesContext
public class HmacBehaviorTest {

    @Test
    public void testHmacIncorrect() {
        given().headers(
                "X-GitHub-Event", "create",
                "X-Hub-Signature", "bogus")
        .and().body(getBody()).with().contentType(JSON)
        .expect().statusCode(HttpStatus.FORBIDDEN.value())
        .when().post();
    }

    @Test
    public void testHmacMissing() {
        given().headers("X-GitHub-Event", "create")
        .and().body(getBody()).with().contentType(JSON)
        .expect().statusCode(HttpStatus.NOT_FOUND.value())
        .when().post();
    }

    @Test
    public void testHmacOkay() {
        given().headers(
                "X-GitHub-Event", "create",
                "X-Hub-Signature", "sha1=95932ea97b74d2dac6d67ae73bf1af6f43ce6db3")
        .and().body(getBody()).with().contentType(JSON)
        .expect().statusCode(HttpStatus.OK.value())
        .when().post();
    }

    private static Map<String, Object> getBody() {
        return ImmutableMap.of(
                "ref_type", "tag",
                "ref", "0.1",
                "master_branch", "master",
                "description", "");
    }

    @Bean
    public CreateHandler handlerBean() {
        return payload -> {
        };
    }
}

@@ -1190,24 +1190,84 @@
 assRunner;%0A%0A
+import com.fasterxml.jackson.databind.SerializationFeature;%0A
 import com.g
@@ -1434,16 +1434,211 @@
 ableMap;
+%0Aimport com.jayway.restassured.internal.mapping.Jackson2Mapper;%0Aimport com.jayway.restassured.mapper.ObjectMapper;%0Aimport com.jayway.restassured.mapper.factory.DefaultJackson2ObjectMapperFactory;
 %0A%0A@RunWi
@@ -1884,16 +1884,285 @@
 Test %7B%0A%0A
+    private static final ObjectMapper restAssuredMapper = new Jackson2Mapper(%0A            (clazz, charset) -%3E new DefaultJackson2ObjectMapperFactory()%0A                    .create(clazz, charset)%0A                    .disable(SerializationFeature.FAIL_ON_EMPTY_BEANS));%0A%0A
     @Tes
@@ -2335,32 +2335,51 @@
 ).body(getBody()
+, restAssuredMapper
 ).with().content
@@ -2598,32 +2598,51 @@
 ).body(getBody()
+, restAssuredMapper
 ).with().content
@@ -2899,48 +2899,48 @@
 ha1=
-95932ea97b74d2dac6d67ae73bf1af6f43ce6db3
+807810001d379cceefed1898c5cacee2b0462b6c
 %22)%0A 
@@ -2967,16 +2967,35 @@
 etBody()
+, restAssuredMapper
 ).with()
@@ -3179,11 +3179,33 @@
 Map.
-of(
+%3CString, Object%3Ebuilder()
 %0A   
@@ -3213,24 +3213,29 @@
             
+.put(
 %22ref_type%22, 
@@ -3239,17 +3239,17 @@
 %22, %22tag%22
-,
+)
 %0A       
@@ -3257,16 +3257,21 @@
         
+.put(
 %22ref%22, %22
@@ -3274,17 +3274,17 @@
 %22, %220.1%22
-,
+)
 %0A       
@@ -3292,16 +3292,21 @@
         
+.put(
 %22master_
@@ -3322,17 +3322,17 @@
 %22master%22
-,
+)
 %0A       
@@ -3340,16 +3340,21 @@
         
+.put(
 %22descrip
@@ -3363,16 +3363,179 @@
 on%22, %22%22)
+%0A                .put(%22pusher_type%22, %22user%22)%0A                .put(%22repository%22, new Object())%0A                .put(%22sender%22, new Object())%0A                .build()
 ;%0A    %7D%0A

Fix HmacBehaviorTest's body