harness-drone/service/content/content_test.go
2019-02-22 09:01:11 -08:00

96 lines
2.7 KiB
Go

// 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.
package contents
import (
"context"
"testing"
"github.com/drone/drone/core"
"github.com/drone/drone/mock"
"github.com/drone/drone/mock/mockscm"
"github.com/drone/go-scm/scm"
"github.com/google/go-cmp/cmp"
"github.com/golang/mock/gomock"
)
var noContext = context.Background()
func TestFind(t *testing.T) {
controller := gomock.NewController(t)
defer controller.Finish()
mockUser := &core.User{}
mockFile := &scm.Content{
Path: ".drone.yml",
Data: []byte("hello world"),
}
mockContents := mockscm.NewMockContentService(controller)
mockContents.EXPECT().Find(gomock.Any(), "octocat/hello-world", ".drone.yml", "a6586b3db244fb6b1198f2b25c213ded5b44f9fa").Return(mockFile, nil, nil)
mockRenewer := mock.NewMockRenewer(controller)
mockRenewer.EXPECT().Renew(gomock.Any(), mockUser, false)
client := new(scm.Client)
client.Contents = mockContents
want := &core.File{
Data: []byte("hello world"),
Hash: []byte(""),
}
service := New(client, mockRenewer)
got, err := service.Find(noContext, mockUser, "octocat/hello-world", "a6586b3db244fb6b1198f2b25c213ded5b44f9fa", "master", ".drone.yml")
if err != nil {
t.Error(err)
}
if diff := cmp.Diff(got, want); diff != "" {
t.Errorf(diff)
}
}
func TestFind_Error(t *testing.T) {
controller := gomock.NewController(t)
defer controller.Finish()
mockUser := &core.User{}
mockContents := mockscm.NewMockContentService(controller)
mockContents.EXPECT().Find(gomock.Any(), "octocat/hello-world", ".drone.yml", "a6586b3db244fb6b1198f2b25c213ded5b44f9fa").Return(nil, nil, scm.ErrNotFound)
mockRenewer := mock.NewMockRenewer(controller)
mockRenewer.EXPECT().Renew(gomock.Any(), mockUser, false)
client := new(scm.Client)
client.Contents = mockContents
s := New(client, mockRenewer)
s.(*service).attempts = 1
s.(*service).wait = 0
_, err := s.Find(noContext, mockUser, "octocat/hello-world", "a6586b3db244fb6b1198f2b25c213ded5b44f9fa", "master", ".drone.yml")
if err != scm.ErrNotFound {
t.Errorf("Expect not found error, got %s", err)
}
}
func TestFind_RenewalError(t *testing.T) {
controller := gomock.NewController(t)
defer controller.Finish()
mockUser := &core.User{}
mockRenewer := mock.NewMockRenewer(controller)
mockRenewer.EXPECT().Renew(gomock.Any(), mockUser, false).Return(scm.ErrNotAuthorized)
client := new(scm.Client)
service := New(client, mockRenewer)
_, err := service.Find(noContext, mockUser, "octocat/hello-world", "a6586b3db244fb6b1198f2b25c213ded5b44f9fa", "master", ".drone.yml")
if err == nil {
t.Errorf("Expect error refreshing token")
}
}