github.com/argoproj/argo-events@v1.9.1/eventsources/sources/github/start_test.go (about)

     1  /*
     2  Copyright 2018 BlackRock, Inc.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8  	http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package github
    18  
    19  import (
    20  	"bytes"
    21  	"encoding/json"
    22  	"io"
    23  	"net/http"
    24  	"testing"
    25  
    26  	"github.com/ghodss/yaml"
    27  	"github.com/smartystreets/goconvey/convey"
    28  	corev1 "k8s.io/api/core/v1"
    29  
    30  	"github.com/argoproj/argo-events/eventsources/common/webhook"
    31  	"github.com/argoproj/argo-events/pkg/apis/eventsource/v1alpha1"
    32  )
    33  
    34  var (
    35  	router = &Router{
    36  		route:             webhook.GetFakeRoute(),
    37  		githubEventSource: &v1alpha1.GithubEventSource{},
    38  	}
    39  )
    40  
    41  func TestRouteActiveHandler(t *testing.T) {
    42  	convey.Convey("Given a route configuration", t, func() {
    43  		route := router.route
    44  		route.DataCh = make(chan []byte)
    45  
    46  		convey.Convey("Inactive route should return error", func() {
    47  			writer := &webhook.FakeHttpWriter{}
    48  			githubEventSource := &v1alpha1.GithubEventSource{
    49  				Webhook: &v1alpha1.WebhookContext{
    50  					Endpoint: "/push",
    51  					URL:      "http://webhook-gateway-svc",
    52  					Port:     "12000",
    53  				},
    54  				Repositories: []v1alpha1.OwnedRepositories{
    55  					{
    56  						Owner: "fake",
    57  						Names: []string{
    58  							"fake0", "fake1",
    59  						},
    60  					},
    61  				},
    62  				Events: []string{
    63  					"PushEvent",
    64  				},
    65  				APIToken: &corev1.SecretKeySelector{
    66  					Key: "accessKey",
    67  					LocalObjectReference: corev1.LocalObjectReference{
    68  						Name: "github-access",
    69  					},
    70  				},
    71  			}
    72  
    73  			body, err := yaml.Marshal(githubEventSource)
    74  			convey.So(err, convey.ShouldBeNil)
    75  
    76  			router.HandleRoute(writer, &http.Request{
    77  				Body: io.NopCloser(bytes.NewReader(body)),
    78  			})
    79  			convey.So(writer.HeaderStatus, convey.ShouldEqual, http.StatusBadRequest)
    80  
    81  			convey.Convey("Active route should return success", func() {
    82  				route.Active = true
    83  
    84  				router.HandleRoute(writer, &http.Request{
    85  					Body: io.NopCloser(bytes.NewReader(body)),
    86  				})
    87  
    88  				convey.So(writer.HeaderStatus, convey.ShouldEqual, http.StatusBadRequest)
    89  			})
    90  		})
    91  	})
    92  }
    93  
    94  func TestRouteActiveHandlerDeprecated(t *testing.T) {
    95  	convey.Convey("Given a route configuration", t, func() {
    96  		route := router.route
    97  		route.DataCh = make(chan []byte)
    98  
    99  		convey.Convey("Inactive route should return error", func() {
   100  			writer := &webhook.FakeHttpWriter{}
   101  			githubEventSource := &v1alpha1.GithubEventSource{
   102  				Webhook: &v1alpha1.WebhookContext{
   103  					Endpoint: "/push",
   104  					URL:      "http://webhook-gateway-svc",
   105  					Port:     "12000",
   106  				},
   107  				DeprecatedOwner:      "fake",
   108  				DeprecatedRepository: "fake",
   109  				Events: []string{
   110  					"PushEvent",
   111  				},
   112  				APIToken: &corev1.SecretKeySelector{
   113  					Key: "accessKey",
   114  					LocalObjectReference: corev1.LocalObjectReference{
   115  						Name: "github-access",
   116  					},
   117  				},
   118  			}
   119  
   120  			body, err := yaml.Marshal(githubEventSource)
   121  			convey.So(err, convey.ShouldBeNil)
   122  
   123  			router.HandleRoute(writer, &http.Request{
   124  				Body: io.NopCloser(bytes.NewReader(body)),
   125  			})
   126  			convey.So(writer.HeaderStatus, convey.ShouldEqual, http.StatusBadRequest)
   127  
   128  			convey.Convey("Active route should return success", func() {
   129  				route.Active = true
   130  
   131  				router.HandleRoute(writer, &http.Request{
   132  					Body: io.NopCloser(bytes.NewReader(body)),
   133  				})
   134  
   135  				convey.So(writer.HeaderStatus, convey.ShouldEqual, http.StatusBadRequest)
   136  			})
   137  		})
   138  	})
   139  }
   140  
   141  func TestAddEventTypeBody(t *testing.T) {
   142  	convey.Convey("Given a request", t, func() {
   143  		var (
   144  			buf        = bytes.NewBuffer([]byte(`{ "hello": "world" }`))
   145  			eventType  = "PushEvent"
   146  			deliveryID = "131C7C9B-A571-4F60-9ACA-EA3ADA19FABE"
   147  		)
   148  		request, err := http.NewRequest("POST", "http://example.com", buf)
   149  		convey.So(err, convey.ShouldBeNil)
   150  		request.Header.Set("X-GitHub-Event", eventType)
   151  		request.Header.Set("X-GitHub-Delivery", deliveryID)
   152  		request.Header.Set("Content-Type", "application/json")
   153  
   154  		convey.Convey("Delivery headers should be written to message", func() {
   155  			body, err := parseValidateRequest(request, []byte{})
   156  			convey.So(err, convey.ShouldBeNil)
   157  			payload := make(map[string]interface{})
   158  			err = json.Unmarshal(body, &payload)
   159  			convey.So(err, convey.ShouldBeNil)
   160  			convey.So(err, convey.ShouldBeNil)
   161  			convey.So(payload["X-GitHub-Event"], convey.ShouldEqual, eventType)
   162  			convey.So(payload["X-GitHub-Delivery"], convey.ShouldEqual, deliveryID)
   163  		})
   164  	})
   165  }