nitter/nitters/gitea/config_test.go

108 lines
2.3 KiB
Go
Raw Permalink Normal View History

package gitea_test
import (
"errors"
"testing"
"code.icb4dc0.de/prskr/nitter/nitters"
"code.icb4dc0.de/prskr/nitter/nitters/gitea"
)
func TestGiteaPRConfig_Validate(t *testing.T) {
t.Parallel()
tests := []struct {
name string
cfg gitea.GiteaPRConfig
errorMatcher func(err error) error
}{
{
name: "Missing namespace",
cfg: gitea.GiteaPRConfig{},
errorMatcher: expectError(nitters.ErrMissingNamespace),
},
{
name: "Missing repo",
cfg: gitea.GiteaPRConfig{
GiteaConfig: gitea.GiteaConfig{
Config: nitters.Config{
Namespace: "some",
},
},
},
errorMatcher: expectError(nitters.ErrMissingRepo),
},
{
name: "Missing base address",
cfg: gitea.GiteaPRConfig{
GiteaConfig: gitea.GiteaConfig{
Config: nitters.Config{
Namespace: "some",
Repo: "repo",
},
},
},
errorMatcher: expectError(gitea.ErrMissingBaseAddress),
},
{
name: "Missing token",
cfg: gitea.GiteaPRConfig{
GiteaConfig: gitea.GiteaConfig{
Config: nitters.Config{
Namespace: "some",
Repo: "repo",
},
BaseAddress: "http://my-forgejo:3000",
},
},
errorMatcher: expectError(gitea.ErrMissingToken),
},
{
name: "Missing pull request index",
cfg: gitea.GiteaPRConfig{
GiteaConfig: gitea.GiteaConfig{
Config: nitters.Config{
Namespace: "some",
Repo: "repo",
},
BaseAddress: "http://my-forgejo:3000",
Token: "sup3rS3cur3",
},
},
errorMatcher: expectError(gitea.ErrMissingPRIndex),
},
{
name: "All set",
cfg: gitea.GiteaPRConfig{
GiteaConfig: gitea.GiteaConfig{
Config: nitters.Config{
Namespace: "some",
Repo: "repo",
},
BaseAddress: "http://my-forgejo:3000",
Token: "sup3rS3cur3",
},
PRIndex: 11,
},
errorMatcher: expectError(nil),
},
}
for _, tc := range tests {
tt := tc
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
if err := tt.errorMatcher(tt.cfg.Validate()); err != nil {
t.Errorf("Validate() error = %v", err)
}
})
}
}
func expectError(errorToIgnore error) func(err error) error {
return func(err error) error {
if errors.Is(err, errorToIgnore) {
return nil
}
return err
}
}