File: errors_test.go

package info (click to toggle)
golang-github-smallstep-certificates 0.28.4-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,684 kB
  • sloc: sh: 367; makefile: 129
file content (54 lines) | stat: -rw-r--r-- 1,625 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package acme

import (
	"encoding/json"
	"testing"

	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"
)

func mustJSON(t *testing.T, m map[string]interface{}) string {
	t.Helper()

	b, err := json.Marshal(m)
	require.NoError(t, err)

	return string(b)
}

func TestError_WithAdditionalErrorDetail(t *testing.T) {
	internalJSON := mustJSON(t, map[string]interface{}{
		"detail": "The server experienced an internal error",
		"type":   "urn:ietf:params:acme:error:serverInternal",
	})
	malformedErr := NewError(ErrorMalformedType, "malformed error") // will result in Err == nil behavior
	malformedJSON := mustJSON(t, map[string]interface{}{
		"detail": "The request message was malformed",
		"type":   "urn:ietf:params:acme:error:malformed",
	})
	withDetailJSON := mustJSON(t, map[string]interface{}{
		"detail": "Attestation statement cannot be verified: invalid property",
		"type":   "urn:ietf:params:acme:error:badAttestationStatement",
	})
	tests := []struct {
		name string
		err  *Error
		want string
	}{
		{"internal", NewDetailedError(ErrorServerInternalType, ""), internalJSON},
		{"nil err", malformedErr, malformedJSON},
		{"detailed", NewDetailedError(ErrorBadAttestationStatementType, "invalid property"), withDetailJSON},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			b, err := json.Marshal(tt.err)
			require.NoError(t, err)

			// tests if the additional error detail is included in the JSON representation
			// of the ACME error. This is what is returned to ACME clients and being logged
			// by the CA.
			assert.JSONEq(t, tt.want, string(b))
		})
	}
}