Compare commits

..

1 Commits

Author SHA1 Message Date
80af337f3a fix(tests): Updated tests for a pass
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
2024-04-09 10:56:23 +12:00

View File

@@ -17,12 +17,12 @@ import (
)
type testHelperMethodStruct struct {
config config.GitHubProvider
config config.GiteaProvider
valid bool
}
type testReleaseStruct struct {
config config.GitHubProvider
config config.GiteaProvider
releaseVersion *shared.ReleaseVersion
generatedChangelog *shared.GeneratedChangelog
requestResponseBody string
@@ -31,20 +31,13 @@ type testReleaseStruct struct {
}
var testNewClient = []testHelperMethodStruct{
{config: config.GitHubProvider{
{config: config.GiteaProvider{
Repo: "foo",
User: "bar",
URL: "https://hub.cybercinch.nz",
},
valid: true,
},
{config: config.GitHubProvider{
Repo: "foo",
User: "bar",
CustomURL: "https://test.com",
},
valid: false,
},
}
var lastVersion, _ = semver.NewVersion("1.0.0")
@@ -52,7 +45,7 @@ var newVersion, _ = semver.NewVersion("2.0.0")
var testReleases = []testReleaseStruct{
{
config: config.GitHubProvider{
config: config.GiteaProvider{
Repo: "foo",
User: "bar",
},
@@ -76,7 +69,7 @@ var testReleases = []testReleaseStruct{
valid: true,
},
{
config: config.GitHubProvider{
config: config.GiteaProvider{
Repo: "foo",
User: "bar",
},
@@ -100,100 +93,147 @@ var testReleases = []testReleaseStruct{
},
}
func initHTTPServer(respCode int, body string) *httptest.Server {
return httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
func initHTTPServerMux() *httptest.Server {
// Take the request entries and return http mux
mux := http.NewServeMux()
mux.HandleFunc("/api/v1/version", func(rw http.ResponseWriter, req *http.Request) {
log.Infof("Got call from %s %s", req.Method, req.URL.String())
rw.WriteHeader(200)
rw.Header().Set("Content-Type", "application/json")
body := `{
"version": "1.21.10"
}`
if _, err := rw.Write([]byte(body)); err != nil {
log.Info(err)
}
})
mux.HandleFunc("/api/v1/repos/bar/foo/releases", func(rw http.ResponseWriter, req *http.Request) {
log.Infof("Got call from %s %s", req.Method, req.URL.String())
rw.WriteHeader(respCode)
rw.WriteHeader(200)
body := `{
"url": "https://api.github.com/repos/octocat/Hello-World/releases/1",
"html_url": "https://github.com/octocat/Hello-World/releases/v1.0.0",
"assets_url": "https://api.github.com/repos/octocat/Hello-World/releases/1/assets",
"upload_url": "https://uploads.github.com/repos/octocat/Hello-World/releases/1/assets{?name,label}",
"tarball_url": "https://api.github.com/repos/octocat/Hello-World/tarball/v1.0.0",
"zipball_url": "https://api.github.com/repos/octocat/Hello-World/zipball/v1.0.0",
"id": 1,
"node_id": "MDc6UmVsZWFzZTE=",
"tag_name": "v1.0.0",
"target_commitish": "master",
"name": "v1.0.0",
"body": "Description of the release",
"draft": false,
"prerelease": false,
"created_at": "2013-02-27T19:35:32Z",
"published_at": "2013-02-27T19:35:32Z",
"author": {
"login": "octocat",
"id": 1,
"node_id": "MDQ6VXNlcjE=",
"avatar_url": "https://github.com/images/error/octocat_happy.gif",
"gravatar_id": "",
"url": "https://api.github.com/users/octocat",
"html_url": "https://github.com/octocat",
"followers_url": "https://api.github.com/users/octocat/followers",
"following_url": "https://api.github.com/users/octocat/following{/other_user}",
"gists_url": "https://api.github.com/users/octocat/gists{/gist_id}",
"starred_url": "https://api.github.com/users/octocat/starred{/owner}{/repo}",
"subscriptions_url": "https://api.github.com/users/octocat/subscriptions",
"organizations_url": "https://api.github.com/users/octocat/orgs",
"repos_url": "https://api.github.com/users/octocat/repos",
"events_url": "https://api.github.com/users/octocat/events{/privacy}",
"received_events_url": "https://api.github.com/users/octocat/received_events",
"type": "User",
"site_admin": false
},
"assets": []
}`
rw.Header().Set("Content-Type", "application/json")
if _, err := rw.Write([]byte(body)); err != nil {
log.Info(err)
}
}))
})
server := httptest.NewServer(mux)
return server
}
func TestNew(t *testing.T) {
func TestNewGitea(t *testing.T) {
for _, testOject := range testNewClient {
if testOject.valid {
os.Setenv("GITHUB_TOKEN", "XXX")
os.Setenv("GITEA_TOKEN", "XXX")
}
server := initHTTPServerMux()
defer server.Close()
testOject.config.URL = server.URL
_, err := New(&testOject.config, true)
assert.Equal(t, testOject.valid, err == nil)
os.Unsetenv("GITHUB_TOKEN")
os.Unsetenv("GITEA_TOKEN")
}
}
func TestGetCommitURL(t *testing.T) {
os.Setenv("GITHUB_TOKEN", "XX")
func TestGetCommitURLGitea(t *testing.T) {
os.Setenv("GITEA_TOKEN", "XX")
for _, testOject := range testNewClient {
server := initHTTPServerMux()
defer server.Close()
testOject.config.URL = server.URL
client, _ := New(&testOject.config, false)
actualURL := client.GetCommitURL()
if testOject.config.CustomURL != "" {
expectedURL := fmt.Sprintf("%s/api/v3/%s/%s/commit/{{hash}}", testOject.config.CustomURL, testOject.config.User, testOject.config.Repo)
assert.EqualValues(t, expectedURL, actualURL)
} else {
expectedURL := fmt.Sprintf("%s/%s/%s/commit/{{hash}}", "https://github.com", testOject.config.User, testOject.config.Repo)
assert.EqualValues(t, expectedURL, actualURL)
}
expectedURL := fmt.Sprintf("%s/%s/%s/commit/{{hash}}", testOject.config.URL, testOject.config.User, testOject.config.Repo)
assert.EqualValues(t, expectedURL, actualURL)
}
os.Unsetenv("GITHUB_TOKEN")
os.Unsetenv("GITEA_TOKEN")
}
func TestGetCompareURL(t *testing.T) {
os.Setenv("GITHUB_TOKEN", "XX")
func TestGetCompareURLGitea(t *testing.T) {
os.Setenv("GITEA_TOKEN", "XX")
for _, testOject := range testNewClient {
server := initHTTPServerMux()
defer server.Close()
testOject.config.URL = server.URL
client, _ := New(&testOject.config, false)
actualURL := client.GetCompareURL("1", "2")
if testOject.config.CustomURL != "" {
expectedURL := fmt.Sprintf("%s/api/v3/%s/%s/compare/%s...%s", testOject.config.CustomURL, testOject.config.User, testOject.config.Repo, "1", "2")
assert.EqualValues(t, expectedURL, actualURL)
} else {
expectedURL := fmt.Sprintf("%s/%s/%s/compare/%s...%s", "https://github.com", testOject.config.User, testOject.config.Repo, "1", "2")
assert.EqualValues(t, expectedURL, actualURL)
}
expectedURL := fmt.Sprintf("%s/%s/%s/compare/%s...%s", testOject.config.URL, testOject.config.User, testOject.config.Repo, "1", "2")
assert.EqualValues(t, expectedURL, actualURL)
}
os.Unsetenv("GITHUB_TOKEN")
os.Unsetenv("GITEA_TOKEN")
}
func TestCreateRelease(t *testing.T) {
os.Setenv("GITHUB_TOKEN", "XX")
func TestCreateReleaseGitea(t *testing.T) {
os.Setenv("GITEA_TOKEN", "XX")
for _, testObejct := range testReleases {
if testObejct.valid {
server := initHTTPServer(testObejct.requestResponseCode, testObejct.requestResponseBody)
testObejct.config.CustomURL = server.URL
client, _ := New(&testObejct.config, false)
err := client.makeRelease(testObejct.releaseVersion, testObejct.generatedChangelog)
for _, testObject := range testReleases {
if testObject.valid {
server := initHTTPServerMux()
defer server.Close()
testObject.config.URL = server.URL
client, _ := New(&testObject.config, false)
err := client.makeRelease(testObject.releaseVersion, testObject.generatedChangelog)
if err != nil {
t.Log(err)
}
assert.Equal(t, testObejct.valid, err == nil)
server.Close()
assert.Equal(t, testObject.valid, err == nil)
} else {
testObejct.config.CustomURL = "http://foo"
client, _ := New(&testObejct.config, false)
err := client.makeRelease(testObejct.releaseVersion, testObejct.generatedChangelog)
server := initHTTPServerMux()
defer server.Close()
testObject.config.URL = server.URL
client, _ := New(&testObject.config, false)
server.Close() // Simulate error response
err := client.makeRelease(testObject.releaseVersion, testObject.generatedChangelog)
if err != nil {
t.Log(err)
}
assert.Error(t, err)
}
}
os.Unsetenv("GITHUB_TOKEN")
os.Unsetenv("GITEA_TOKEN")
}