File: nosql_test.go

package info (click to toggle)
golang-github-smallstep-certificates 0.29.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,720 kB
  • sloc: sh: 385; makefile: 129
file content (139 lines) | stat: -rw-r--r-- 3,461 bytes parent folder | download | duplicates (4)
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package nosql

import (
	"context"
	"testing"

	"github.com/pkg/errors"
	"github.com/smallstep/assert"
	"github.com/smallstep/certificates/db"
	"github.com/smallstep/nosql"
)

func TestNew(t *testing.T) {
	type test struct {
		db  nosql.DB
		err error
	}
	var tests = map[string]test{
		"fail/db.CreateTable-error": {
			db: &db.MockNoSQLDB{
				MCreateTable: func(bucket []byte) error {
					assert.Equals(t, string(bucket), string(accountTable))
					return errors.New("force")
				},
			},
			err: errors.Errorf("error creating table %s: force", string(accountTable)),
		},
		"ok": {
			db: &db.MockNoSQLDB{
				MCreateTable: func(bucket []byte) error {
					return nil
				},
			},
		},
	}
	for name, tc := range tests {
		t.Run(name, func(t *testing.T) {
			if _, err := New(tc.db); err != nil {
				if assert.NotNil(t, tc.err) {
					assert.HasPrefix(t, err.Error(), tc.err.Error())
				}
			} else {
				assert.Nil(t, tc.err)
			}
		})
	}
}

type errorThrower string

func (et errorThrower) MarshalJSON() ([]byte, error) {
	return nil, errors.New("force")
}

func TestDB_save(t *testing.T) {
	type test struct {
		db  nosql.DB
		nu  interface{}
		old interface{}
		err error
	}
	var tests = map[string]test{
		"fail/error-marshaling-new": {
			nu:  errorThrower("foo"),
			err: errors.New("error marshaling acme type: challenge"),
		},
		"fail/error-marshaling-old": {
			nu:  "new",
			old: errorThrower("foo"),
			err: errors.New("error marshaling acme type: challenge"),
		},
		"fail/db.CmpAndSwap-error": {
			nu:  "new",
			old: "old",
			db: &db.MockNoSQLDB{
				MCmpAndSwap: func(bucket, key, old, nu []byte) ([]byte, bool, error) {
					assert.Equals(t, bucket, challengeTable)
					assert.Equals(t, string(key), "id")
					assert.Equals(t, string(old), "\"old\"")
					assert.Equals(t, string(nu), "\"new\"")
					return nil, false, errors.New("force")
				},
			},
			err: errors.New("error saving acme challenge: force"),
		},
		"fail/db.CmpAndSwap-false-marshaling-old": {
			nu:  "new",
			old: "old",
			db: &db.MockNoSQLDB{
				MCmpAndSwap: func(bucket, key, old, nu []byte) ([]byte, bool, error) {
					assert.Equals(t, bucket, challengeTable)
					assert.Equals(t, string(key), "id")
					assert.Equals(t, string(old), "\"old\"")
					assert.Equals(t, string(nu), "\"new\"")
					return nil, false, nil
				},
			},
			err: errors.New("error saving acme challenge; changed since last read"),
		},
		"ok": {
			nu:  "new",
			old: "old",
			db: &db.MockNoSQLDB{
				MCmpAndSwap: func(bucket, key, old, nu []byte) ([]byte, bool, error) {
					assert.Equals(t, bucket, challengeTable)
					assert.Equals(t, string(key), "id")
					assert.Equals(t, string(old), "\"old\"")
					assert.Equals(t, string(nu), "\"new\"")
					return nu, true, nil
				},
			},
		},
		"ok/nils": {
			nu:  nil,
			old: nil,
			db: &db.MockNoSQLDB{
				MCmpAndSwap: func(bucket, key, old, nu []byte) ([]byte, bool, error) {
					assert.Equals(t, bucket, challengeTable)
					assert.Equals(t, string(key), "id")
					assert.Equals(t, old, nil)
					assert.Equals(t, nu, nil)
					return nu, true, nil
				},
			},
		},
	}
	for name, tc := range tests {
		t.Run(name, func(t *testing.T) {
			d := &DB{db: tc.db}
			if err := d.save(context.Background(), "id", tc.nu, tc.old, "challenge", challengeTable); err != nil {
				if assert.NotNil(t, tc.err) {
					assert.HasPrefix(t, err.Error(), tc.err.Error())
				}
			} else {
				assert.Nil(t, tc.err)
			}
		})
	}
}