diff --git a/pkg/scheduler/scheduler_test.go b/pkg/scheduler/scheduler_test.go index 60457fce..ac9d6b61 100644 --- a/pkg/scheduler/scheduler_test.go +++ b/pkg/scheduler/scheduler_test.go @@ -18,6 +18,7 @@ import ( ) var ( + testEmail = "nobody@localhost" repositoryName = "vibioh/ketchup" repositoryVersion = "1.0.0" repositoryBetaVersion = "1.0.0-beta1" @@ -292,12 +293,12 @@ func TestGetKetchupToNotify(t *testing.T) { { Pattern: model.DefaultPattern, Repository: model.NewRepository(1, model.Github, repositoryName).AddVersion(model.DefaultPattern, repositoryVersion).AddVersion("latest", repositoryBetaVersion), - User: model.NewUser(1, "nobody@localhost", authModel.NewUser(0, "")), + User: model.NewUser(1, testEmail, authModel.NewUser(0, "")), }, { Pattern: "latest", Repository: model.NewRepository(1, model.Github, repositoryName).AddVersion(model.DefaultPattern, repositoryVersion).AddVersion("latest", repositoryBetaVersion), - User: model.NewUser(1, "nobody@localhost", authModel.NewUser(0, "")), + User: model.NewUser(1, testEmail, authModel.NewUser(0, "")), }, { Pattern: model.DefaultPattern, @@ -332,7 +333,7 @@ func TestGetKetchupToNotify(t *testing.T) { }, Repository: model.NewRepository(1, model.Github, repositoryName).AddVersion(model.DefaultPattern, repositoryVersion).AddVersion("latest", repositoryBetaVersion), }}, - {ID: 1, Email: "nobody@localhost"}: {{ + {ID: 1, Email: testEmail}: {{ Pattern: "latest", Version: semver.Version{ Name: "1.1.0-beta", @@ -402,7 +403,7 @@ func TestSendNotification(t *testing.T) { ketchupToNotify: map[model.User][]model.Release{ { ID: 1, - Email: "nobody@localhost", + Email: testEmail, }: { { Repository: model.NewRepository(1, model.Github, repositoryName), @@ -425,7 +426,7 @@ func TestSendNotification(t *testing.T) { ketchupToNotify: map[model.User][]model.Release{ { ID: 1, - Email: "nobody@localhost", + Email: testEmail, }: { { Repository: model.NewRepository(1, model.Github, repositoryName), @@ -448,7 +449,7 @@ func TestSendNotification(t *testing.T) { ketchupToNotify: map[model.User][]model.Release{ { ID: 1, - Email: "nobody@localhost", + Email: testEmail, }: { { Repository: model.NewRepository(1, model.Github, repositoryName), @@ -471,7 +472,7 @@ func TestSendNotification(t *testing.T) { ketchupToNotify: map[model.User][]model.Release{ { ID: 1, - Email: "nobody@localhost", + Email: testEmail, }: { { Repository: model.NewRepository(1, model.Github, repositoryName), diff --git a/pkg/semver/pattern_test.go b/pkg/semver/pattern_test.go index be2ec9e0..959a33ef 100644 --- a/pkg/semver/pattern_test.go +++ b/pkg/semver/pattern_test.go @@ -5,6 +5,11 @@ import ( "testing" ) +var ( + stableVersion = "1.0.0" + betaVersion = "1.0.0-beta1" +) + func TestCheck(t *testing.T) { type args struct { version Version @@ -20,7 +25,7 @@ func TestCheck(t *testing.T) { "too short", safeParsePattern("tst"), args{ - version: safeParse("1.0.0"), + version: safeParse(stableVersion), }, true, }, @@ -28,7 +33,7 @@ func TestCheck(t *testing.T) { "no veersion", safeParsePattern("^latest"), args{ - version: safeParse("1.0.0"), + version: safeParse(stableVersion), }, true, }, @@ -36,7 +41,7 @@ func TestCheck(t *testing.T) { "latest", safeParsePattern("latest"), args{ - version: safeParse("1.0.0"), + version: safeParse(stableVersion), }, true, }, @@ -44,7 +49,7 @@ func TestCheck(t *testing.T) { "latest beta", safeParsePattern("latest"), args{ - version: safeParse("1.0.0-beta1"), + version: safeParse(betaVersion), }, true, }, @@ -52,7 +57,7 @@ func TestCheck(t *testing.T) { "stable", safeParsePattern("stable"), args{ - version: safeParse("1.0.0"), + version: safeParse(stableVersion), }, true, }, @@ -60,7 +65,7 @@ func TestCheck(t *testing.T) { "stable beta", safeParsePattern("stable"), args{ - version: safeParse("1.0.0-beta1"), + version: safeParse(betaVersion), }, false, }, @@ -68,7 +73,7 @@ func TestCheck(t *testing.T) { "caret", safeParsePattern("^1.0"), args{ - version: safeParse("1.0.0"), + version: safeParse(stableVersion), }, true, }, @@ -100,7 +105,7 @@ func TestCheck(t *testing.T) { "caret no beta", safeParsePattern("^1.0"), args{ - version: safeParse("1.0.0-beta1"), + version: safeParse(betaVersion), }, false, }, @@ -140,7 +145,7 @@ func TestCheck(t *testing.T) { "tilde no beta", safeParsePattern("~1.0"), args{ - version: safeParse("1.0.0-beta1"), + version: safeParse(betaVersion), }, false, }, diff --git a/pkg/service/ketchup/ketchup_test.go b/pkg/service/ketchup/ketchup_test.go index 98eb8abd..d473c87a 100644 --- a/pkg/service/ketchup/ketchup_test.go +++ b/pkg/service/ketchup/ketchup_test.go @@ -15,6 +15,9 @@ import ( ) var ( + ketchupRepository = "vibioh/ketchup" + viwsRepository = "vibioh/viws" + errAtomicStart = errors.New("invalid context") errAtomicEnd = errors.New("invalid context") ) @@ -36,15 +39,15 @@ func TestList(t *testing.T) { { "simple", New(ketchuptest.New().SetList([]model.Ketchup{ - model.NewKetchup(model.DefaultPattern, "1.2.3", model.NewRepository(2, model.Github, "vibioh/viws").AddVersion(model.DefaultPattern, "1.2.3")), - model.NewKetchup(model.DefaultPattern, "1.0.0", model.NewRepository(1, model.Github, "vibioh/ketchup").AddVersion(model.DefaultPattern, "1.0.2")), + model.NewKetchup(model.DefaultPattern, "1.2.3", model.NewRepository(2, model.Github, viwsRepository).AddVersion(model.DefaultPattern, "1.2.3")), + model.NewKetchup(model.DefaultPattern, "1.0.0", model.NewRepository(1, model.Github, ketchupRepository).AddVersion(model.DefaultPattern, "1.0.2")), }, 2, nil), nil), args{ page: 1, }, []model.Ketchup{ - {Pattern: model.DefaultPattern, Version: "1.0.0", Semver: "Patch", Repository: model.NewRepository(1, model.Github, "vibioh/ketchup").AddVersion(model.DefaultPattern, "1.0.2")}, - {Pattern: model.DefaultPattern, Version: "1.2.3", Repository: model.NewRepository(2, model.Github, "vibioh/viws").AddVersion(model.DefaultPattern, "1.2.3")}, + {Pattern: model.DefaultPattern, Version: "1.0.0", Semver: "Patch", Repository: model.NewRepository(1, model.Github, ketchupRepository).AddVersion(model.DefaultPattern, "1.0.2")}, + {Pattern: model.DefaultPattern, Version: "1.2.3", Repository: model.NewRepository(2, model.Github, viwsRepository).AddVersion(model.DefaultPattern, "1.2.3")}, }, 2, nil, @@ -97,8 +100,8 @@ func TestListForRepositories(t *testing.T) { { "simple", New(ketchuptest.New().SetListByRepositoriesID([]model.Ketchup{ - model.NewKetchup(model.DefaultPattern, "1.0.0", model.NewRepository(1, model.Github, "vibioh/ketchup").AddVersion(model.DefaultPattern, "1.0.2")), - model.NewKetchup(model.DefaultPattern, "1.2.3", model.NewRepository(2, model.Github, "vibioh/viws").AddVersion(model.DefaultPattern, "1.2.3")), + model.NewKetchup(model.DefaultPattern, "1.0.0", model.NewRepository(1, model.Github, ketchupRepository).AddVersion(model.DefaultPattern, "1.0.2")), + model.NewKetchup(model.DefaultPattern, "1.2.3", model.NewRepository(2, model.Github, viwsRepository).AddVersion(model.DefaultPattern, "1.2.3")), }, nil), nil), args{ repositories: []model.Repository{ @@ -107,8 +110,8 @@ func TestListForRepositories(t *testing.T) { }, }, []model.Ketchup{ - {Pattern: model.DefaultPattern, Version: "1.0.0", Semver: "Patch", Repository: model.NewRepository(1, model.Github, "vibioh/ketchup").AddVersion(model.DefaultPattern, "1.0.2")}, - {Pattern: model.DefaultPattern, Version: "1.2.3", Repository: model.NewRepository(2, model.Github, "vibioh/viws").AddVersion(model.DefaultPattern, "1.2.3")}, + {Pattern: model.DefaultPattern, Version: "1.0.0", Semver: "Patch", Repository: model.NewRepository(1, model.Github, ketchupRepository).AddVersion(model.DefaultPattern, "1.0.2")}, + {Pattern: model.DefaultPattern, Version: "1.2.3", Repository: model.NewRepository(2, model.Github, viwsRepository).AddVersion(model.DefaultPattern, "1.2.3")}, }, nil, }, @@ -178,7 +181,7 @@ func TestCreate(t *testing.T) { New(ketchuptest.New(), repositorytest.New()), args{ ctx: context.Background(), - item: model.NewKetchup(model.DefaultPattern, "", model.NewRepository(1, model.Github, "vibioh/ketchup")), + item: model.NewKetchup(model.DefaultPattern, "", model.NewRepository(1, model.Github, ketchupRepository)), }, model.NoneKetchup, httpModel.ErrInvalid, @@ -188,19 +191,19 @@ func TestCreate(t *testing.T) { New(ketchuptest.New().SetCreate(0, errors.New("failed")), repositorytest.New()), args{ ctx: model.StoreUser(context.Background(), model.NewUser(1, "", authModel.NewUser(0, ""))), - item: model.NewKetchup(model.DefaultPattern, "0.0.0", model.NewRepository(1, model.Github, "vibioh/ketchup")), + item: model.NewKetchup(model.DefaultPattern, "0.0.0", model.NewRepository(1, model.Github, ketchupRepository)), }, model.NoneKetchup, httpModel.ErrInternalError, }, { "success", - New(ketchuptest.New(), repositorytest.New().SetGetOrCreate(model.NewRepository(1, model.Github, "vibioh/ketchup").AddVersion(model.DefaultPattern, "1.0.0"), nil)), + New(ketchuptest.New(), repositorytest.New().SetGetOrCreate(model.NewRepository(1, model.Github, ketchupRepository).AddVersion(model.DefaultPattern, "1.0.0"), nil)), args{ ctx: model.StoreUser(context.Background(), model.NewUser(1, "", authModel.NewUser(0, ""))), - item: model.NewKetchup(model.DefaultPattern, "1.0.0", model.NewRepository(1, model.Github, "vibioh/ketchup")), + item: model.NewKetchup(model.DefaultPattern, "1.0.0", model.NewRepository(1, model.Github, ketchupRepository)), }, - model.NewKetchup(model.DefaultPattern, "1.0.0", model.NewRepository(1, model.Github, "vibioh/ketchup").AddVersion(model.DefaultPattern, "1.0.0")), + model.NewKetchup(model.DefaultPattern, "1.0.0", model.NewRepository(1, model.Github, ketchupRepository).AddVersion(model.DefaultPattern, "1.0.0")), nil, }, } @@ -259,20 +262,20 @@ func TestUpdate(t *testing.T) { }, { "check error", - New(ketchuptest.New().SetGetByRepositoryID(model.NewKetchup(model.DefaultPattern, "0.9.0", model.NewRepository(1, model.Github, "vibioh/ketchup")), nil), repositorytest.New()), + New(ketchuptest.New().SetGetByRepositoryID(model.NewKetchup(model.DefaultPattern, "0.9.0", model.NewRepository(1, model.Github, ketchupRepository)), nil), repositorytest.New()), args{ ctx: context.Background(), - item: model.NewKetchup(model.DefaultPattern, "", model.NewRepository(1, 0, "vibioh/ketchup")), + item: model.NewKetchup(model.DefaultPattern, "", model.NewRepository(1, 0, ketchupRepository)), }, model.NoneKetchup, httpModel.ErrInvalid, }, { "pattern change error", - New(ketchuptest.New().SetGetByRepositoryID(model.NewKetchup(model.DefaultPattern, "0.9.0", model.NewRepository(1, model.Github, "vibioh/ketchup")), nil), repositorytest.New().SetGetOrCreate(model.NoneRepository, errors.New("failed"))), + New(ketchuptest.New().SetGetByRepositoryID(model.NewKetchup(model.DefaultPattern, "0.9.0", model.NewRepository(1, model.Github, ketchupRepository)), nil), repositorytest.New().SetGetOrCreate(model.NoneRepository, errors.New("failed"))), args{ ctx: model.StoreUser(context.Background(), model.NewUser(1, "", authModel.NewUser(0, ""))), - item: model.NewKetchup("latest", "1.0.0", model.NewRepository(1, 0, "vibioh/ketchup")), + item: model.NewKetchup("latest", "1.0.0", model.NewRepository(1, 0, ketchupRepository)), }, model.NoneKetchup, httpModel.ErrInternalError, @@ -282,24 +285,24 @@ func TestUpdate(t *testing.T) { New(ketchuptest.New().SetGetByRepositoryID(model.Ketchup{ Pattern: model.DefaultPattern, Version: "0.9.0", - Repository: model.NewRepository(1, model.Github, "vibioh/ketchup"), + Repository: model.NewRepository(1, model.Github, ketchupRepository), User: model.NewUser(1, "", authModel.NewUser(0, "")), - }, nil), repositorytest.New().SetGetOrCreate(model.NewRepository(1, 0, "vibioh/ketchup").AddVersion("latest", "1.0.1"), nil)), + }, nil), repositorytest.New().SetGetOrCreate(model.NewRepository(1, 0, ketchupRepository).AddVersion("latest", "1.0.1"), nil)), args{ ctx: model.StoreUser(context.Background(), model.NewUser(1, "", authModel.NewUser(0, ""))), - item: model.NewKetchup("latest", "1.0.0", model.NewRepository(1, 0, "vibioh/ketchup")), + item: model.NewKetchup("latest", "1.0.0", model.NewRepository(1, 0, ketchupRepository)), }, model.Ketchup{ Pattern: "latest", Version: "1.0.0", - Repository: model.NewRepository(1, model.Github, "vibioh/ketchup").AddVersion("latest", "1.0.1"), + Repository: model.NewRepository(1, model.Github, ketchupRepository).AddVersion("latest", "1.0.1"), User: model.NewUser(1, "", authModel.NewUser(0, "")), }, nil, }, { "update error", - New(ketchuptest.New().SetGetByRepositoryID(model.NewKetchup(model.DefaultPattern, "0.9.0", model.NewRepository(1, model.Github, "vibioh/ketchup")), nil).SetUpdate(errors.New("failed")), repositorytest.New()), + New(ketchuptest.New().SetGetByRepositoryID(model.NewKetchup(model.DefaultPattern, "0.9.0", model.NewRepository(1, model.Github, ketchupRepository)), nil).SetUpdate(errors.New("failed")), repositorytest.New()), args{ ctx: model.StoreUser(context.Background(), model.NewUser(1, "", authModel.NewUser(0, ""))), item: model.NewKetchup(model.DefaultPattern, "0.0.0", model.NewRepository(2, 0, "")), @@ -312,7 +315,7 @@ func TestUpdate(t *testing.T) { New(ketchuptest.New().SetGetByRepositoryID(model.Ketchup{ Pattern: model.DefaultPattern, Version: "0.9.0", - Repository: model.NewRepository(1, model.Github, "vibioh/ketchup").AddVersion(model.DefaultPattern, "1.2.3"), + Repository: model.NewRepository(1, model.Github, ketchupRepository).AddVersion(model.DefaultPattern, "1.2.3"), User: model.NewUser(1, "", authModel.NewUser(0, "")), }, nil), repositorytest.New()), args{ @@ -322,7 +325,7 @@ func TestUpdate(t *testing.T) { model.Ketchup{ Pattern: model.DefaultPattern, Version: "1.0.0", - Repository: model.NewRepository(1, model.Github, "vibioh/ketchup").AddVersion(model.DefaultPattern, "1.2.3"), + Repository: model.NewRepository(1, model.Github, ketchupRepository).AddVersion(model.DefaultPattern, "1.2.3"), User: model.NewUser(1, "", authModel.NewUser(0, "")), }, nil, @@ -496,10 +499,10 @@ func TestCheck(t *testing.T) { }, { "create already exists", - app{ketchupStore: ketchuptest.New().SetGetByRepositoryID(model.NewKetchup(model.DefaultPattern, "1.0.0", model.NewRepository(1, model.Github, "vibioh/ketchup")), nil)}, + app{ketchupStore: ketchuptest.New().SetGetByRepositoryID(model.NewKetchup(model.DefaultPattern, "1.0.0", model.NewRepository(1, model.Github, ketchupRepository)), nil)}, args{ ctx: model.StoreUser(context.Background(), model.NewUser(1, "", authModel.NewUser(0, ""))), - new: model.Ketchup{Pattern: model.DefaultPattern, Version: "1.0.0", Repository: model.NewRepository(2, model.Github, "vibioh/ketchup"), User: model.NewUser(1, "", authModel.NewUser(0, ""))}, + new: model.Ketchup{Pattern: model.DefaultPattern, Version: "1.0.0", Repository: model.NewRepository(2, model.Github, ketchupRepository), User: model.NewUser(1, "", authModel.NewUser(0, ""))}, }, errors.New("ketchup for vibioh/ketchup already exists"), }, diff --git a/pkg/service/repository/repository_test.go b/pkg/service/repository/repository_test.go index 2f8549f4..e0dafe1b 100644 --- a/pkg/service/repository/repository_test.go +++ b/pkg/service/repository/repository_test.go @@ -17,6 +17,9 @@ import ( ) var ( + ketchupRepository = "vibioh/ketchup" + viwsRepository = "vibioh/viws" + errAtomicStart = errors.New("invalid context") errAtomicEnd = errors.New("invalid context") ) @@ -46,15 +49,15 @@ func TestList(t *testing.T) { { "simple", New(repositorytest.New().SetList([]model.Repository{ - model.NewRepository(1, model.Github, "vibioh/ketchup").AddVersion(model.DefaultPattern, "1.0.0"), - model.NewRepository(2, model.Github, "vibioh/viws").AddVersion(model.DefaultPattern, "1.2.3"), + model.NewRepository(1, model.Github, ketchupRepository).AddVersion(model.DefaultPattern, "1.0.0"), + model.NewRepository(2, model.Github, viwsRepository).AddVersion(model.DefaultPattern, "1.2.3"), }, 2, nil), nil, nil), args{ page: 1, }, []model.Repository{ - model.NewRepository(1, model.Github, "vibioh/ketchup").AddVersion(model.DefaultPattern, "1.0.0"), - model.NewRepository(2, model.Github, "vibioh/viws").AddVersion(model.DefaultPattern, "1.2.3"), + model.NewRepository(1, model.Github, ketchupRepository).AddVersion(model.DefaultPattern, "1.0.0"), + model.NewRepository(2, model.Github, viwsRepository).AddVersion(model.DefaultPattern, "1.2.3"), }, 2, nil, @@ -109,13 +112,13 @@ func TestSuggest(t *testing.T) { { "simple", New(repositorytest.New().SetSuggest([]model.Repository{ - model.NewRepository(1, model.Github, "vibioh/ketchup").AddVersion(model.DefaultPattern, "1.2.3"), + model.NewRepository(1, model.Github, ketchupRepository).AddVersion(model.DefaultPattern, "1.2.3"), }, nil), nil, nil), args{ ctx: context.Background(), }, []model.Repository{ - model.NewRepository(1, model.Github, "vibioh/ketchup").AddVersion(model.DefaultPattern, "1.2.3"), + model.NewRepository(1, model.Github, ketchupRepository).AddVersion(model.DefaultPattern, "1.2.3"), }, nil, }, @@ -178,19 +181,19 @@ func TestGetOrCreate(t *testing.T) { }, { "exists with pattern", - New(repositorytest.New().SetGetByName(model.NewRepository(1, model.Github, "vibioh/ketchup").AddVersion(model.DefaultPattern, "1.0.0"), nil), githubtest.New(), nil), + New(repositorytest.New().SetGetByName(model.NewRepository(1, model.Github, ketchupRepository).AddVersion(model.DefaultPattern, "1.0.0"), nil), githubtest.New(), nil), args{ ctx: context.Background(), name: "exist", repositoryKind: model.Github, pattern: model.DefaultPattern, }, - model.NewRepository(1, model.Github, "vibioh/ketchup").AddVersion(model.DefaultPattern, "1.0.0"), + model.NewRepository(1, model.Github, ketchupRepository).AddVersion(model.DefaultPattern, "1.0.0"), nil, }, { "exists no pattern error", - New(repositorytest.New().SetGetByName(model.NewRepository(1, model.Github, "vibioh/ketchup"), nil), githubtest.New().SetLatestVersions(nil, errors.New("failed")), nil), + New(repositorytest.New().SetGetByName(model.NewRepository(1, model.Github, ketchupRepository), nil), githubtest.New().SetLatestVersions(nil, errors.New("failed")), nil), args{ ctx: context.Background(), name: "exist", @@ -202,7 +205,7 @@ func TestGetOrCreate(t *testing.T) { }, { "exists pattern not found", - New(repositorytest.New().SetGetByName(model.NewRepository(1, model.Github, "vibioh/ketchup"), nil), githubtest.New().SetLatestVersions(map[string]semver.Version{ + New(repositorytest.New().SetGetByName(model.NewRepository(1, model.Github, ketchupRepository), nil), githubtest.New().SetLatestVersions(map[string]semver.Version{ "latest": safeParse("1.0.0"), }, nil), nil), args{ @@ -216,7 +219,7 @@ func TestGetOrCreate(t *testing.T) { }, { "exists but no pattern", - New(repositorytest.New().SetGetByName(model.NewRepository(1, model.Github, "vibioh/ketchup"), nil), githubtest.New().SetLatestVersions(map[string]semver.Version{ + New(repositorytest.New().SetGetByName(model.NewRepository(1, model.Github, ketchupRepository), nil), githubtest.New().SetLatestVersions(map[string]semver.Version{ model.DefaultPattern: safeParse("1.0.0"), }, nil), nil), args{ @@ -225,12 +228,12 @@ func TestGetOrCreate(t *testing.T) { repositoryKind: model.Github, pattern: model.DefaultPattern, }, - model.NewRepository(1, model.Github, "vibioh/ketchup").AddVersion(model.DefaultPattern, "1.0.0"), + model.NewRepository(1, model.Github, ketchupRepository).AddVersion(model.DefaultPattern, "1.0.0"), nil, }, { "update error", - New(repositorytest.New().SetGetByName(model.NewRepository(1, model.Helm, "vibioh/ketchup"), nil).SetUpdateVersions(errors.New("failed")), githubtest.New(), helmtest.New().SetLatestVersions(map[string]semver.Version{ + New(repositorytest.New().SetGetByName(model.NewRepository(1, model.Helm, ketchupRepository), nil).SetUpdateVersions(errors.New("failed")), githubtest.New(), helmtest.New().SetLatestVersions(map[string]semver.Version{ model.DefaultPattern: safeParse("1.0.0"), }, nil)), args{ @@ -341,9 +344,9 @@ func TestCreate(t *testing.T) { }, args{ ctx: context.Background(), - item: model.NewRepository(1, model.Github, "vibioh/ketchup").AddVersion(model.DefaultPattern, "0.0.0"), + item: model.NewRepository(1, model.Github, ketchupRepository).AddVersion(model.DefaultPattern, "0.0.0"), }, - model.NewRepository(1, model.Github, "vibioh/ketchup").AddVersion(model.DefaultPattern, "1.0.0"), + model.NewRepository(1, model.Github, ketchupRepository).AddVersion(model.DefaultPattern, "1.0.0"), nil, }, } @@ -399,7 +402,7 @@ func TestUpdate(t *testing.T) { }, { "invalid check", - New(repositorytest.New().SetGet(model.NewRepository(1, model.Github, "vibioh/ketchup"), nil), nil, nil), + New(repositorytest.New().SetGet(model.NewRepository(1, model.Github, ketchupRepository), nil), nil, nil), args{ ctx: context.Background(), item: model.NewRepository(1, model.Github, ""), @@ -408,7 +411,7 @@ func TestUpdate(t *testing.T) { }, { "update error", - New(repositorytest.New().SetGet(model.NewRepository(1, model.Github, "vibioh/ketchup"), nil).SetUpdateVersions(errors.New("failed")), nil, nil), + New(repositorytest.New().SetGet(model.NewRepository(1, model.Github, ketchupRepository), nil).SetUpdateVersions(errors.New("failed")), nil, nil), args{ ctx: context.Background(), item: model.NewRepository(1, model.Github, "").AddVersion(model.DefaultPattern, "1.2.3"), @@ -417,7 +420,7 @@ func TestUpdate(t *testing.T) { }, { "success", - New(repositorytest.New().SetGet(model.NewRepository(1, model.Github, "vibioh/ketchup"), nil), nil, nil), + New(repositorytest.New().SetGet(model.NewRepository(1, model.Github, ketchupRepository), nil), nil, nil), args{ ctx: context.Background(), item: model.NewRepository(3, model.Github, "").AddVersion(model.DefaultPattern, "1.2.3"), @@ -530,8 +533,8 @@ func TestCheck(t *testing.T) { "no kind change", app{repositoryStore: repositorytest.New()}, args{ - old: model.NewRepository(1, model.Github, "vibioh/ketchup").AddVersion(model.DefaultPattern, "1.0.0"), - new: model.NewRepository(1, model.Helm, "vibioh/ketchup"), + old: model.NewRepository(1, model.Github, ketchupRepository).AddVersion(model.DefaultPattern, "1.0.0"), + new: model.NewRepository(1, model.Helm, ketchupRepository), }, errors.New("kind cannot be changed"), }, @@ -539,8 +542,8 @@ func TestCheck(t *testing.T) { "version required for update", app{repositoryStore: repositorytest.New()}, args{ - old: model.NewRepository(1, model.Github, "vibioh/ketchup").AddVersion(model.DefaultPattern, "1.0.0"), - new: model.NewRepository(1, model.Github, "vibioh/ketchup"), + old: model.NewRepository(1, model.Github, ketchupRepository).AddVersion(model.DefaultPattern, "1.0.0"), + new: model.NewRepository(1, model.Github, ketchupRepository), }, errors.New("version is required"), }, @@ -554,7 +557,7 @@ func TestCheck(t *testing.T) { }, { "exist", - app{repositoryStore: repositorytest.New().SetGetByName(model.NewRepository(2, model.Github, "vibioh/ketchup"), nil)}, + app{repositoryStore: repositorytest.New().SetGetByName(model.NewRepository(2, model.Github, ketchupRepository), nil)}, args{ new: model.NewRepository(1, model.Github, "exist"), }, @@ -605,21 +608,21 @@ func TestSanitizeName(t *testing.T) { args{ name: "github.com/vibioh/ketchup", }, - "vibioh/ketchup", + ketchupRepository, }, { "full url", args{ name: "https://github.com/vibioh/ketchup", }, - "vibioh/ketchup", + ketchupRepository, }, { "with suffix", args{ name: "https://github.com/vibioh/ketchup/releases/latest", }, - "vibioh/ketchup", + ketchupRepository, }, } diff --git a/pkg/service/user/user_test.go b/pkg/service/user/user_test.go index 6ce91fce..e3740e76 100644 --- a/pkg/service/user/user_test.go +++ b/pkg/service/user/user_test.go @@ -15,6 +15,8 @@ import ( ) var ( + testEmail = "nobody@localhost" + errAtomicStart = errors.New("invalid context") errAtomicEnd = errors.New("invalid context") ) @@ -56,11 +58,11 @@ func TestStoreInContext(t *testing.T) { }, { "valid", - New(usertest.New().SetGetByLoginID(model.NewUser(1, "nobody@localhost", authModel.NoneUser), nil), nil), + New(usertest.New().SetGetByLoginID(model.NewUser(1, testEmail, authModel.NoneUser), nil), nil), args{ ctx: authModel.StoreUser(context.Background(), authModel.NewUser(1, "")), }, - model.NewUser(1, "nobody@localhost", authModel.NewUser(0, "")), + model.NewUser(1, testEmail, authModel.NewUser(0, "")), }, } @@ -101,7 +103,7 @@ func TestCreate(t *testing.T) { New(usertest.New(), servicetest.New().SetCheck(errors.New("failed"))), args{ ctx: context.TODO(), - item: model.NewUser(0, "nobody@localhost", authModel.NewUser(0, "")), + item: model.NewUser(0, testEmail, authModel.NewUser(0, "")), }, model.NoneUser, httpModel.ErrInvalid, @@ -111,7 +113,7 @@ func TestCreate(t *testing.T) { New(usertest.New().SetGetByEmail(model.NoneUser, nil).SetDoAtomic(errAtomicStart), servicetest.New()), args{ ctx: context.TODO(), - item: model.NewUser(1, "nobody@localhost", authModel.NewUser(1, "")), + item: model.NewUser(1, testEmail, authModel.NewUser(1, "")), }, model.NoneUser, errAtomicStart, @@ -121,7 +123,7 @@ func TestCreate(t *testing.T) { New(usertest.New().SetGetByEmail(model.NoneUser, nil), servicetest.New().SetCreate(authModel.NoneUser, errors.New("failed"))), args{ ctx: context.Background(), - item: model.NewUser(1, "nobody@localhost", authModel.NewUser(1, "")), + item: model.NewUser(1, testEmail, authModel.NewUser(1, "")), }, model.NoneUser, httpModel.ErrInternalError, @@ -131,7 +133,7 @@ func TestCreate(t *testing.T) { New(usertest.New().SetGetByEmail(model.NoneUser, nil).SetCreate(0, errors.New("failed")), servicetest.New()), args{ ctx: context.Background(), - item: model.NewUser(2, "nobody@localhost", authModel.NewUser(2, "")), + item: model.NewUser(2, testEmail, authModel.NewUser(2, "")), }, model.NoneUser, httpModel.ErrInternalError, @@ -141,9 +143,9 @@ func TestCreate(t *testing.T) { New(usertest.New().SetGetByEmail(model.NoneUser, nil).SetCreate(2, nil), servicetest.New().SetCreate(authModel.NewUser(2, "admin"), nil)), args{ ctx: context.Background(), - item: model.NewUser(2, "nobody@localhost", authModel.NewUser(2, "")), + item: model.NewUser(2, testEmail, authModel.NewUser(2, "")), }, - model.NewUser(2, "nobody@localhost", authModel.NewUser(2, "admin")), + model.NewUser(2, testEmail, authModel.NewUser(2, "admin")), nil, }, } @@ -202,16 +204,16 @@ func TestCheck(t *testing.T) { app{userStore: usertest.New().SetGetByEmail(model.NoneUser, errors.New("failed"))}, args{ ctx: context.Background(), - new: model.NewUser(1, "nobody@localhost", authModel.NewUser(1, "")), + new: model.NewUser(1, testEmail, authModel.NewUser(1, "")), }, errors.New("unable to check if email already exists"), }, { "already used", - app{userStore: usertest.New().SetGetByEmail(model.NewUser(1, "nobody@localhost", authModel.NewUser(1, "")), nil)}, + app{userStore: usertest.New().SetGetByEmail(model.NewUser(1, testEmail, authModel.NewUser(1, "")), nil)}, args{ ctx: context.Background(), - new: model.NewUser(1, "nobody@localhost", authModel.NewUser(1, "")), + new: model.NewUser(1, testEmail, authModel.NewUser(1, "")), }, errors.New("email already used"), }, diff --git a/pkg/store/repository/repository_test.go b/pkg/store/repository/repository_test.go index b6180655..0df9761d 100644 --- a/pkg/store/repository/repository_test.go +++ b/pkg/store/repository/repository_test.go @@ -14,6 +14,11 @@ import ( "github.com/lib/pq" ) +var ( + ketchupRepository = "vibioh/ketchup" + viwsRepository = "vibioh/viws" +) + func TestList(t *testing.T) { type args struct { page uint @@ -34,8 +39,8 @@ func TestList(t *testing.T) { pageSize: 20, }, []model.Repository{ - model.NewRepository(1, model.Github, "vibioh/ketchup").AddVersion(model.DefaultPattern, "1.0.0"), - model.NewRepository(2, model.Github, "vibioh/viws").AddVersion(model.DefaultPattern, "1.2.3"), + model.NewRepository(1, model.Github, ketchupRepository).AddVersion(model.DefaultPattern, "1.0.0"), + model.NewRepository(2, model.Github, viwsRepository).AddVersion(model.DefaultPattern, "1.2.3"), }, 2, nil, @@ -92,7 +97,7 @@ func TestList(t *testing.T) { ).WillReturnRows( sqlmock.NewRows([]string{"repository_id", "pattern", "version"}).AddRow(1, model.DefaultPattern, "1.0.0").AddRow(2, model.DefaultPattern, "1.2.3"), ) - rows.AddRow(1, "vibioh/ketchup", "github", 2).AddRow(2, "vibioh/viws", "github", 2) + rows.AddRow(1, ketchupRepository, "github", 2).AddRow(2, viwsRepository, "github", 2) case "timeout": savedSQLTimeout := db.SQLTimeout @@ -103,10 +108,10 @@ func TestList(t *testing.T) { expectedQuery.WillDelayFor(db.SQLTimeout * 2) case "invalid rows": - rows.AddRow("a", "vibioh/ketchup", "github", 2) + rows.AddRow("a", ketchupRepository, "github", 2) case "invalid kind": - rows.AddRow(1, "vibioh/viws", "wrong", 1) + rows.AddRow(1, viwsRepository, "wrong", 1) } got, gotCount, gotErr := New(mockDb).List(context.Background(), tc.args.page, tc.args.pageSize) @@ -154,8 +159,8 @@ func TestSuggest(t *testing.T) { count: 2, }, []model.Repository{ - model.NewRepository(1, model.Github, "vibioh/ketchup").AddVersion(model.DefaultPattern, "1.0.0"), - model.NewRepository(2, model.Helm, "vibioh/viws").AddVersion(model.DefaultPattern, "1.2.3"), + model.NewRepository(1, model.Github, ketchupRepository).AddVersion(model.DefaultPattern, "1.0.0"), + model.NewRepository(2, model.Helm, viwsRepository).AddVersion(model.DefaultPattern, "1.2.3"), }, nil, }, @@ -171,7 +176,7 @@ func TestSuggest(t *testing.T) { rows := sqlmock.NewRows([]string{"id", "name", "kind", "full_count"}) mock.ExpectQuery("SELECT id, name, kind, \\( SELECT COUNT\\(1\\) FROM ketchup.ketchup WHERE repository_id = id \\) AS count FROM ketchup.repository").WithArgs(tc.args.count, pq.Array(tc.args.ignoreIds)).WillReturnRows(rows) - rows.AddRow(1, "vibioh/ketchup", "github", 2).AddRow(2, "vibioh/viws", "helm", 2) + rows.AddRow(1, ketchupRepository, "github", 2).AddRow(2, viwsRepository, "helm", 2) mock.ExpectQuery( "SELECT repository_id, pattern, version FROM ketchup.repository_version WHERE repository_id = ANY", @@ -224,7 +229,7 @@ func TestGet(t *testing.T) { id: 1, }, "SELECT id, name, kind FROM ketchup.repository WHERE id =", - model.NewRepository(1, model.Helm, "vibioh/ketchup").AddVersion(model.DefaultPattern, "1.0.0"), + model.NewRepository(1, model.Helm, ketchupRepository).AddVersion(model.DefaultPattern, "1.0.0"), nil, }, { @@ -234,7 +239,7 @@ func TestGet(t *testing.T) { forUpdate: true, }, "SELECT id, name, kind FROM ketchup.repository WHERE id = .+ FOR UPDATE", - model.NewRepository(1, model.Helm, "vibioh/ketchup").AddVersion(model.DefaultPattern, "1.0.0"), + model.NewRepository(1, model.Helm, ketchupRepository).AddVersion(model.DefaultPattern, "1.0.0"), nil, }, { @@ -264,7 +269,7 @@ func TestGet(t *testing.T) { fallthrough case "for update": - rows.AddRow(1, "vibioh/ketchup", "helm") + rows.AddRow(1, ketchupRepository, "helm") mock.ExpectQuery( "SELECT repository_id, pattern, version FROM ketchup.repository_version WHERE repository_id = ANY", @@ -311,15 +316,15 @@ func TestGetByName(t *testing.T) { { "simple", args{ - name: "vibioh/ketchup", + name: ketchupRepository, }, - model.NewRepository(1, model.Github, "vibioh/ketchup").AddVersion(model.DefaultPattern, "1.0.0"), + model.NewRepository(1, model.Github, ketchupRepository).AddVersion(model.DefaultPattern, "1.0.0"), nil, }, { "no rows", args{ - name: "vibioh/ketchup", + name: ketchupRepository, }, model.NoneRepository, nil, @@ -327,7 +332,7 @@ func TestGetByName(t *testing.T) { { "invalid kind", args{ - name: "vibioh/ketchup", + name: ketchupRepository, }, model.NoneRepository, errors.New("invalid value `wrong` for repository kind"), @@ -343,11 +348,11 @@ func TestGetByName(t *testing.T) { defer mockDb.Close() rows := sqlmock.NewRows([]string{"id", "name", "kind"}) - mock.ExpectQuery("SELECT id, name, kind FROM ketchup.repository WHERE name =").WithArgs("vibioh/ketchup", "github").WillReturnRows(rows) + mock.ExpectQuery("SELECT id, name, kind FROM ketchup.repository WHERE name =").WithArgs(ketchupRepository, "github").WillReturnRows(rows) switch tc.intention { case "simple": - rows.AddRow(1, "vibioh/ketchup", "github") + rows.AddRow(1, ketchupRepository, "github") mock.ExpectQuery( "SELECT repository_id, pattern, version FROM ketchup.repository_version WHERE repository_id = ANY", ).WithArgs( @@ -357,7 +362,7 @@ func TestGetByName(t *testing.T) { ) case "invalid kind": - rows.AddRow(1, "vibioh/ketchup", "wrong") + rows.AddRow(1, ketchupRepository, "wrong") } got, gotErr := New(mockDb).GetByName(context.Background(), tc.args.name, tc.args.repositoryKind) @@ -399,7 +404,7 @@ func TestCreate(t *testing.T) { { "error lock", args{ - o: model.NewRepository(0, model.Github, "vibioh/ketchup").AddVersion(model.DefaultPattern, "1.0.0"), + o: model.NewRepository(0, model.Github, ketchupRepository).AddVersion(model.DefaultPattern, "1.0.0"), }, 0, errors.New("unable to obtain lock"), @@ -407,7 +412,7 @@ func TestCreate(t *testing.T) { { "error get", args{ - o: model.NewRepository(0, model.Github, "vibioh/ketchup").AddVersion(model.DefaultPattern, "1.0.0"), + o: model.NewRepository(0, model.Github, ketchupRepository).AddVersion(model.DefaultPattern, "1.0.0"), }, 0, errors.New("unable to read"), @@ -415,7 +420,7 @@ func TestCreate(t *testing.T) { { "found get", args{ - o: model.NewRepository(0, model.Github, "vibioh/ketchup").AddVersion(model.DefaultPattern, "1.0.0"), + o: model.NewRepository(0, model.Github, ketchupRepository).AddVersion(model.DefaultPattern, "1.0.0"), }, 0, errors.New("repository already exists with name"), @@ -423,7 +428,7 @@ func TestCreate(t *testing.T) { { "error create", args{ - o: model.NewRepository(0, model.Github, "vibioh/ketchup").AddVersion(model.DefaultPattern, "1.0.0"), + o: model.NewRepository(0, model.Github, ketchupRepository).AddVersion(model.DefaultPattern, "1.0.0"), }, 0, errors.New("failed"), @@ -431,7 +436,7 @@ func TestCreate(t *testing.T) { { "success", args{ - o: model.NewRepository(0, model.Github, "vibioh/ketchup").AddVersion(model.DefaultPattern, "1.0.0"), + o: model.NewRepository(0, model.Github, ketchupRepository).AddVersion(model.DefaultPattern, "1.0.0"), }, 1, nil, @@ -455,6 +460,7 @@ func TestCreate(t *testing.T) { ctx = db.StoreTx(ctx, tx) lockQuery := mock.ExpectExec("LOCK ketchup.repository IN SHARE ROW EXCLUSIVE MODE") + selectQueryString := "SELECT id, name, kind FROM ketchup.repository WHERE name = .+ AND kind = .+" switch tc.intention { case "error lock": @@ -462,11 +468,11 @@ func TestCreate(t *testing.T) { case "error get": lockQuery.WillReturnResult(sqlmock.NewResult(0, 0)) - mock.ExpectQuery("SELECT id, name, kind FROM ketchup.repository WHERE name = .+ AND kind = .+").WithArgs("vibioh/ketchup", tc.args.o.Kind.String()).WillReturnError(errors.New("unable to read")) + mock.ExpectQuery(selectQueryString).WithArgs(ketchupRepository, tc.args.o.Kind.String()).WillReturnError(errors.New("unable to read")) case "found get": lockQuery.WillReturnResult(sqlmock.NewResult(0, 0)) - mock.ExpectQuery("SELECT id, name, kind FROM ketchup.repository WHERE name = .+ AND kind = .+").WithArgs("vibioh/ketchup", "github").WillReturnRows(sqlmock.NewRows([]string{"id", "name", "kind"}).AddRow(1, "vibioh/ketchup", "github")) + mock.ExpectQuery(selectQueryString).WithArgs(ketchupRepository, "github").WillReturnRows(sqlmock.NewRows([]string{"id", "name", "kind"}).AddRow(1, ketchupRepository, "github")) mock.ExpectQuery( "SELECT repository_id, pattern, version FROM ketchup.repository_version WHERE repository_id = ANY", ).WithArgs( @@ -477,13 +483,13 @@ func TestCreate(t *testing.T) { case "error create": lockQuery.WillReturnResult(sqlmock.NewResult(0, 0)) - mock.ExpectQuery("SELECT id, name, kind FROM ketchup.repository WHERE name = .+ AND kind = .+").WithArgs("vibioh/ketchup", tc.args.o.Kind.String()).WillReturnRows(sqlmock.NewRows([]string{"id", "name", "kind"})) - mock.ExpectQuery("INSERT INTO ketchup.repository").WithArgs("vibioh/ketchup", "github").WillReturnError(errors.New("failed")) + mock.ExpectQuery(selectQueryString).WithArgs(ketchupRepository, tc.args.o.Kind.String()).WillReturnRows(sqlmock.NewRows([]string{"id", "name", "kind"})) + mock.ExpectQuery("INSERT INTO ketchup.repository").WithArgs(ketchupRepository, "github").WillReturnError(errors.New("failed")) case "success": lockQuery.WillReturnResult(sqlmock.NewResult(0, 0)) - mock.ExpectQuery("SELECT id, name, kind FROM ketchup.repository WHERE name = .+ AND kind = .+").WithArgs("vibioh/ketchup", tc.args.o.Kind.String()).WillReturnRows(sqlmock.NewRows([]string{"id", "name", "kind"})) - mock.ExpectQuery("INSERT INTO ketchup.repository").WithArgs("vibioh/ketchup", "github").WillReturnRows(sqlmock.NewRows([]string{"id"}).AddRow(1)) + mock.ExpectQuery(selectQueryString).WithArgs(ketchupRepository, tc.args.o.Kind.String()).WillReturnRows(sqlmock.NewRows([]string{"id", "name", "kind"})) + mock.ExpectQuery("INSERT INTO ketchup.repository").WithArgs(ketchupRepository, "github").WillReturnRows(sqlmock.NewRows([]string{"id"}).AddRow(1)) mock.ExpectQuery("SELECT pattern, version FROM ketchup.repository_version WHERE repository_id = .+").WithArgs(1).WillReturnRows(sqlmock.NewRows([]string{"pattern", "version"})) mock.ExpectExec("INSERT INTO ketchup.repository_version").WithArgs(1, model.DefaultPattern, "1.0.0").WillReturnResult(sqlmock.NewResult(0, 1)) } diff --git a/pkg/store/user/user_test.go b/pkg/store/user/user_test.go index acfa0824..0a9be603 100644 --- a/pkg/store/user/user_test.go +++ b/pkg/store/user/user_test.go @@ -12,6 +12,10 @@ import ( "github.com/ViBiOh/ketchup/pkg/model" ) +var ( + testEmail = "nobody@localhost" +) + func TestGetByEmail(t *testing.T) { type args struct { email string @@ -26,15 +30,15 @@ func TestGetByEmail(t *testing.T) { { "simple", args{ - email: "nobody@localhost", + email: testEmail, }, - model.NewUser(1, "nobody@localhost", authModel.NewUser(1, "")), + model.NewUser(1, testEmail, authModel.NewUser(1, "")), nil, }, { "no rows", args{ - email: "nobody@localhost", + email: testEmail, }, model.NoneUser, nil, @@ -50,10 +54,10 @@ func TestGetByEmail(t *testing.T) { defer mockDb.Close() rows := sqlmock.NewRows([]string{"id", "email", "login_id"}) - mock.ExpectQuery("SELECT id, email, login_id FROM ketchup.user").WithArgs("nobody@localhost").WillReturnRows(rows) + mock.ExpectQuery("SELECT id, email, login_id FROM ketchup.user").WithArgs(testEmail).WillReturnRows(rows) if tc.intention != "no rows" { - rows.AddRow(1, "nobody@localhost", 1) + rows.AddRow(1, testEmail, 1) } got, gotErr := New(mockDb).GetByEmail(context.Background(), tc.args.email) @@ -93,7 +97,7 @@ func TestGetByLoginID(t *testing.T) { args{ loginID: 2, }, - model.NewUser(1, "nobody@localhost", authModel.NewUser(2, "")), + model.NewUser(1, testEmail, authModel.NewUser(2, "")), nil, }, { @@ -118,7 +122,7 @@ func TestGetByLoginID(t *testing.T) { mock.ExpectQuery("SELECT id, email, login_id FROM ketchup.user").WithArgs(2).WillReturnRows(rows) if tc.intention != "no rows" { - rows.AddRow(1, "nobody@localhost", 2) + rows.AddRow(1, testEmail, 2) } got, gotErr := New(mockDb).GetByLoginID(context.Background(), tc.args.loginID) @@ -156,7 +160,7 @@ func TestCreate(t *testing.T) { { "simple", args{ - o: model.NewUser(0, "nobody@localhost", authModel.User{ + o: model.NewUser(0, testEmail, authModel.User{ ID: 1, Login: "vibioh", Password: "secret", @@ -184,7 +188,7 @@ func TestCreate(t *testing.T) { ctx = db.StoreTx(ctx, tx) } - mock.ExpectQuery("INSERT INTO ketchup.user").WithArgs("nobody@localhost", 1).WillReturnRows(sqlmock.NewRows([]string{"id"}).AddRow(1)) + mock.ExpectQuery("INSERT INTO ketchup.user").WithArgs(testEmail, 1).WillReturnRows(sqlmock.NewRows([]string{"id"}).AddRow(1)) got, gotErr := New(mockDb).Create(ctx, tc.args.o)