2018-10-05 21:48:36 +00:00
package authority
import (
"net/http"
"testing"
"github.com/pkg/errors"
"github.com/smallstep/assert"
)
func TestRoot ( t * testing . T ) {
a := testAuthority ( t )
a . certificates . Store ( "invaliddata" , "a string" ) // invalid cert for testing
tests := map [ string ] struct {
sum string
err * apiError
} {
"not-found" : { "foo" , & apiError { errors . New ( "certificate with fingerprint foo was not found" ) , http . StatusNotFound , context { } } } ,
2019-01-05 01:51:32 +00:00
"invalid-stored-certificate" : { "invaliddata" , & apiError { errors . New ( "stored value is not a *x509.Certificate" ) , http . StatusInternalServerError , context { } } } ,
2018-10-05 21:48:36 +00:00
"success" : { "189f573cfa159251e445530847ef80b1b62a3a380ee670dcb49e33ed34da0616" , nil } ,
}
for name , tc := range tests {
t . Run ( name , func ( t * testing . T ) {
crt , err := a . Root ( tc . sum )
if err != nil {
if assert . NotNil ( t , tc . err ) {
switch v := err . ( type ) {
case * apiError :
assert . HasPrefix ( t , v . err . Error ( ) , tc . err . Error ( ) )
assert . Equals ( t , v . code , tc . err . code )
assert . Equals ( t , v . context , tc . err . context )
default :
t . Errorf ( "unexpected error type: %T" , v )
}
}
} else {
if assert . Nil ( t , tc . err ) {
2019-01-07 23:30:28 +00:00
assert . Equals ( t , crt , a . rootX509Certs [ 0 ] )
2018-10-05 21:48:36 +00:00
}
}
} )
}
}