Skip to content

Commit

Permalink
CR comments
Browse files Browse the repository at this point in the history
  • Loading branch information
RobiNino committed Oct 2, 2023
1 parent 96d7688 commit da83114
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 94 deletions.
200 changes: 113 additions & 87 deletions access_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,20 @@ import (
coreTests "github.com/jfrog/jfrog-cli-core/v2/utils/tests"
"github.com/jfrog/jfrog-cli/utils/tests"
"github.com/jfrog/jfrog-client-go/auth"
"github.com/jfrog/jfrog-client-go/http/httpclient"
clientUtils "github.com/jfrog/jfrog-client-go/utils"
"github.com/jfrog/jfrog-client-go/utils/errorutils"
"github.com/jfrog/jfrog-client-go/utils/io/httputils"
clientTestUtils "github.com/jfrog/jfrog-client-go/utils/tests"
"github.com/stretchr/testify/assert"
"net/http"
"testing"
)

var (
accessDetails *config.ServerDetails
accessCli *tests.JfrogCli
accessDetails *config.ServerDetails
accessCli *tests.JfrogCli
accessHttpDetails httputils.HttpClientDetails
)

func initAccessTest(t *testing.T) {
Expand All @@ -28,6 +33,47 @@ func initAccessTest(t *testing.T) {
}
}

func initAccessCli() {
if accessCli != nil {
return
}
accessCli = tests.NewJfrogCli(execMain, "jfrog", authenticateAccess())
}

func InitAccessTests() {
initArtifactoryCli()
initAccessCli()
cleanUpOldBuilds()
cleanUpOldRepositories()
cleanUpOldUsers()
tests.AddTimestampToGlobalVars()
createRequiredRepos()
cleanArtifactoryTest()
}

func authenticateAccess() string {
*tests.JfrogUrl = clientUtils.AddTrailingSlashIfNeeded(*tests.JfrogUrl)
accessDetails = &config.ServerDetails{
AccessUrl: *tests.JfrogUrl + tests.AccessEndpoint}

cred := fmt.Sprintf("--url=%s", *tests.JfrogUrl)
if *tests.JfrogAccessToken != "" {
accessDetails.AccessToken = *tests.JfrogAccessToken
cred += fmt.Sprintf(" --access-token=%s", accessDetails.AccessToken)
} else {
accessDetails.User = *tests.JfrogUser
accessDetails.Password = *tests.JfrogPassword
cred += fmt.Sprintf(" --user=%s --password=%s", accessDetails.User, accessDetails.Password)
}

accessAuth, err := accessDetails.CreateAccessAuthConfig()
if err != nil {
coreutils.ExitOnErr(err)
}
accessHttpDetails = accessAuth.CreateHttpClientDetails()
return cred
}

