You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
drone-go/plugin/config/handler_test.go

106 lines
2.7 KiB
Go

// Copyright 2018 Drone.IO Inc.
//
// 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 config
import (
"bytes"
"context"
"encoding/json"
"net/http"
"net/http/httptest"
"testing"
"time"
"git.awesome-for.me/liuzhiguo/drone-go/drone"
"github.com/99designs/httpsignatures-go"
)
func TestHandler(t *testing.T) {
key := "xVKAGlWQiY3sOp8JVc0nbuNId3PNCgWh"
buf := new(bytes.Buffer)
json.NewEncoder(buf).Encode(&Request{})
res := httptest.NewRecorder()
req := httptest.NewRequest("GET", "/", buf)
req.Header.Add("Date", time.Now().UTC().Format(http.TimeFormat))
err := httpsignatures.DefaultSha256Signer.AuthRequest("hmac-key", key, req)
if err != nil {
t.Error(err)
return
}
want := &drone.Config{
Kind: "drone.v1.yaml",
Data: "pipeilne: []",
}
plugin := &mockPlugin{
res: want,
err: nil,
}
handler := Handler(plugin, key, nil)
handler.ServeHTTP(res, req)
if got, want := res.Code, 200; got != want {
t.Errorf("Want status code %d, got %d", want, got)
}
resp := &drone.Config{}
json.Unmarshal(res.Body.Bytes(), resp)
if got, want := resp.Data, want.Data; got != want {
t.Errorf("Want configuration data %s, got %s", want, got)
}
}
func TestHandler_MissingSignature(t *testing.T) {
res := httptest.NewRecorder()
req := httptest.NewRequest("GET", "/", nil)
handler := Handler(nil, "xVKAGlWQiY3sOp8JVc0nbuNId3PNCgWh", nil)
handler.ServeHTTP(res, req)
got, want := res.Body.String(), "Invalid or Missing Signature\n"
if got != want {
t.Errorf("Want response body %q, got %q", want, got)
}
}
func TestHandler_InvalidSignature(t *testing.T) {
sig := `keyId="hmac-key",algorithm="hmac-sha256",signature="QrS16+RlRsFjXn5IVW8tWz+3ZRAypjpNgzehEuvJksk=",headers="(request-target) accept accept-encoding content-type date digest"`
res := httptest.NewRecorder()
req := httptest.NewRequest("GET", "/", nil)
req.Header.Set("Signature", sig)
handler := Handler(nil, "xVKAGlWQiY3sOp8JVc0nbuNId3PNCgWh", nil)
handler.ServeHTTP(res, req)
got, want := res.Body.String(), "Invalid Signature\n"
if got != want {
t.Errorf("Want response body %q, got %q", want, got)
}
}
type mockPlugin struct {
res *drone.Config
err error
}
func (m *mockPlugin) Find(ctx context.Context, req *Request) (*drone.Config, error) {
return m.res, m.err
}