-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmiddleware_test.go
202 lines (182 loc) · 4.38 KB
/
middleware_test.go
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
package auth
import (
"crypto"
"crypto/rand"
"crypto/rsa"
"crypto/x509"
"encoding/pem"
"fmt"
"github.com/go-ap/client"
"github.com/go-ap/jsonld"
"net"
"net/http"
"net/http/httptest"
"net/url"
"path/filepath"
"reflect"
"testing"
"git.sr.ht/~mariusor/lw"
vocab "github.com/go-ap/activitypub"
"github.com/openshift/osin"
)
func TestKeyLoader_GetKey(t *testing.T) {
t.Skipf("TODO")
}
func TestOauthLoader_Verify(t *testing.T) {
t.Skipf("TODO")
}
func TestActorContext(t *testing.T) {
t.Skipf("TODO")
}
func TestServer_LoadActorFromRequest(t *testing.T) {
type fields struct {
Server *osin.Server
localURLs vocab.IRIs
account Account
cl Client
st readStore
l lw.Logger
}
tests := []struct {
name string
fields fields
header string
want vocab.Actor
wantErr bool
}{
{
name: "empty",
fields: fields{},
header: "",
want: AnonymousActor,
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
s := &Server{
Server: tt.fields.Server,
localURLs: tt.fields.localURLs,
account: tt.fields.account,
cl: tt.fields.cl,
l: tt.fields.l,
}
r := http.Request{Header: http.Header{}, URL: new(url.URL)}
r.Header.Set("Authorization", tt.header)
got, err := s.LoadActorFromRequest(&r)
if (err != nil) != tt.wantErr {
t.Errorf("LoadActorFromRequest() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("LoadActorFromRequest() got = %v, want %v", got, tt.want)
}
})
}
}
var ll = lw.Dev()
var cl = client.New(
client.WithLogger(ll.WithContext(lw.Ctx{"log": "client"})),
client.SkipTLSValidation(true),
)
var logFn LoggerFn = func(ctx lw.Ctx, msg string, p ...interface{}) {
ll.WithContext(ctx).Debugf(msg, p...)
}
func isNotLocal(_ vocab.IRI) bool {
return false
}
var prv, _ = rsa.GenerateKey(rand.Reader, 512)
func pemEncodePublicKey(prvKey *rsa.PrivateKey) string {
pubKey := prvKey.PublicKey
pubEnc, err := x509.MarshalPKIXPublicKey(&pubKey)
if err != nil {
panic(err)
}
p := pem.Block{
Type: "PUBLIC KEY",
Bytes: pubEnc,
}
return string(pem.EncodeToMemory(&p))
}
func mockActor(base string) *vocab.Actor {
return &vocab.Actor{
ID: vocab.IRI(base + "/jdoe"),
Type: vocab.PersonType,
PublicKey: vocab.PublicKey{
ID: vocab.IRI(base + "/jdoe#main"),
Owner: vocab.IRI(base + "/jdoe"),
PublicKeyPem: pemEncodePublicKey(prv),
},
}
}
func mockKeyAndActorHandler(base string) http.Handler {
cnt := 0
actor := mockActor(base)
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
var payload []byte
status := http.StatusNotModified
if cnt == 0 {
payload, _ = jsonld.Marshal(actor)
if filepath.Base(r.URL.Path) == "key" {
actor.PublicKey.ID = vocab.IRI(base + "/jdoe/key")
payload, _ = jsonld.Marshal(actor.PublicKey)
}
status = http.StatusOK
w.Header().Set("Cache-Control", "public")
}
cnt++
w.WriteHeader(status)
_, _ = w.Write(payload)
})
}
var srv, _ = testServerWithURL(mockKeyAndActorHandler)
func testServerWithURL(handler func(string) http.Handler) (*httptest.Server, error) {
l, _ := net.Listen("tcp", "127.0.0.1:0")
ts := httptest.NewUnstartedServer(nil)
ts.Listener = l
ts.Config.Handler = handler(fmt.Sprintf("http://%s", ts.Listener.Addr().String()))
ts.Start()
return ts, nil
}
func Test_keyLoader_GetKey(t *testing.T) {
loadActorFromKeyFn := (actorResolver{c: cl, l: logFn, iriIsLocal: isNotLocal}).LoadActorFromKeyIRI
tests := []struct {
name string
arg string
want crypto.PublicKey
wantErr bool
}{
//{
// name: "empty",
// wantErr: true,
//},
//{
// name: "remote key IRI as separate resource",
// arg: srv.URL + "/jdoe/key",
// want: prv.Public(),
// wantErr: false,
//},
{
name: "remote key IRI as actor resource",
arg: srv.URL + "/jdoe#main",
want: prv.Public(),
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
k := &keyLoader{
loadActorFromKeyFn: loadActorFromKeyFn,
logFn: logFn,
}
got, err := k.GetKey(tt.arg)
if (err != nil) != tt.wantErr {
t.Errorf("GetKey() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("GetKey() got = %v, want %v", got, tt.want)
}
})
}
}