2021-12-10 11:14:24 +03:00
// Copyright 2021 The Gitea Authors. All rights reserved.
2022-11-27 21:20:29 +03:00
// SPDX-License-Identifier: MIT
2021-12-10 11:14:24 +03:00
package asymkey
import (
"testing"
asymkey_model "code.gitea.io/gitea/models/asymkey"
2022-01-02 16:12:35 +03:00
"code.gitea.io/gitea/models/auth"
2023-09-25 16:17:37 +03:00
"code.gitea.io/gitea/models/db"
2021-12-10 11:14:24 +03:00
"code.gitea.io/gitea/models/unittest"
user_model "code.gitea.io/gitea/models/user"
2021-12-15 08:39:34 +03:00
2021-12-10 11:14:24 +03:00
"github.com/stretchr/testify/assert"
)
func TestAddLdapSSHPublicKeys ( t * testing . T ) {
assert . NoError ( t , unittest . PrepareTestDatabase ( ) )
2022-08-16 05:22:25 +03:00
user := unittest . AssertExistsAndLoadBean ( t , & user_model . User { ID : 2 } )
2022-01-02 16:12:35 +03:00
s := & auth . Source { ID : 1 }
2021-12-10 11:14:24 +03:00
testCases := [ ] struct {
keyString string
number int
keyContents [ ] string
} {
{
keyString : "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQC4cn+iXnA4KvcQYSV88vGn0Yi91vG47t1P7okprVmhNTkipNRIHWr6WdCO4VDr/cvsRkuVJAsLO2enwjGWWueOO6BodiBgyAOZ/5t5nJNMCNuLGT5UIo/RI1b0WRQwxEZTRjt6mFNw6lH14wRd8ulsr9toSWBPMOGWoYs1PDeDL0JuTjL+tr1SZi/EyxCngpYszKdXllJEHyI79KQgeD0Vt3pTrkbNVTOEcCNqZePSVmUH8X8Vhugz3bnE0/iE9Pb5fkWO9c4AnM1FgI/8Bvp27Fw2ShryIXuR6kKvUqhVMTuOSDHwu6A8jLE5Owt3GAYugDpDYuwTVNGrHLXKpPzrGGPE/jPmaLCMZcsdkec95dYeU3zKODEm8UQZFhmJmDeWVJ36nGrGZHL4J5aTTaeFUJmmXDaJYiJ+K2/ioKgXqnXvltu0A9R8/LGy4nrTJRr4JMLuJFoUXvGm1gXQ70w2LS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FhmJmDeWVJ36nGrGZHL4J5aTTaeFUJmmXDaJYiJ + K2 / ioKgXqnXvltu0A9R8 / LGy4nrTJRr4JMLuJFoUXvGm1gXQ70w2LSpk6yl71RNC0hCtsBe8BP8IhYCM0EP5jh7eCMQZNvM = nocomment
ssh - dss AAAAB3NzaC1kc3MAAACBAOChCC7lf6Uo9n7BmZ6M8St19PZf4Tn59NriyboW2x / DZuYAz3ibZ2OkQ3S0SqDIa0HXSEJ1zaExQdmbO + Ux / wsytWZmCczWOVsaszBZSl90q8UnWlSH6P + / YA + RWJm5SFtuV9PtGIhyZgoNuz5kBQ7K139wuQsecdKktISwTakzAAAAFQCzKsO2JhNKlL + wwwLGOcLffoAmkwAAAIBpK7 / 3 xvduajLBD / 9 vASqBQIHrgK2J + wiQnIb / Wzy0UsVmvfn8A + udRbBo + csM8xrSnlnlJnjkJS3qiM5g + eTwsLIV1IdKPEwmwB + VcP53Cw6lSyWyJcvhFb0N6s08NZysLzvj0N + ZC / FnhKTLzIyMtkHf / IrPCwlM + pV / M / 96 YgAAAIEAqQcGn9CKgzgPaguIZooTAOQdvBLMI5y0bQjOW6734XOpqQGf / Kra90wpoasLKZjSYKNPjE + FRUOrStLrxcNs4BeVKhy2PYTRnybfYVk1 / dmKgH6P1YSRONsGKvTsH6c5IyCRG0ncCgYeF8tXppyd642982daopE7zQ / NPAnJfag = nocomment ` ,
number : 2 ,
keyContents : [ ] string {
"ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQC4cn+iXnA4KvcQYSV88vGn0Yi91vG47t1P7okprVmhNTkipNRIHWr6WdCO4VDr/cvsRkuVJAsLO2enwjGWWueOO6BodiBgyAOZ/5t5nJNMCNuLGT5UIo/RI1b0WRQwxEZTRjt6mFNw6lH14wRd8ulsr9toSWBPMOGWoYs1PDeDL0JuTjL+tr1SZi/EyxCngpYszKdXllJEHyI79KQgeD0Vt3pTrkbNVTOEcCNqZePSVmUH8X8Vhugz3bnE0/iE9Pb5fkWO9c4AnM1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r1SZi / EyxCngpYszKdXllJEHyI79KQgeD0Vt3pTrkbNVTOEcCNqZePSVmUH8X8Vhugz3bnE0 / iE9Pb5fkWO9c4AnM1FgI / 8 Bvp27Fw2ShryIXuR6kKvUqhVMTuOSDHwu6A8jLE5Owt3GAYugDpDYuwTVNGrHLXKpPzrGGPE / jPmaLCMZcsdkec95dYeU3zKODEm8UQZFhmJmDeWVJ36nGrGZHL4J5aTTaeFUJmmXDaJYiJ + K2 / ioKgXqnXvltu0A9R8 / LGy4nrTJRr4JMLuJFoUXvGm1gXQ70w2LSpk6yl71RNC0hCtsBe8BP8IhYCM0EP5jh7eCMQZNvM = nocomment
# comment asmdna , ndp
ssh - dss AAAAB3NzaC1kc3MAAACBAOChCC7lf6Uo9n7BmZ6M8St19PZf4Tn59NriyboW2x / DZuYAz3ibZ2OkQ3S0SqDIa0HXSEJ1zaExQdmbO + Ux / wsytWZmCczWOVsaszBZSl90q8UnWlSH6P + / YA + RWJm5SFtuV9PtGIhyZgoNuz5kBQ7K139wuQsecdKktISwTakzAAAAFQCzKsO2JhNKlL + wwwLGOcLffoAmkwAAAIBpK7 / 3 xvduajLBD / 9 vASqBQIHrgK2J + wiQnIb / Wzy0UsVmvfn8A + udRbBo + csM8xrSnlnlJnjkJS3qiM5g + eTwsLIV1IdKPEwmwB + VcP53Cw6lSyWyJcvhFb0N6s08NZysLzvj0N + ZC / FnhKTLzIyMtkHf / IrPCwlM + pV / M / 96 YgAAAIEAqQcGn9CKgzgPaguIZooTAOQdvBLMI5y0bQjOW6734XOpqQGf / Kra90wpoasLKZjSYKNPjE + FRUOrStLrxcNs4BeVKhy2PYTRnybfYVk1 / dmKgH6P1YSRONsGKvTsH6c5IyCRG0ncCgYeF8tXppyd642982daopE7zQ / NPAnJfag = nocomment ` ,
number : 2 ,
keyContents : [ ] string {
"ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQC4cn+iXnA4KvcQYSV88vGn0Yi91vG47t1P7okprVmhNTkipNRIHWr6W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` ssh - rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQC4cn + iXnA4KvcQYSV88vGn0Yi91vG47t1P7okprVmhNTkipNRIHWr6WdCO4VDr / cvsRkuVJAsLO2enwjGWWueOO6BodiBgyAOZ / 5 t5nJNMCNuLGT5UIo / RI1b0WRQwxEZTRjt6mFNw6lH14wRd8ulsr9toSWBPMOGWoYs1PDeDL0JuTjL + tr1SZi / EyxCngpYszKdXllJEHyI79KQgeD0Vt3pTrkbNVTOEcCNqZePSVmUH8X8Vhugz3bnE0 / iE9Pb5fkWO9c4AnM1FgI / 8 Bvp27Fw2ShryIXuR6kKvUqhVMTuOSDHwu6A8jLE5Owt3GAYugDpDYuwTVNGrHLXKpPzrGGPE / jPmaLCMZcsdkec95dYeU3zKODEm8UQZFhmJmDeWVJ36nGrGZHL4J5aTTaeFUJmmXDaJYiJ + K2 / ioKgXqnXvltu0A9R8 / LGy4nrTJRr4JMLuJFoUXvGm1gXQ70w2LSpk6yl71RNC0hCtsBe8BP8IhYCM0EP5jh7eCMQZNvM = nocomment
382488320 jasdj1lasmva / vasodifipi4193 - fksma . cm
ssh - dss AAAAB3NzaC1kc3MAAACBAOChCC7lf6Uo9n7BmZ6M8St19PZf4Tn59NriyboW2x / DZuYAz3ibZ2OkQ3S0SqDIa0HXSEJ1zaExQdmbO + Ux / wsytWZmCczWOVsaszBZSl90q8UnWlSH6P + / YA + RWJm5SFtuV9PtGIhyZgoNuz5kBQ7K139wuQsecdKktISwTakzAAAAFQCzKsO2JhNKlL + wwwLGOcLffoAmkwAAAIBpK7 / 3 xvduajLBD / 9 vASqBQIHrgK2J + wiQnIb / Wzy0UsVmvfn8A + udRbBo + csM8xrSnlnlJnjkJS3qiM5g + eTwsLIV1IdKPEwmwB + VcP53Cw6lSyWyJcvhFb0N6s08NZysLzvj0N + ZC / FnhKTLzIyMtkHf / IrPCwlM + pV / M / 96 YgAAAIEAqQcGn9CKgzgPaguIZooTAOQdvBLMI5y0bQjOW6734XOpqQGf /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NZysLzvj0N+ZC/FnhKTLzIyMtkHf/IrPCwlM+pV/M/96YgAAAIEAqQcGn9CKgzgPaguIZooTAOQdvBLMI5y0bQjOW6734XOpqQGf/Kra90wpoasLKZjSYKNPjE+FRUOrStLrxcNs4BeVKhy2PYTRnybfYVk1/dmKgH6P1YSRONsGKvTsH6c5IyCRG0ncCgYeF8tXppyd642982daopE7zQ/NPAnJfag=" ,
} ,
} ,
}
for i , kase := range testCases {
s . ID = int64 ( i ) + 20
2023-10-11 07:24:07 +03:00
asymkey_model . AddPublicKeysBySource ( db . DefaultContext , user , s , [ ] string { kase . keyString } )
2023-11-24 06:49:41 +03:00
keys , err := db . Find [ asymkey_model . PublicKey ] ( db . DefaultContext , asymkey_model . FindPublicKeyOptions {
OwnerID : user . ID ,
LoginSourceID : s . ID ,
} )
2021-12-10 11:14:24 +03:00
assert . NoError ( t , err )
if err != nil {
continue
}
assert . Len ( t , keys , kase . number )
for _ , key := range keys {
assert . Contains ( t , kase . keyContents , key . Content )
}
for _ , key := range keys {
2023-09-25 16:17:37 +03:00
DeletePublicKey ( db . DefaultContext , user , key . ID )
2021-12-10 11:14:24 +03:00
}
}
}