Add Forge Cacher tests

This commit is contained in:
Mustafa Gezen 2023-08-29 02:40:30 +02:00
parent 19a3a9fdd8
commit 392be966fd
Signed by: mustafa
GPG Key ID: DCDF010D946438C1
3 changed files with 99 additions and 1 deletions

View File

@ -1,4 +1,4 @@
load("@io_bazel_rules_go//go:def.bzl", "go_library")
load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test")
go_library(
name = "forge",
@ -10,3 +10,14 @@ go_library(
visibility = ["//visibility:public"],
deps = ["//vendor/github.com/go-git/go-git/v5/plumbing/transport"],
)
go_test(
name = "forge_test",
size = "small",
srcs = ["caching_test.go"],
embed = [":forge"],
deps = [
"//vendor/github.com/go-git/go-git/v5/plumbing/transport/http",
"//vendor/github.com/stretchr/testify/require",
],
)

View File

@ -0,0 +1,86 @@
package forge
import (
transport_http "github.com/go-git/go-git/v5/plumbing/transport/http"
"github.com/stretchr/testify/require"
"testing"
"time"
)
type testForge struct {
Forge
callToGetAuthenticator int
}
func (t *testForge) GetAuthenticator() (*Authenticator, error) {
t.callToGetAuthenticator++
return &Authenticator{
AuthMethod: &transport_http.BasicAuth{
Username: "test",
Password: "test",
},
AuthorName: "test",
AuthorEmail: "test@resf.org",
Expires: time.Now().Add(time.Minute * 45),
}, nil
}
func TestNewCacher(t *testing.T) {
f := &testForge{}
fAuth, err := f.GetAuthenticator()
require.Nil(t, err)
fAuthAuthMethod := fAuth.AuthMethod.(*transport_http.BasicAuth)
require.Equal(t, "test", fAuthAuthMethod.Username)
require.Equal(t, "test", fAuthAuthMethod.Password)
c := NewCacher(f)
cAuth, err := c.GetAuthenticator()
require.Nil(t, err)
cAuthAuthMethod := cAuth.AuthMethod.(*transport_http.BasicAuth)
require.Equal(t, "test", cAuthAuthMethod.Username)
require.Equal(t, "test", cAuthAuthMethod.Password)
}
func TestCacher_GetAuthenticator(t *testing.T) {
f := &testForge{}
c := NewCacher(f)
cAuth, err := c.GetAuthenticator()
require.Nil(t, err)
cAuthAuthMethod := cAuth.AuthMethod.(*transport_http.BasicAuth)
require.Equal(t, "test", cAuthAuthMethod.Username)
require.Equal(t, "test", cAuthAuthMethod.Password)
}
func TestCacher_GetAuthenticator_Cached(t *testing.T) {
f := &testForge{}
c := NewCacher(f)
cAuth, err := c.GetAuthenticator()
require.Nil(t, err)
require.Equal(t, "test", cAuth.AuthorName)
cAuth, err = c.GetAuthenticator()
require.Nil(t, err)
require.Equal(t, "test", cAuth.AuthorName)
require.Equal(t, 1, f.callToGetAuthenticator)
}
func TestCacher_GetAuthenticator_Expired(t *testing.T) {
f := &testForge{}
c := NewCacher(f)
cAuth, err := c.GetAuthenticator()
require.Nil(t, err)
require.Equal(t, "test", cAuth.AuthorName)
cAuth.Expires = time.Now().Add(-time.Minute * 10)
cAuth, err = c.GetAuthenticator()
require.Nil(t, err)
require.Equal(t, "test", cAuth.AuthorName)
require.Equal(t, 2, f.callToGetAuthenticator)
}

View File

@ -18,6 +18,7 @@ go_test(
srcs = ["github_test.go"],
embed = [":github"],
deps = [
"//vendor/github.com/go-git/go-git/v5/plumbing/transport/http",
"//vendor/github.com/jarcoal/httpmock",
"//vendor/github.com/stretchr/testify/require",
],