-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcoverage_misc_test.go
More file actions
106 lines (90 loc) · 3.59 KB
/
Copy pathcoverage_misc_test.go
File metadata and controls
106 lines (90 loc) · 3.59 KB
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
package botapi
import (
"context"
"encoding/base64"
"testing"
"github.com/gotd/td/fileid"
"github.com/gotd/td/tg"
)
// TestSetPassportDataErrorsAllVariants drives SetPassportDataErrors with one of
// every PassportElementError variant, covering each variant's toTg plus the
// nil-skip path of the dispatch loop.
func TestSetPassportDataErrorsAllVariants(t *testing.T) {
inv := newMockInvoker()
inv.reply(tg.UsersSetSecureValueErrorsRequestTypeID, &tg.BoolTrue{})
b := newMockBot(inv)
h := base64.StdEncoding.EncodeToString([]byte("abc"))
errs := []PassportElementError{
nil, // skipped
&PassportElementErrorDataField{Type: "personal_details", FieldName: "f", DataHash: h, Message: "m"},
&PassportElementErrorFrontSide{Type: "passport", FileHash: h, Message: "m"},
&PassportElementErrorReverseSide{Type: "driver_license", FileHash: h, Message: "m"},
&PassportElementErrorSelfie{Type: "identity_card", FileHash: h, Message: "m"},
&PassportElementErrorFile{Type: "utility_bill", FileHash: h, Message: "m"},
&PassportElementErrorFiles{Type: "bank_statement", FileHashes: []string{h, h}, Message: "m"},
&PassportElementErrorTranslationFile{Type: "rental_agreement", FileHash: h, Message: "m"},
&PassportElementErrorTranslationFiles{Type: "passport_registration", FileHashes: []string{h}, Message: "m"},
&PassportElementErrorUnspecified{Type: "email", ElementHash: h, Message: "m"},
}
if err := b.SetPassportDataErrors(context.Background(), 42, errs); err != nil {
t.Fatalf("SetPassportDataErrors: %v", err)
}
if !inv.called(tg.UsersSetSecureValueErrorsRequestTypeID) {
t.Fatal("secure value errors RPC not invoked")
}
}
// TestPassportErrorBadHashPropagates ensures a bad hash in any variant aborts
// the whole call.
func TestPassportErrorBadHashPropagates(t *testing.T) {
b := newMockBot(newMockInvoker())
errs := []PassportElementError{
&PassportElementErrorFiles{Type: "passport", FileHashes: []string{"!!!"}, Message: "m"},
}
if err := b.SetPassportDataErrors(context.Background(), 42, errs); err == nil {
t.Fatal("expected error for invalid hash")
}
}
// TestFileUniqueID covers every branch of the file_unique_id derivation.
func TestFileUniqueID(t *testing.T) {
cases := []struct {
name string
f fileid.FileID
}{
{"web", fileid.FileID{URL: "https://example.com/x"}},
{"photo-volume", fileid.FileID{Type: fileid.Photo, PhotoSizeSource: fileid.PhotoSizeSource{VolumeID: 9, LocalID: 3}}},
{"photo-novolume", fileid.FileID{Type: fileid.Photo, ID: 7}},
{"document", fileid.FileID{Type: fileid.Document, ID: 5}},
{"secure", fileid.FileID{Type: fileid.Secure, ID: 1}},
{"encrypted", fileid.FileID{Type: fileid.Encrypted, ID: 2}},
{"temp", fileid.FileID{Type: fileid.Temp, ID: 3}},
{"profilephoto", fileid.FileID{Type: fileid.ProfilePhoto, ID: 4}},
}
for _, c := range cases {
if got := fileUniqueID(c.f); got == "" {
t.Errorf("%s: empty file_unique_id", c.name)
}
}
}
func TestGetFile(t *testing.T) {
b := &Bot{}
id := documentFileID(t, 0x1234)
f, err := b.GetFile(context.Background(), id)
if err != nil {
t.Fatal(err)
}
if f.FileID != id || f.FileUniqueID == "" {
t.Fatalf("file = %#v", f)
}
if _, err := b.GetFile(context.Background(), "not-a-file-id"); err == nil {
t.Fatal("expected error for bad file_id")
}
}
func TestDownloadFileBadID(t *testing.T) {
b := &Bot{}
if _, err := b.DownloadFile(context.Background(), "bad", nil); err == nil {
t.Fatal("expected error for bad file_id (DownloadFile)")
}
if err := b.DownloadFileToPath(context.Background(), "bad", "/tmp/x"); err == nil {
t.Fatal("expected error for bad file_id (DownloadFileToPath)")
}
}