2019-02-03 18:56:53 -05:00
// Copyright 2019 The Gitea Authors. All rights reserved.
2022-11-27 13:20:29 -05:00
// SPDX-License-Identifier: MIT
2019-02-03 18:56:53 -05:00
2022-09-02 14:18:23 -05:00
package integration
2019-02-03 18:56:53 -05:00
import (
"fmt"
"net/http"
"net/url"
2021-09-22 00:38:34 -05:00
"os"
2019-02-03 18:56:53 -05:00
"path/filepath"
"testing"
"time"
2023-01-17 16:46:03 -05:00
auth_model "code.gitea.io/gitea/models/auth"
2019-03-27 04:33:00 -05:00
"code.gitea.io/gitea/modules/git"
2019-05-11 05:21:34 -05:00
api "code.gitea.io/gitea/modules/structs"
2019-08-23 11:40:30 -05:00
2019-02-03 18:56:53 -05:00
"github.com/stretchr/testify/assert"
)
func doCheckRepositoryEmptyStatus ( ctx APITestContext , isEmpty bool ) func ( * testing . T ) {
return doAPIGetRepository ( ctx , func ( t * testing . T , repository api . Repository ) {
assert . Equal ( t , isEmpty , repository . Empty )
} )
}
func doAddChangesToCheckout ( dstPath , filename string ) func ( * testing . T ) {
return func ( t * testing . T ) {
2022-01-20 12:46:10 -05:00
assert . NoError ( t , os . WriteFile ( filepath . Join ( dstPath , filename ) , [ ] byte ( fmt . Sprintf ( "# Testing Repository\n\nOriginally created in: %s at time: %v" , dstPath , time . Now ( ) ) ) , 0 o644 ) )
2019-02-03 18:56:53 -05:00
assert . NoError ( t , git . AddChanges ( dstPath , true ) )
signature := git . Signature {
Email : "test@example.com" ,
Name : "test" ,
When : time . Now ( ) ,
}
assert . NoError ( t , git . CommitChanges ( dstPath , git . CommitChangesOptions {
Committer : & signature ,
Author : & signature ,
Message : "Initial Commit" ,
} ) )
}
}
func TestPushDeployKeyOnEmptyRepo ( t * testing . T ) {
onGiteaRun ( t , testPushDeployKeyOnEmptyRepo )
}
func testPushDeployKeyOnEmptyRepo ( t * testing . T , u * url . URL ) {
// OK login
2023-01-17 16:46:03 -05:00
ctx := NewAPITestContext ( t , "user2" , "deploy-key-empty-repo-1" , auth_model . AccessTokenScopeRepo )
ctxWithDeleteRepo := NewAPITestContext ( t , "user2" , "deploy-key-empty-repo-1" , auth_model . AccessTokenScopeRepo , auth_model . AccessTokenScopeDeleteRepo )
2019-02-03 18:56:53 -05:00
keyname := fmt . Sprintf ( "%s-push" , ctx . Reponame )
u . Path = ctx . GitPath ( )
t . Run ( "CreateEmptyRepository" , doAPICreateRepository ( ctx , true ) )
t . Run ( "CheckIsEmpty" , doCheckRepositoryEmptyStatus ( ctx , true ) )
withKeyFile ( t , keyname , func ( keyFile string ) {
t . Run ( "CreatePushDeployKey" , doAPICreateDeployKey ( ctx , keyname , keyFile , false ) )
// Setup the testing repository
2022-09-04 10:14:53 -05:00
dstPath := t . TempDir ( )
2019-02-03 18:56:53 -05:00
t . Run ( "InitTestRepository" , doGitInitTestRepository ( dstPath ) )
2022-01-20 12:46:10 -05:00
// Setup remote link
2019-02-03 18:56:53 -05:00
sshURL := createSSHUrl ( ctx . GitPath ( ) , u )
t . Run ( "AddRemote" , doGitAddRemote ( dstPath , "origin" , sshURL ) )
t . Run ( "SSHPushTestRepository" , doGitPushTestRepository ( dstPath , "origin" , "master" ) )
t . Run ( "CheckIsNotEmpty" , doCheckRepositoryEmptyStatus ( ctx , false ) )
2023-01-17 16:46:03 -05:00
t . Run ( "DeleteRepository" , doAPIDeleteRepository ( ctxWithDeleteRepo ) )
2019-02-03 18:56:53 -05:00
} )
}
func TestKeyOnlyOneType ( t * testing . T ) {
onGiteaRun ( t , testKeyOnlyOneType )
}
func testKeyOnlyOneType ( t * testing . T , u * url . URL ) {
// Once a key is a user key we cannot use it as a deploy key
// If we delete it from the user we should be able to use it as a deploy key
reponame := "ssh-key-test-repo"
username := "user2"
u . Path = fmt . Sprintf ( "%s/%s.git" , username , reponame )
keyname := fmt . Sprintf ( "%s-push" , reponame )
// OK login
2023-01-17 16:46:03 -05:00
ctx := NewAPITestContext ( t , username , reponame , auth_model . AccessTokenScopeRepo , auth_model . AccessTokenScopeAdminPublicKey )
ctxWithDeleteRepo := NewAPITestContext ( t , username , reponame , auth_model . AccessTokenScopeRepo , auth_model . AccessTokenScopeAdminPublicKey , auth_model . AccessTokenScopeDeleteRepo )
2019-02-03 18:56:53 -05:00
otherCtx := ctx
otherCtx . Reponame = "ssh-key-test-repo-2"
2023-01-17 16:46:03 -05:00
otherCtxWithDeleteRepo := ctxWithDeleteRepo
otherCtxWithDeleteRepo . Reponame = otherCtx . Reponame
2019-02-03 18:56:53 -05:00
failCtx := ctx
failCtx . ExpectedCode = http . StatusUnprocessableEntity
t . Run ( "CreateRepository" , doAPICreateRepository ( ctx , false ) )
t . Run ( "CreateOtherRepository" , doAPICreateRepository ( otherCtx , false ) )
withKeyFile ( t , keyname , func ( keyFile string ) {
var userKeyPublicKeyID int64
t . Run ( "KeyCanOnlyBeUser" , func ( t * testing . T ) {
2022-09-04 10:14:53 -05:00
dstPath := t . TempDir ( )
2019-02-03 18:56:53 -05:00
sshURL := createSSHUrl ( ctx . GitPath ( ) , u )
2020-04-28 03:32:23 -05:00
t . Run ( "FailToClone" , doGitCloneFail ( sshURL ) )
2019-02-03 18:56:53 -05:00
t . Run ( "CreateUserKey" , doAPICreateUserKey ( ctx , keyname , keyFile , func ( t * testing . T , publicKey api . PublicKey ) {
userKeyPublicKeyID = publicKey . ID
} ) )
t . Run ( "FailToAddReadOnlyDeployKey" , doAPICreateDeployKey ( failCtx , keyname , keyFile , true ) )
t . Run ( "FailToAddDeployKey" , doAPICreateDeployKey ( failCtx , keyname , keyFile , false ) )
t . Run ( "Clone" , doGitClone ( dstPath , sshURL ) )
t . Run ( "AddChanges" , doAddChangesToCheckout ( dstPath , "CHANGES1.md" ) )
t . Run ( "Push" , doGitPushTestRepository ( dstPath , "origin" , "master" ) )
t . Run ( "DeleteUserKey" , doAPIDeleteUserKey ( ctx , userKeyPublicKeyID ) )
} )
t . Run ( "KeyCanBeAnyDeployButNotUserAswell" , func ( t * testing . T ) {
2022-09-04 10:14:53 -05:00
dstPath := t . TempDir ( )
2019-02-03 18:56:53 -05:00
sshURL := createSSHUrl ( ctx . GitPath ( ) , u )
2020-04-28 03:32:23 -05:00
t . Run ( "FailToClone" , doGitCloneFail ( sshURL ) )
2019-02-03 18:56:53 -05:00
// Should now be able to add...
t . Run ( "AddReadOnlyDeployKey" , doAPICreateDeployKey ( ctx , keyname , keyFile , true ) )
t . Run ( "Clone" , doGitClone ( dstPath , sshURL ) )
t . Run ( "AddChanges" , doAddChangesToCheckout ( dstPath , "CHANGES2.md" ) )
t . Run ( "FailToPush" , doGitPushTestRepositoryFail ( dstPath , "origin" , "master" ) )
otherSSHURL := createSSHUrl ( otherCtx . GitPath ( ) , u )
2022-09-04 10:14:53 -05:00
dstOtherPath := t . TempDir ( )
2019-02-03 18:56:53 -05:00
t . Run ( "AddWriterDeployKeyToOther" , doAPICreateDeployKey ( otherCtx , keyname , keyFile , false ) )
t . Run ( "CloneOther" , doGitClone ( dstOtherPath , otherSSHURL ) )
t . Run ( "AddChangesToOther" , doAddChangesToCheckout ( dstOtherPath , "CHANGES3.md" ) )
t . Run ( "PushToOther" , doGitPushTestRepository ( dstOtherPath , "origin" , "master" ) )
t . Run ( "FailToCreateUserKey" , doAPICreateUserKey ( failCtx , keyname , keyFile ) )
} )
t . Run ( "DeleteRepositoryShouldReleaseKey" , func ( t * testing . T ) {
otherSSHURL := createSSHUrl ( otherCtx . GitPath ( ) , u )
2022-09-04 10:14:53 -05:00
dstOtherPath := t . TempDir ( )
2019-02-03 18:56:53 -05:00
2023-01-17 16:46:03 -05:00
t . Run ( "DeleteRepository" , doAPIDeleteRepository ( ctxWithDeleteRepo ) )
2019-02-03 18:56:53 -05:00
t . Run ( "FailToCreateUserKeyAsStillDeploy" , doAPICreateUserKey ( failCtx , keyname , keyFile ) )
t . Run ( "MakeSureCloneOtherStillWorks" , doGitClone ( dstOtherPath , otherSSHURL ) )
t . Run ( "AddChangesToOther" , doAddChangesToCheckout ( dstOtherPath , "CHANGES3.md" ) )
t . Run ( "PushToOther" , doGitPushTestRepository ( dstOtherPath , "origin" , "master" ) )
2023-01-17 16:46:03 -05:00
t . Run ( "DeleteOtherRepository" , doAPIDeleteRepository ( otherCtxWithDeleteRepo ) )
2019-02-03 18:56:53 -05:00
2023-01-17 16:46:03 -05:00
t . Run ( "RecreateRepository" , doAPICreateRepository ( ctxWithDeleteRepo , false ) )
2019-02-03 18:56:53 -05:00
t . Run ( "CreateUserKey" , doAPICreateUserKey ( ctx , keyname , keyFile , func ( t * testing . T , publicKey api . PublicKey ) {
userKeyPublicKeyID = publicKey . ID
} ) )
2022-09-04 10:14:53 -05:00
dstPath := t . TempDir ( )
2019-02-03 18:56:53 -05:00
sshURL := createSSHUrl ( ctx . GitPath ( ) , u )
t . Run ( "Clone" , doGitClone ( dstPath , sshURL ) )
t . Run ( "AddChanges" , doAddChangesToCheckout ( dstPath , "CHANGES1.md" ) )
t . Run ( "Push" , doGitPushTestRepository ( dstPath , "origin" , "master" ) )
} )
t . Run ( "DeleteUserKeyShouldRemoveAbilityToClone" , func ( t * testing . T ) {
sshURL := createSSHUrl ( ctx . GitPath ( ) , u )
t . Run ( "DeleteUserKey" , doAPIDeleteUserKey ( ctx , userKeyPublicKeyID ) )
2020-04-28 03:32:23 -05:00
t . Run ( "FailToClone" , doGitCloneFail ( sshURL ) )
2019-02-03 18:56:53 -05:00
} )
} )
}