2017-09-05 16:45:18 +03:00
// Copyright 2017 The Gogs Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package integrations
import (
"net/http"
2017-12-04 01:46:01 +03:00
"net/http/httptest"
2017-09-05 16:45:18 +03:00
"strconv"
"testing"
2019-05-11 13:21:34 +03:00
api "code.gitea.io/gitea/modules/structs"
2017-12-04 01:46:01 +03:00
"github.com/stretchr/testify/assert"
2017-09-05 16:45:18 +03:00
)
2017-12-04 01:46:01 +03:00
type makeRequestFunc func ( testing . TB , * http . Request , int ) * httptest . ResponseRecorder
2017-09-05 16:45:18 +03:00
func TestGPGKeys ( t * testing . T ) {
2019-11-26 02:21:37 +03:00
defer prepareTestEnv ( t ) ( )
2017-09-05 16:45:18 +03:00
session := loginUser ( t , "user2" )
2018-09-10 19:15:52 +03:00
token := getTokenForLoggedInUser ( t , session )
2017-09-05 16:45:18 +03:00
tt := [ ] struct {
2017-12-04 01:46:01 +03:00
name string
makeRequest makeRequestFunc
2018-09-10 19:15:52 +03:00
token string
2017-12-04 01:46:01 +03:00
results [ ] int
2017-09-05 16:45:18 +03:00
} {
2018-09-10 19:15:52 +03:00
{ name : "NoLogin" , makeRequest : MakeRequest , token : "" ,
2017-09-05 16:45:18 +03:00
results : [ ] int { http . StatusUnauthorized , http . StatusUnauthorized , http . StatusUnauthorized , http . StatusUnauthorized , http . StatusUnauthorized , http . StatusUnauthorized , http . StatusUnauthorized , http . StatusUnauthorized } ,
} ,
2018-09-10 19:15:52 +03:00
{ name : "LoggedAsUser2" , makeRequest : session . MakeRequest , token : token ,
2020-05-29 00:25:54 +03:00
results : [ ] int { http . StatusOK , http . StatusOK , http . StatusNotFound , http . StatusNoContent , http . StatusUnprocessableEntity , http . StatusNotFound , http . StatusCreated , http . StatusCreated } } ,
2017-09-05 16:45:18 +03:00
}
for _ , tc := range tt {
//Basic test on result code
t . Run ( tc . name , func ( t * testing . T ) {
t . Run ( "ViewOwnGPGKeys" , func ( t * testing . T ) {
2018-09-10 19:15:52 +03:00
testViewOwnGPGKeys ( t , tc . makeRequest , tc . token , tc . results [ 0 ] )
2017-09-05 16:45:18 +03:00
} )
t . Run ( "ViewGPGKeys" , func ( t * testing . T ) {
2018-09-10 19:15:52 +03:00
testViewGPGKeys ( t , tc . makeRequest , tc . token , tc . results [ 1 ] )
2017-09-05 16:45:18 +03:00
} )
t . Run ( "GetGPGKey" , func ( t * testing . T ) {
2018-09-10 19:15:52 +03:00
testGetGPGKey ( t , tc . makeRequest , tc . token , tc . results [ 2 ] )
2017-09-05 16:45:18 +03:00
} )
t . Run ( "DeleteGPGKey" , func ( t * testing . T ) {
2018-09-10 19:15:52 +03:00
testDeleteGPGKey ( t , tc . makeRequest , tc . token , tc . results [ 3 ] )
2017-09-05 16:45:18 +03:00
} )
t . Run ( "CreateInvalidGPGKey" , func ( t * testing . T ) {
2018-09-10 19:15:52 +03:00
testCreateInvalidGPGKey ( t , tc . makeRequest , tc . token , tc . results [ 4 ] )
2017-09-05 16:45:18 +03:00
} )
t . Run ( "CreateNoneRegistredEmailGPGKey" , func ( t * testing . T ) {
2018-09-10 19:15:52 +03:00
testCreateNoneRegistredEmailGPGKey ( t , tc . makeRequest , tc . token , tc . results [ 5 ] )
2017-09-05 16:45:18 +03:00
} )
t . Run ( "CreateValidGPGKey" , func ( t * testing . T ) {
2018-09-10 19:15:52 +03:00
testCreateValidGPGKey ( t , tc . makeRequest , tc . token , tc . results [ 6 ] )
2017-09-05 16:45:18 +03:00
} )
t . Run ( "CreateValidSecondaryEmailGPGKey" , func ( t * testing . T ) {
2018-09-10 19:15:52 +03:00
testCreateValidSecondaryEmailGPGKey ( t , tc . makeRequest , tc . token , tc . results [ 7 ] )
2017-09-05 16:45:18 +03:00
} )
} )
}
//Check state after basic add
t . Run ( "CheckState" , func ( t * testing . T ) {
var keys [ ] * api . GPGKey
2018-09-10 19:15:52 +03:00
req := NewRequest ( t , "GET" , "/api/v1/user/gpg_keys?token=" + token ) //GET all keys
2017-09-05 16:45:18 +03:00
resp := session . MakeRequest ( t , req , http . StatusOK )
DecodeJSON ( t , resp , & keys )
primaryKey1 := keys [ 0 ] //Primary key 1
assert . EqualValues ( t , "38EA3BCED732982C" , primaryKey1 . KeyID )
assert . EqualValues ( t , 1 , len ( primaryKey1 . Emails ) )
assert . EqualValues ( t , "user2@example.com" , primaryKey1 . Emails [ 0 ] . Email )
assert . EqualValues ( t , true , primaryKey1 . Emails [ 0 ] . Verified )
subKey := primaryKey1 . SubsKey [ 0 ] //Subkey of 38EA3BCED732982C
assert . EqualValues ( t , "70D7C694D17D03AD" , subKey . KeyID )
assert . EqualValues ( t , 0 , len ( subKey . Emails ) )
primaryKey2 := keys [ 1 ] //Primary key 2
assert . EqualValues ( t , "FABF39739FE1E927" , primaryKey2 . KeyID )
assert . EqualValues ( t , 1 , len ( primaryKey2 . Emails ) )
assert . EqualValues ( t , "user21@example.com" , primaryKey2 . Emails [ 0 ] . Email )
assert . EqualValues ( t , false , primaryKey2 . Emails [ 0 ] . Verified )
var key api . GPGKey
2018-09-10 19:15:52 +03:00
req = NewRequest ( t , "GET" , "/api/v1/user/gpg_keys/" + strconv . FormatInt ( primaryKey1 . ID , 10 ) + "?token=" + token ) //Primary key 1
2017-09-05 16:45:18 +03:00
resp = session . MakeRequest ( t , req , http . StatusOK )
DecodeJSON ( t , resp , & key )
assert . EqualValues ( t , "38EA3BCED732982C" , key . KeyID )
assert . EqualValues ( t , 1 , len ( key . Emails ) )
assert . EqualValues ( t , "user2@example.com" , key . Emails [ 0 ] . Email )
assert . EqualValues ( t , true , key . Emails [ 0 ] . Verified )
2018-09-10 19:15:52 +03:00
req = NewRequest ( t , "GET" , "/api/v1/user/gpg_keys/" + strconv . FormatInt ( subKey . ID , 10 ) + "?token=" + token ) //Subkey of 38EA3BCED732982C
2017-09-05 16:45:18 +03:00
resp = session . MakeRequest ( t , req , http . StatusOK )
DecodeJSON ( t , resp , & key )
assert . EqualValues ( t , "70D7C694D17D03AD" , key . KeyID )
assert . EqualValues ( t , 0 , len ( key . Emails ) )
2018-09-10 19:15:52 +03:00
req = NewRequest ( t , "GET" , "/api/v1/user/gpg_keys/" + strconv . FormatInt ( primaryKey2 . ID , 10 ) + "?token=" + token ) //Primary key 2
2017-09-05 16:45:18 +03:00
resp = session . MakeRequest ( t , req , http . StatusOK )
DecodeJSON ( t , resp , & key )
assert . EqualValues ( t , "FABF39739FE1E927" , key . KeyID )
assert . EqualValues ( t , 1 , len ( key . Emails ) )
assert . EqualValues ( t , "user21@example.com" , key . Emails [ 0 ] . Email )
assert . EqualValues ( t , false , key . Emails [ 0 ] . Verified )
} )
//Check state after basic add
t . Run ( "CheckCommits" , func ( t * testing . T ) {
t . Run ( "NotSigned" , func ( t * testing . T ) {
var branch api . Branch
2018-09-10 19:15:52 +03:00
req := NewRequest ( t , "GET" , "/api/v1/repos/user2/repo16/branches/not-signed?token=" + token )
2017-09-05 16:45:18 +03:00
resp := session . MakeRequest ( t , req , http . StatusOK )
DecodeJSON ( t , resp , & branch )
assert . EqualValues ( t , false , branch . Commit . Verification . Verified )
} )
t . Run ( "SignedWithNotValidatedEmail" , func ( t * testing . T ) {
var branch api . Branch
2018-09-10 19:15:52 +03:00
req := NewRequest ( t , "GET" , "/api/v1/repos/user2/repo16/branches/good-sign-not-yet-validated?token=" + token )
2017-09-05 16:45:18 +03:00
resp := session . MakeRequest ( t , req , http . StatusOK )
DecodeJSON ( t , resp , & branch )
assert . EqualValues ( t , false , branch . Commit . Verification . Verified )
} )
t . Run ( "SignedWithValidEmail" , func ( t * testing . T ) {
var branch api . Branch
2018-09-10 19:15:52 +03:00
req := NewRequest ( t , "GET" , "/api/v1/repos/user2/repo16/branches/good-sign?token=" + token )
2017-09-05 16:45:18 +03:00
resp := session . MakeRequest ( t , req , http . StatusOK )
DecodeJSON ( t , resp , & branch )
assert . EqualValues ( t , true , branch . Commit . Verification . Verified )
} )
} )
}
2018-09-10 19:15:52 +03:00
func testViewOwnGPGKeys ( t * testing . T , makeRequest makeRequestFunc , token string , expected int ) {
req := NewRequest ( t , "GET" , "/api/v1/user/gpg_keys?token=" + token )
2017-12-04 01:46:01 +03:00
makeRequest ( t , req , expected )
2017-09-05 16:45:18 +03:00
}
2018-09-10 19:15:52 +03:00
func testViewGPGKeys ( t * testing . T , makeRequest makeRequestFunc , token string , expected int ) {
req := NewRequest ( t , "GET" , "/api/v1/users/user2/gpg_keys?token=" + token )
2017-12-04 01:46:01 +03:00
makeRequest ( t , req , expected )
2017-09-05 16:45:18 +03:00
}
2018-09-10 19:15:52 +03:00
func testGetGPGKey ( t * testing . T , makeRequest makeRequestFunc , token string , expected int ) {
req := NewRequest ( t , "GET" , "/api/v1/user/gpg_keys/1?token=" + token )
2017-12-04 01:46:01 +03:00
makeRequest ( t , req , expected )
2017-09-05 16:45:18 +03:00
}
2018-09-10 19:15:52 +03:00
func testDeleteGPGKey ( t * testing . T , makeRequest makeRequestFunc , token string , expected int ) {
req := NewRequest ( t , "DELETE" , "/api/v1/user/gpg_keys/1?token=" + token )
2017-12-04 01:46:01 +03:00
makeRequest ( t , req , expected )
2017-09-05 16:45:18 +03:00
}
2018-09-10 19:15:52 +03:00
func testCreateGPGKey ( t * testing . T , makeRequest makeRequestFunc , token string , expected int , publicKey string ) {
req := NewRequestWithJSON ( t , "POST" , "/api/v1/user/gpg_keys?token=" + token , api . CreateGPGKeyOption {
2017-09-05 16:45:18 +03:00
ArmoredKey : publicKey ,
} )
2017-12-04 01:46:01 +03:00
makeRequest ( t , req , expected )
2017-09-05 16:45:18 +03:00
}
2018-09-10 19:15:52 +03:00
func testCreateInvalidGPGKey ( t * testing . T , makeRequest makeRequestFunc , token string , expected int ) {
testCreateGPGKey ( t , makeRequest , token , expected , "invalid_key" )
2017-09-05 16:45:18 +03:00
}
2018-09-10 19:15:52 +03:00
func testCreateNoneRegistredEmailGPGKey ( t * testing . T , makeRequest makeRequestFunc , token string , expected int ) {
testCreateGPGKey ( t , makeRequest , token , expected , ` -- -- - BEGIN PGP PUBLIC KEY BLOCK -- -- -
2017-09-05 16:45:18 +03:00
mQENBFmGUygBCACjCNbKvMGgp0fd5vyFW9olE1CLCSyyF9gQN2hSuzmZLuAZF2Kh
dCMCG2T1UwzUB / yWUFWJ2BtCwSjuaRv + cGohqEy6bhEBV90peGA33lHfjx7wP25O
7 moAphDOTZtDj1AZfCh / PTcJut8Lc0eRDMhNyp / bYtO7SHNT1Hr6rrCV / xEtSAvR
3 b148 / tmIBiSadaLwc558KU3ucjnW5RVGins3AjBZ + TuT4XXVH / oeLSeXPSJ5rt1
rHwaseslMqZ4AbvwFLx5qn1OC9rEQv / F548QsA8m0IntLjoPon + 6 wcubA9Gra21c
Fp6aRYl9x7fiqXDLg8i3s2nKdV7 + e6as6Tp9ABEBAAG0FG5vdGtub3duQGV4YW1w
bGUuY29tiQEcBBABAgAGBQJZhlMoAAoJEC8 + pvYULDtte / wH / 2 JNrhmHwDY + hMj0
batIK4HICnkKxjIgbha80P2Ao08NkzSge58fsxiKDFYAQjHui + ZAw4dq79Ax9AOO
Iv2GS9 + DUfWhrb6RF + vNuJldFzcI0rTW / z2q + XGKrUCwN3khJY5XngHfQQrdBtMK
qsoUXz / 5 B8g422RTbo / SdPsyYAV6HeLLeV3rdgjI1fpaW0seZKHeTXQb / HvNeuPg
qz + XV1g6Gdqa1RjDOaX7A8elVKxrYq3LBtc93FW + grBde8n7JL0zPM3DY + vJ0IJZ
INx / MmBfmtCq05FqNclvU + sj2R3N1JJOtBOjZrJHQbJhzoILou8AkxeX1A + q9OAz
1 geiY5E =
= TkP3
-- -- - END PGP PUBLIC KEY BLOCK -- -- - ` )
}
2018-09-10 19:15:52 +03:00
func testCreateValidGPGKey ( t * testing . T , makeRequest makeRequestFunc , token string , expected int ) {
2017-09-05 16:45:18 +03:00
//User2 <user2@example.com> //primary & activated
2018-09-10 19:15:52 +03:00
testCreateGPGKey ( t , makeRequest , token , expected , ` -- -- - BEGIN PGP PUBLIC KEY BLOCK -- -- -
2017-09-05 16:45:18 +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1GNFocl6tLUvJdEqs + VcJGGANMpmzE4WLa8KhSYnxipwuQ62JBy9R + cHyKT
OARk8znRqSu5bT3LtlrZ / HXu + 6 Oy4 + 2 uCdNzZIh5J5tPS7CPA6ptl88iGVBte / CJ
7 cjgJWSQqeYp2Y5QvsWAivkQ4Ww9plHbbwV0A2eaHsjjWzlUl3HoJ / snMOhBABEB
AAGJATwEGAEIACYWIQRXgbSh0TtGbgRd7XI46jvO1zKYLAUCWYZWwwIbDAUJA8Jn
AAAKCRA46jvO1zKYLBwLCACQOpeRVrwIKVaWcPMYjVHHJsGscaLKpgpARAUgbiG6
Cbc2WI8Sm3fRwrY0VAfN + u9QwrtvxANcyB3vTgTzw7FimfhOimxiTSO8HQCfjDZF
Xly8rq + Fua7 + ClWUpy21IekW41VvZYjH2sL6EVP + UcEOaGAyN53XfhaRVZPhNtZN
NKAE9N5EG3rbsZ33LzJj40rEKlzFSseAAPft8qA3IXjzFBx + PQXHMpNCagL79he6
lqockTJ + oPmta4CF / J0U5LUr1tOZXheL3TP6m8d08gDrtn0YuGOPk87i9sJz + jR9
uy6MA3VSB99SK9ducGmE1Jv8mcziREroz2TEGr0zPs6h
= J59D
-- -- - END PGP PUBLIC KEY BLOCK -- -- - ` )
}
2018-09-10 19:15:52 +03:00
func testCreateValidSecondaryEmailGPGKey ( t * testing . T , makeRequest makeRequestFunc , token string , expected int ) {
2017-09-05 16:45:18 +03:00
//User2 <user21@example.com> //secondary and not activated
2018-09-10 19:15:52 +03:00
testCreateGPGKey ( t , makeRequest , token , expected , ` -- -- - BEGIN PGP PUBLIC KEY BLOCK -- -- -
2017-09-05 16:45:18 +03:00
mQENBFmGWN4BCAC18V4tVGO65VLCV7p14FuXJlUtZ5CuYMvgEkcOqrvRaBSW9ao4
PGESOhJpfWpnW3QgJniYndLzPpsmdHEclEER6aZjiNgReWPOjHD5tykWocZAJqXD
eY1ym59gvVMLcfbV2yQsyR2hbJlc + dJsl16tigSEe3nwxZSw2IsW92pgEzT9JNU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