79 lines
2.6 KiB
Go
79 lines
2.6 KiB
Go
/*
|
|
Copyright 2020 The Flux authors
|
|
|
|
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 notifier
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"github.com/google/go-github/v53/github"
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
func TestNewGitHubBasic(t *testing.T) {
|
|
g, err := NewGitHub("0c9c2e41-d2f9-4f9b-9c41-bebc1984d67a", "https://github.com/foo/bar", "foobar", nil)
|
|
assert.Nil(t, err)
|
|
assert.Equal(t, g.Owner, "foo")
|
|
assert.Equal(t, g.Repo, "bar")
|
|
assert.Equal(t, g.Client.BaseURL.Host, "api.github.com")
|
|
}
|
|
|
|
func TestNewEmterpriseGitHubBasic(t *testing.T) {
|
|
g, err := NewGitHub("0c9c2e41-d2f9-4f9b-9c41-bebc1984d67a", "https://foobar.com/foo/bar", "foobar", nil)
|
|
assert.Nil(t, err)
|
|
assert.Equal(t, g.Owner, "foo")
|
|
assert.Equal(t, g.Repo, "bar")
|
|
assert.Equal(t, g.Client.BaseURL.Host, "foobar.com")
|
|
}
|
|
|
|
func TestNewGitHubInvalidUrl(t *testing.T) {
|
|
_, err := NewGitHub("0c9c2e41-d2f9-4f9b-9c41-bebc1984d67a", "https://github.com/foo/bar/baz", "foobar", nil)
|
|
assert.NotNil(t, err)
|
|
}
|
|
|
|
func TestNewGitHubEmptyToken(t *testing.T) {
|
|
_, err := NewGitHub("0c9c2e41-d2f9-4f9b-9c41-bebc1984d67a", "https://github.com/foo/bar", "", nil)
|
|
assert.NotNil(t, err)
|
|
}
|
|
|
|
func TestDuplicateGithubStatus(t *testing.T) {
|
|
assert := assert.New(t)
|
|
|
|
var tests = []struct {
|
|
ss []*github.RepoStatus
|
|
s *github.RepoStatus
|
|
dup bool
|
|
}{
|
|
{[]*github.RepoStatus{ghStatus("success", "foo", "bar")}, ghStatus("success", "foo", "bar"), true},
|
|
{[]*github.RepoStatus{ghStatus("success", "foo", "bar")}, ghStatus("failure", "foo", "bar"), false},
|
|
{[]*github.RepoStatus{ghStatus("success", "foo", "bar")}, ghStatus("success", "baz", "bar"), false},
|
|
{[]*github.RepoStatus{ghStatus("success", "foo", "bar")}, ghStatus("success", "foo", "baz"), false},
|
|
{[]*github.RepoStatus{ghStatus("success", "baz", "bar"), ghStatus("success", "foo", "bar")}, ghStatus("success", "foo", "bar"), true},
|
|
}
|
|
|
|
for _, test := range tests {
|
|
assert.Equal(test.dup, duplicateGithubStatus(test.ss, test.s))
|
|
}
|
|
}
|
|
|
|
func ghStatus(state string, context string, description string) *github.RepoStatus {
|
|
return &github.RepoStatus{
|
|
State: &state,
|
|
Context: &context,
|
|
Description: &description,
|
|
}
|
|
}
|