Skip to content

Commit

Permalink
Fix refresh tokens tests (#2530)
Browse files Browse the repository at this point in the history
  • Loading branch information
RobiNino authored May 5, 2024
1 parent bf350dc commit 26635ae
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 23 deletions.
19 changes: 13 additions & 6 deletions access_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,12 +121,13 @@ func TestRefreshableAccessTokens(t *testing.T) {
if !assert.NoError(t, err) {
return
}
curAccessToken, curRefreshToken, err := getAccessTokensFromConfig(t, tests.ServerId)
curAccessToken, curRefreshToken, curArtifactoryRefreshToken, err := getTokensFromConfig(t)
if !assert.NoError(t, err) {
return
}
assert.NotEmpty(t, curAccessToken)
assert.NotEmpty(t, curRefreshToken)
assert.Empty(t, curArtifactoryRefreshToken)

// Make the token always refresh.
auth.RefreshPlatformTokenBeforeExpiryMinutes = 365 * 24 * 60
Expand All @@ -137,7 +138,7 @@ func TestRefreshableAccessTokens(t *testing.T) {
if !assert.NoError(t, err) {
return
}
curAccessToken, curRefreshToken, err = assertTokensChanged(t, curAccessToken, curRefreshToken)
curAccessToken, curRefreshToken, err = assertAccessTokensChanged(t, curAccessToken, curRefreshToken)
if !assert.NoError(t, err) {
return
}
Expand All @@ -149,24 +150,30 @@ func TestRefreshableAccessTokens(t *testing.T) {
if !assert.NoError(t, err) {
return
}
newAccessToken, newRefreshToken, err := getArtifactoryTokensFromConfig(t)
newAccessToken, newRefreshToken, newArtifactoryRefreshToken, err := getTokensFromConfig(t)
if !assert.NoError(t, err) {
return
}
assert.Equal(t, curAccessToken, newAccessToken)
assert.Equal(t, curRefreshToken, newRefreshToken)
assert.Empty(t, newArtifactoryRefreshToken)

// Cleanup
cleanArtifactoryTest()
}

func getAccessTokensFromConfig(t *testing.T, serverId string) (accessToken, refreshToken string, err error) {
details, err := config.GetSpecificConfig(serverId, false, false)
// After refreshing an access token, assert that the access token and the refresh token were changed, and the Artifactory refresh token remained empty.
func assertAccessTokensChanged(t *testing.T, curAccessToken, curRefreshToken string) (newAccessToken, newRefreshToken string, err error) {
var newArtifactoryRefreshToken string
newAccessToken, newRefreshToken, newArtifactoryRefreshToken, err = getTokensFromConfig(t)
if err != nil {
assert.NoError(t, err)
return "", "", err
}
return details.AccessToken, details.RefreshToken, nil
assert.NotEqual(t, curAccessToken, newAccessToken)
assert.NotEqual(t, curRefreshToken, newRefreshToken)
assert.Empty(t, newArtifactoryRefreshToken)
return newAccessToken, newRefreshToken, nil
}

const (
Expand Down
33 changes: 19 additions & 14 deletions artifactory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5343,12 +5343,13 @@ func TestRefreshableArtifactoryTokens(t *testing.T) {
if err != nil {
return
}
curAccessToken, curRefreshToken, err := getArtifactoryTokensFromConfig(t)
curAccessToken, curRefreshToken, curArtifactoryRefreshToken, err := getTokensFromConfig(t)
if err != nil {
return
}
assert.NotEmpty(t, curAccessToken)
assert.NotEmpty(t, curRefreshToken)
assert.NotEmpty(t, curArtifactoryRefreshToken)
assert.Empty(t, curRefreshToken)

// Make the token always refresh.
auth.RefreshArtifactoryTokenBeforeExpiryMinutes = 60
Expand All @@ -5359,7 +5360,7 @@ func TestRefreshableArtifactoryTokens(t *testing.T) {
if err != nil {
return
}
curAccessToken, curRefreshToken, err = assertTokensChanged(t, curAccessToken, curRefreshToken)
curAccessToken, curArtifactoryRefreshToken, err = assertArtifactoryTokensChanged(t, curAccessToken, curArtifactoryRefreshToken)
if err != nil {
return
}
Expand All @@ -5374,7 +5375,7 @@ func TestRefreshableArtifactoryTokens(t *testing.T) {
if err != nil {
return
}
curAccessToken, curRefreshToken, err = assertTokensChanged(t, curAccessToken, curRefreshToken)
curAccessToken, curArtifactoryRefreshToken, err = assertArtifactoryTokensChanged(t, curAccessToken, curArtifactoryRefreshToken)
if err != nil {
return
}
Expand All @@ -5389,7 +5390,7 @@ func TestRefreshableArtifactoryTokens(t *testing.T) {
if err != nil {
return
}
curAccessToken, curRefreshToken, err = assertTokensChanged(t, curAccessToken, curRefreshToken)
curAccessToken, curArtifactoryRefreshToken, err = assertArtifactoryTokensChanged(t, curAccessToken, curArtifactoryRefreshToken)
if err != nil {
return
}
Expand All @@ -5401,12 +5402,13 @@ func TestRefreshableArtifactoryTokens(t *testing.T) {
if err != nil {
return
}
newAccessToken, newRefreshToken, err := getArtifactoryTokensFromConfig(t)
newAccessToken, newRefreshToken, newArtifactoryRefreshToken, err := getTokensFromConfig(t)
if err != nil {
return
}
assert.Equal(t, curAccessToken, newAccessToken)
assert.Equal(t, curRefreshToken, newRefreshToken)
assert.Equal(t, curArtifactoryRefreshToken, newArtifactoryRefreshToken)
assert.Empty(t, newRefreshToken)

// Cleanup
cleanArtifactoryTest()
Expand Down Expand Up @@ -5442,24 +5444,27 @@ func setPasswordInConfig(t *testing.T, serverId, password string) error {
return nil
}

func getArtifactoryTokensFromConfig(t *testing.T) (accessToken, refreshToken string, err error) {
func getTokensFromConfig(t *testing.T) (accessToken, refreshToken, artifactoryRefreshToken string, err error) {
details, err := config.GetSpecificConfig(tests.ServerId, false, false)
if err != nil {
assert.NoError(t, err)
return "", "", err
return "", "", "", err
}
return details.AccessToken, details.ArtifactoryRefreshToken, nil
return details.AccessToken, details.RefreshToken, details.ArtifactoryRefreshToken, nil
}

func assertTokensChanged(t *testing.T, curAccessToken, curRefreshToken string) (newAccessToken, newRefreshToken string, err error) {
newAccessToken, newRefreshToken, err = getArtifactoryTokensFromConfig(t)
// After refreshing an Artifactory access token, assert that the access token and the artifactory refresh token were changed, and refresh token remained empty.
func assertArtifactoryTokensChanged(t *testing.T, curAccessToken, curArtifactoryRefreshToken string) (newAccessToken, newArtifactoryRefreshToken string, err error) {
var newRefreshToken string
newAccessToken, newRefreshToken, newArtifactoryRefreshToken, err = getTokensFromConfig(t)
if err != nil {
assert.NoError(t, err)
return "", "", err
}
assert.NotEqual(t, curAccessToken, newAccessToken)
assert.NotEqual(t, curRefreshToken, newRefreshToken)
return newAccessToken, newRefreshToken, nil
assert.NotEqual(t, curArtifactoryRefreshToken, newArtifactoryRefreshToken)
assert.Empty(t, newRefreshToken)
return newAccessToken, newArtifactoryRefreshToken, nil
}

func uploadWithSpecificServerAndVerify(t *testing.T, cli *coretests.JfrogCli, source string, expectedResults int) error {
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ require (
gopkg.in/yaml.v3 v3.0.1 // indirect
)

replace github.com/jfrog/jfrog-cli-core/v2 => github.com/jfrog/jfrog-cli-core/v2 v2.31.1-0.20240502151649-934fb7391b5c
replace github.com/jfrog/jfrog-cli-core/v2 => github.com/jfrog/jfrog-cli-core/v2 v2.31.1-0.20240505135056-aa682847c303

replace github.com/jfrog/jfrog-client-go => github.com/jfrog/jfrog-client-go v1.28.1-0.20240430132902-9d4dfaf05041

Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,8 @@ github.com/jfrog/gofrog v1.7.1 h1:ME1Meg4hukAT/7X6HUQCVSe4DNjMZACCP8aCY37EW/w=
github.com/jfrog/gofrog v1.7.1/go.mod h1:X7bjfWoQDN0Z4FQGbE91j3gbPP7Urwzm4Z8tkvrlbRI=
github.com/jfrog/jfrog-apps-config v1.0.1 h1:mtv6k7g8A8BVhlHGlSveapqf4mJfonwvXYLipdsOFMY=
github.com/jfrog/jfrog-apps-config v1.0.1/go.mod h1:8AIIr1oY9JuH5dylz2S6f8Ym2MaadPLR6noCBO4C22w=
github.com/jfrog/jfrog-cli-core/v2 v2.31.1-0.20240502151649-934fb7391b5c h1:7pvL4q0Mo3i7b7/r8+FriL/j2JwI6AIDn5QTfCKvLSk=
github.com/jfrog/jfrog-cli-core/v2 v2.31.1-0.20240502151649-934fb7391b5c/go.mod h1:S0hIZcojq4GehyOOSt0o9x4fExWFtamwfFc7bfIWW+4=
github.com/jfrog/jfrog-cli-core/v2 v2.31.1-0.20240505135056-aa682847c303 h1:UARvAZKTfwmbcNX6g2SS9hCs/rp/YwK+UJe6BNm8zKA=
github.com/jfrog/jfrog-cli-core/v2 v2.31.1-0.20240505135056-aa682847c303/go.mod h1:S0hIZcojq4GehyOOSt0o9x4fExWFtamwfFc7bfIWW+4=
github.com/jfrog/jfrog-cli-security v1.1.0 h1:ifCjFJSa1D1pWyW/ADYPqnMkOddzkAT/WY4vHAufn1g=
github.com/jfrog/jfrog-cli-security v1.1.0/go.mod h1:086t7e/einVAGfBXxRdEGDKovWt67I6SqUb1rcpdiZc=
github.com/jfrog/jfrog-client-go v1.28.1-0.20240430132902-9d4dfaf05041 h1:cDTm4RxkI+RA8aNmdfM2BVxb5iCoXjLjhb+M+ReEIrE=
Expand Down

0 comments on commit 26635ae

Please sign in to comment.