forked from TrueCloudLab/certificates
Fix test mocking for CreateCertificate
This commit is contained in:
parent
2b15230aa4
commit
97165f1844
2 changed files with 30 additions and 11 deletions
|
@ -182,6 +182,7 @@ func (m *MockDB) GetCertificate(ctx context.Context, id string) (*Certificate, e
|
|||
return m.MockRet1.(*Certificate), m.MockError
|
||||
}
|
||||
|
||||
// GetCertificateBySerial mock
|
||||
func (m *MockDB) GetCertificateBySerial(ctx context.Context, serial string) (*Certificate, error) {
|
||||
if m.MockGetCertificateBySerial != nil {
|
||||
return m.MockGetCertificateBySerial(ctx, serial)
|
||||
|
|
|
@ -31,6 +31,7 @@ func TestDB_CreateCertificate(t *testing.T) {
|
|||
err error
|
||||
_id *string
|
||||
}
|
||||
countOfCmpAndSwapCalls := 0
|
||||
var tests = map[string]func(t *testing.T) test{
|
||||
"fail/cmpAndSwap-error": func(t *testing.T) test {
|
||||
cert := &acme.Certificate{
|
||||
|
@ -75,6 +76,7 @@ func TestDB_CreateCertificate(t *testing.T) {
|
|||
return test{
|
||||
db: &db.MockNoSQLDB{
|
||||
MCmpAndSwap: func(bucket, key, old, nu []byte) ([]byte, bool, error) {
|
||||
if countOfCmpAndSwapCalls == 0 {
|
||||
*idPtr = string(key)
|
||||
assert.Equals(t, bucket, certTable)
|
||||
assert.Equals(t, key, []byte(cert.ID))
|
||||
|
@ -87,6 +89,22 @@ func TestDB_CreateCertificate(t *testing.T) {
|
|||
assert.Equals(t, dbc.AccountID, cert.AccountID)
|
||||
assert.True(t, clock.Now().Add(-time.Minute).Before(dbc.CreatedAt))
|
||||
assert.True(t, clock.Now().Add(time.Minute).After(dbc.CreatedAt))
|
||||
}
|
||||
if countOfCmpAndSwapCalls == 1 {
|
||||
assert.Equals(t, bucket, certBySerialTable)
|
||||
assert.Equals(t, key, []byte(cert.Leaf.SerialNumber.String()))
|
||||
assert.Equals(t, old, nil)
|
||||
|
||||
dbs := new(dbSerial)
|
||||
assert.FatalError(t, json.Unmarshal(nu, dbs))
|
||||
assert.Equals(t, dbs.Serial, string(key))
|
||||
assert.Equals(t, dbs.CertificateID, cert.ID)
|
||||
|
||||
*idPtr = cert.ID
|
||||
}
|
||||
|
||||
countOfCmpAndSwapCalls += 1
|
||||
|
||||
return nil, true, nil
|
||||
},
|
||||
},
|
||||
|
|
Loading…
Reference in a new issue