package githubclient import ( "testing" "github.com/stretchr/testify/assert" ) func TestAuth_Validate(t *testing.T) { tcs := map[string]struct { config Config valid bool }{ // Bad "badConfigNoAuthPresent": { Config{ Repository: "https://github.com/foo/bar.git", }, false, }, "badConfigNoAppID": { Config{ Repository: "https://github.com/foo/bar.git", PrivateKey: []byte("foo"), InstallationID: 1234, }, false, }, "badConfigNoPrivateKey": { Config{ Repository: "https://github.com/foo/bar.git", AppID: 1234, InstallationID: 1234, AccessToken: "asdf", }, true, }, "badConfigNoPrivateKeyOrAccessToken": { Config{ Repository: "https://github.com/foo/bar.git", AppID: 1234, InstallationID: 1234, }, false, }, // Good "goodConfigGithubApp": { Config{ Repository: "https://github.com/place-holder/for-unit-tests", PrivateKey: []byte("foo"), AppID: 1234, InstallationID: 1234, }, true, }, "goodConfigNoInstallationID": { Config{ Repository: "https://github.com/foo/bar.git", PrivateKey: []byte("foo"), AppID: 1234, }, true, }, } for name, tc := range tcs { t.Run(name, func(t *testing.T) { switch tc.valid { case true: assert.NoError(t, tc.config.Validate()) case false: assert.Error(t, tc.config.Validate()) } }) } } func TestRepo_Validate(t *testing.T) { type Test struct { Config expectOwner string expectRepo string } tcs := map[string]struct { test Test valid bool }{ "badConfigNoRepository": { Test{ Config: Config{ Repository: "", PrivateKey: []byte("foo"), AppID: 1234, }, }, false, }, "badConfigNonHTTPSConfig": { Test{ Config: Config{ Repository: "http://github.com/foo/bar.git", PrivateKey: []byte("foo"), AppID: 1234, }, }, false, }, "goodConfigOne": { Test{ Config: Config{ Repository: "https://github.com/foo/bar.git", PrivateKey: []byte("foo"), AppID: 1234, }, expectOwner: "foo", expectRepo: "bar", }, true, }, "goodConfigTwo": { Test{ Config: Config{ Repository: "https://github.com/foo-bar/baz-bit.git", PrivateKey: []byte("foo"), AppID: 1234, }, expectOwner: "foo-bar", expectRepo: "baz-bit", }, true, }, "goodConfigThree": { Test{ Config: Config{ Repository: "https://github.com/foo/bar-baz", PrivateKey: []byte("foo"), AppID: 1234, }, expectOwner: "foo", expectRepo: "bar-baz", }, true, }, } for name, tc := range tcs { t.Run(name, func(t *testing.T) { switch tc.valid { case true: actualOwner := tc.test.Config.Owner() actualRepo := tc.test.Config.Repo() assert.Equal(t, tc.test.expectOwner, actualOwner) assert.Equal(t, tc.test.expectRepo, actualRepo) case false: err := tc.test.Config.Validate() assert.Error(t, err) } }) } }