1package identity
2
3import (
4 "encoding/json"
5 "io"
6 "os"
7 "testing"
8
9 "github.com/stretchr/testify/assert"
10)
11
12// Tests parsing and normalizing handles from DID documents
13func TestHandleExtraction(t *testing.T) {
14 assert := assert.New(t)
15 f, err := os.Open("testdata/did_plc_doc.json")
16 if err != nil {
17 t.Fatal(err)
18 }
19 defer f.Close()
20
21 docBytes, err := io.ReadAll(f)
22 if err != nil {
23 t.Fatal(err)
24 }
25
26 var doc DIDDocument
27 err = json.Unmarshal(docBytes, &doc)
28 assert.NoError(err)
29
30 {
31 ident := ParseIdentity(&doc)
32 hdl, err := ident.DeclaredHandle()
33 assert.NoError(err)
34 assert.Equal("atproto.com", hdl.String())
35 }
36
37 {
38 doc.AlsoKnownAs = []string{
39 "at://BLAH.com",
40 "at://other.org",
41 }
42 ident := ParseIdentity(&doc)
43 hdl, err := ident.DeclaredHandle()
44 assert.NoError(err)
45 assert.Equal("blah.com", hdl.String())
46 }
47
48 {
49 doc.AlsoKnownAs = []string{
50 "https://http.example.com",
51 "at://under_example_com",
52 "at://correct.EXAMPLE.com",
53 "at://other.example.com",
54 }
55 ident := ParseIdentity(&doc)
56 hdl, err := ident.DeclaredHandle()
57 assert.NoError(err)
58 assert.Equal("correct.example.com", hdl.String())
59 }
60
61 {
62 doc.AlsoKnownAs = []string{
63 "https://http.example.com",
64 }
65 ident := ParseIdentity(&doc)
66 _, err := ident.DeclaredHandle()
67 assert.Error(err)
68 assert.Equal("handle.invalid", ident.Handle.String())
69 }
70}