// Copyright 2019 Drone.IO Inc. All rights reserved. // Use of this source code is governed by the Drone Non-Commercial License // that can be found in the LICENSE file. // +build !oss package config import ( "testing" "time" "github.com/drone/drone/core" "github.com/h2non/gock" ) func TestGlobal(t *testing.T) { defer gock.Off() gock.New("https://company.com"). Post("/config"). MatchHeader("Accept", "application/vnd.drone.config.v1\\+json"). MatchHeader("Accept-Encoding", "identity"). MatchHeader("Content-Type", "application/json"). Reply(200). BodyString(`{"data": "{ kind: pipeline, name: default }"}`). Done() args := &core.ConfigArgs{ User: &core.User{Login: "octocat"}, Repo: &core.Repository{Slug: "octocat/hello-world", Config: ".drone.yml"}, Build: &core.Build{After: "6d144de7"}, } service := Global("https://company.com/config", "GMEuUHQfmrMRsseWxi9YlIeBtn9lm6im", false, time.Minute) result, err := service.Find(noContext, args) if err != nil { t.Error(err) return } if result.Data != "{ kind: pipeline, name: default }" { t.Errorf("unexpected file contents") } if gock.IsPending() { t.Errorf("Unfinished requests") return } } func TestGlobalErr(t *testing.T) { defer gock.Off() gock.New("https://company.com"). Post("/config"). MatchHeader("Accept", "application/vnd.drone.config.v1\\+json"). MatchHeader("Accept-Encoding", "identity"). MatchHeader("Content-Type", "application/json"). Reply(404). Done() args := &core.ConfigArgs{ User: &core.User{Login: "octocat"}, Repo: &core.Repository{Slug: "octocat/hello-world", Config: ".drone.yml"}, Build: &core.Build{After: "6d144de7"}, } service := Global("https://company.com/config", "GMEuUHQfmrMRsseWxi9YlIeBtn9lm6im", false, time.Minute) _, err := service.Find(noContext, args) if err == nil { t.Errorf("Expect http.Response error") } else if err.Error() != "Not Found" { t.Errorf("Expect Not Found error") } if gock.IsPending() { t.Errorf("Unfinished requests") } } func TestGlobalEmpty(t *testing.T) { defer gock.Off() gock.New("https://company.com"). Post("/config"). MatchHeader("Accept", "application/vnd.drone.config.v1\\+json"). MatchHeader("Accept-Encoding", "identity"). MatchHeader("Content-Type", "application/json"). Reply(204). Done() args := &core.ConfigArgs{ User: &core.User{Login: "octocat"}, Repo: &core.Repository{Slug: "octocat/hello-world", Config: ".drone.yml"}, Build: &core.Build{After: "6d144de7"}, } service := Global("https://company.com/config", "GMEuUHQfmrMRsseWxi9YlIeBtn9lm6im", false, time.Minute) result, err := service.Find(noContext, args) if err != nil { t.Error(err) return } if result != nil { t.Errorf("Expect empty data") } if gock.IsPending() { t.Errorf("Unfinished requests") return } } func TestGlobalDisabled(t *testing.T) { res, err := Global("", "", false, time.Minute).Find(noContext, nil) if err != nil { t.Error(err) } if res != nil { t.Errorf("expect nil config when disabled") } }