func TestSetupInvitedUser(t *testing.T) {
initAccessTest(t)
tempDirPath, createTempDirCallback := coreTests.CreateTempDirWithCallbackAndAssert(t)
Expand Down Expand Up @@ -128,72 +174,74 @@ const (
defaultExpiry = 31536000
)

var atcTestCases = []struct {
name string
args []string
shouldExpire bool
expectedExpiry uint
expectedScope string
expectedRefreshable bool
expectedReference bool
}{
{
name: "default",
args: []string{"atc"},
shouldExpire: true,
expectedExpiry: defaultExpiry,
expectedScope: userScope,
expectedRefreshable: false,
expectedReference: false,
},
{
name: "explicit user, no expiry",
args: []string{"atc", auth.ExtractUsernameFromAccessToken(*tests.JfrogAccessToken), "--expiry=0"},
shouldExpire: false,
expectedExpiry: 0,
expectedScope: userScope,
expectedRefreshable: false,
expectedReference: false,
},
{
name: "refreshable, admin",
args: []string{"atc", "--refreshable", "--grant-admin"},
shouldExpire: true,
expectedExpiry: defaultExpiry,
expectedScope: "applied-permissions/admin",
expectedRefreshable: true,
expectedReference: false,
},
{
name: "reference, custom scope, custom expiry",
args: []string{"atc", "--reference", "--scope=system:metrics:r", "--expiry=1234"},
shouldExpire: true,
expectedExpiry: 1234,
expectedScope: "system:metrics:r",
expectedRefreshable: false,
expectedReference: true,
},
{
name: "groups, description",
args: []string{"atc", "--groups=group1,group2", "--description=description"},
shouldExpire: true,
expectedExpiry: defaultExpiry,
expectedScope: "applied-permissions/groups:group1,group2",
expectedRefreshable: false,
expectedReference: false,
},
}

func TestAccessTokenCreate(t *testing.T) {
initAccessTest(t)
if *tests.JfrogAccessToken == "" {
t.Skip("access token create command only supports authorization with access token, but a token is not provided. Skipping...")
}
testCases := []struct {
name string
args []string
shouldExpire bool
expectedExpiry uint
expectedScope string
expectedRefreshable bool
expectedReference bool
}{
{
name: "default",
args: []string{"atc"},
shouldExpire: true,
expectedExpiry: defaultExpiry,
expectedScope: userScope,
expectedRefreshable: false,
expectedReference: false,
},
{
name: "explicit user, no expiry",
args: []string{"atc", auth.ExtractUsernameFromAccessToken(*tests.JfrogAccessToken), "--expiry=0"},
shouldExpire: false,
expectedExpiry: 0,
expectedScope: userScope,
expectedRefreshable: false,
expectedReference: false,
},
{
name: "refreshable, admin",
args: []string{"atc", "--refreshable", "--grant-admin"},
shouldExpire: true,
expectedExpiry: defaultExpiry,
expectedScope: "applied-permissions/admin",
expectedRefreshable: true,
expectedReference: false,
},
{
name: "reference, custom scope, custom expiry",
args: []string{"atc", "--reference", "--scope=system:metrics:r", "--expiry=1234"},
shouldExpire: true,
expectedExpiry: 1234,
expectedScope: "system:metrics:r",
expectedRefreshable: false,
expectedReference: true,
},
{
name: "groups, description",
args: []string{"atc", "--groups=group1,group2", "--description=description"},
shouldExpire: true,
expectedExpiry: defaultExpiry,
expectedScope: "applied-permissions/groups:group1,group2",
expectedRefreshable: false,
expectedReference: false,
},
}

for _, test := range testCases {
for _, test := range atcTestCases {
t.Run(test.name, func(t *testing.T) {
var token auth.CreateTokenResponseData
output := accessCli.RunCliCmdWithOutput(t, test.args...)
assert.NoError(t, json.Unmarshal([]byte(output), &token))
defer revokeToken(t, token.TokenId)

if test.shouldExpire {
assert.EqualValues(t, test.expectedExpiry, *token.ExpiresIn)
Expand All @@ -220,37 +268,15 @@ func assertNotEmptyIfExpected(t *testing.T, expected bool, output string) {
}
}

func initAccessCli() {
if accessCli != nil {
func revokeToken(t *testing.T, tokenId string) {
if tokenId == "" {
return
}
accessCli = tests.NewJfrogCli(execMain, "jfrog", authenticateAccess())
}

func InitAccessTests() {
initArtifactoryCli()
initAccessCli()
cleanUpOldBuilds()
cleanUpOldRepositories()
cleanUpOldUsers()
tests.AddTimestampToGlobalVars()
createRequiredRepos()
cleanArtifactoryTest()
}

func authenticateAccess() string {
*tests.JfrogUrl = clientUtils.AddTrailingSlashIfNeeded(*tests.JfrogUrl)
accessDetails = &config.ServerDetails{
AccessUrl: *tests.JfrogUrl + tests.AccessEndpoint}
client, err := httpclient.ClientBuilder().Build()
assert.NoError(t, err)

cred := fmt.Sprintf("--url=%s", *tests.JfrogUrl)
if *tests.JfrogAccessToken != "" {
accessDetails.AccessToken = *tests.JfrogAccessToken
cred += fmt.Sprintf(" --access-token=%s", accessDetails.AccessToken)
} else {
accessDetails.User = *tests.JfrogUser
accessDetails.Password = *tests.JfrogPassword
cred += fmt.Sprintf(" --user=%s --password=%s", accessDetails.User, accessDetails.Password)
}
return cred
resp, _, err := client.SendDelete(*tests.JfrogUrl+"access/api/v1/tokens/"+tokenId, nil, accessHttpDetails, "")
assert.NoError(t, err)
assert.NoError(t, errorutils.CheckResponseStatus(resp, http.StatusOK))
}
2 changes: 1 addition & 1 deletion docs/general/token/help.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ var Usage = []string{"atc", "atc <username>"}
func GetDescription() string {
return `Creates an access token.
By default, an user-scoped token will be created.
Administrator may provide the scope explicitly with '--scope', or implicitly with '--groups', '--grant-admin'`
Administrator may provide the scope explicitly with '--scope', or implicitly with '--groups', '--grant-admin'.`
}

func GetArguments() string {
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -128,10 +128,10 @@ require (

replace github.com/jfrog/build-info-go => github.com/jfrog/build-info-go v1.8.9-0.20230928084830-478bd49f5d3e

replace github.com/jfrog/jfrog-cli-core/v2 => github.com/RobiNino/jfrog-cli-core/v2 v2.0.0-20231002131847-e7ac8274f4e3
replace github.com/jfrog/jfrog-cli-core/v2 => github.com/RobiNino/jfrog-cli-core/v2 v2.0.0-20231002153919-5ea5f96bc2a1

// replace github.com/jfrog/gofrog => github.com/jfrog/gofrog v1.2.6-0.20230418122323-2bf299dd6d27

replace github.com/jfrog/jfrog-client-go => github.com/RobiNino/jfrog-client-go v0.0.0-20231002130445-b16f43c60c85
replace github.com/jfrog/jfrog-client-go => github.com/RobiNino/jfrog-client-go v0.0.0-20231002153224-502dc4746fbc

// replace github.com/jfrog/build-info-go => github.com/jfrog/build-info-go v1.8.9-0.20230831151231-e5e7bd035ddc
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,10 @@ github.com/Microsoft/hcsshim v0.11.0 h1:7EFNIY4igHEXUdj1zXgAyU3fLc7QfOKHbkldRVTB
github.com/Microsoft/hcsshim v0.11.0/go.mod h1:OEthFdQv/AD2RAdzR6Mm1N1KPCztGKDurW1Z8b8VGMM=
github.com/ProtonMail/go-crypto v0.0.0-20230923063757-afb1ddc0824c h1:kMFnB0vCcX7IL/m9Y5LO+KQYv+t1CQOiFe6+SV2J7bE=
github.com/ProtonMail/go-crypto v0.0.0-20230923063757-afb1ddc0824c/go.mod h1:EjAoLdwvbIOoOQr3ihjnSoLZRtE8azugULFRteWMNc0=
github.com/RobiNino/jfrog-cli-core/v2 v2.0.0-20231002131847-e7ac8274f4e3 h1:p2Q/CBUdWPEmFQ7lPYElNvezgXKBowWFzeTvjjZzluE=
github.com/RobiNino/jfrog-cli-core/v2 v2.0.0-20231002131847-e7ac8274f4e3/go.mod h1:Ij3mfD91tmAMnQ19cFuv2eCs3UAsCfZ54ICxdQtxUg4=
github.com/RobiNino/jfrog-client-go v0.0.0-20231002130445-b16f43c60c85 h1:lfFzAw4zLbV/zlvQVy0441vvkJbqIBFOgPJtptn1G64=
github.com/RobiNino/jfrog-client-go v0.0.0-20231002130445-b16f43c60c85/go.mod h1:AePTNv5H1YSGycxiL+1jXHCzqu3rCGruVP7S0N+BEEo=
github.com/RobiNino/jfrog-cli-core/v2 v2.0.0-20231002153919-5ea5f96bc2a1 h1:hSHUwPYQ/zU186qm9fBqXldYbH5RSbbkXPDBh+yOzUU=
github.com/RobiNino/jfrog-cli-core/v2 v2.0.0-20231002153919-5ea5f96bc2a1/go.mod h1:fOwXp2oGZYSLSAU9z36X9cscZlsIW2XLbfAxIDpTobU=
github.com/RobiNino/jfrog-client-go v0.0.0-20231002153224-502dc4746fbc h1:dYkzhDo2Qqx4HVcuwbpq4pTL+D29PkyVHccR9ElD4A8=
github.com/RobiNino/jfrog-client-go v0.0.0-20231002153224-502dc4746fbc/go.mod h1:AePTNv5H1YSGycxiL+1jXHCzqu3rCGruVP7S0N+BEEo=
github.com/VividCortex/ewma v1.2.0 h1:f58SaIzcDXrSy3kWaHNvuJgJ3Nmz59Zji6XoJR/q1ow=
github.com/VividCortex/ewma v1.2.0/go.mod h1:nz4BbCtbLyFDeC9SUHbtcT5644juEuWfUAUnGx7j5l4=
github.com/acarl005/stripansi v0.0.0-20180116102854-5a71ef0e047d h1:licZJFw2RwpHMqeKTCYkitsPqHNxTmd4SNR5r94FGM8=
Expand Down

0 comments on commit da83114

Please sign in to comment.