-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjson_test.go
More file actions
164 lines (148 loc) · 4.74 KB
/
json_test.go
File metadata and controls
164 lines (148 loc) · 4.74 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
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
// Copyright 2018 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package telegramwidget
import (
"net/url"
"strings"
"testing"
"time"
)
func TestConvertAndVerifyJSON_WithValidCredentials(t *testing.T) {
u, err := ConvertAndVerifyJSON(strings.NewReader(`{
"auth_date": 1512345678,
"first_name": "John 🕶",
"hash": "25409759c10beb29bd3f3fe1d16ee0605ac82eb2907d886e196d481371b91501",
"id": 12345678,
"last_name": "Smith",
"photo_url": "https://t.me/i/userpic/320/jsmith.jpg",
"username": "jsmith"
}`), testBotToken)
if err != nil {
t.Fatalf("failed to convert and verify: %v", err)
}
if !time.Date(2017, time.December, 4, 0, 1, 18, 0, time.UTC).Equal(u.AuthDate) {
t.Errorf("auth date should be 2017-12-04T00:01:18Z, but was %v", u.AuthDate)
}
if !u.HasFirstName {
t.Error("first name should be present, but was missing")
}
if u.FirstName != "John 🕶" {
t.Errorf("first name should be John 🕶, but was %v", u.FirstName)
}
if u.ID != 12345678 {
t.Errorf("ID should be 12345678, but was %d", u.ID)
}
if !u.HasLastName {
t.Error("last name should be present, but was missing")
}
if u.LastName != "Smith" {
t.Errorf("last name should be Smith, but was %v", u.LastName)
}
if !u.HasPhotoURL {
t.Error("photo URL should be present, but was missing")
}
p := url.URL{Scheme: "https", Host: "t.me", Path: "/i/userpic/320/jsmith.jpg"}
if *u.PhotoURL != p {
t.Errorf("photo URL should be https://t.me/i/userpic/320/jsmith.jpg but was %v", u.PhotoURL)
}
if !u.HasUsername {
t.Error("username should be present, but was missing")
}
if u.Username != "jsmith" {
t.Errorf("username should be jsmith, but was %s", u.Username)
}
}
func TestConvertAndVerifyJSON_WithoutHash(t *testing.T) {
_, err := ConvertAndVerifyJSON(strings.NewReader(`{
"auth_date": 1512345678,
"first_name": "John 🕶",
"id": 12345678,
"last_name": "Smith",
"photo_url": "https://t.me/i/userpic/320/jsmith.jpg",
"username": "jsmith"
}`), testBotToken)
if err != ErrInvalidHash {
t.Errorf("expected ErrInvalidHash, but was %v", err)
}
}
func TestConvertAndVerifyJSON_WithIncorrectHash(t *testing.T) {
_, err := ConvertAndVerifyJSON(strings.NewReader(`{
"auth_date": 1512345678,
"first_name": "John 🕶",
"hash": "0000000000000000000000000000000000000000000000000000000000000000",
"id": 12345678,
"last_name": "Smith",
"photo_url": "https://t.me/i/userpic/320/jsmith.jpg",
"username": "jsmith"
}`), testBotToken)
if err != ErrInvalidHash {
t.Errorf("expected ErrInvalidHash, but was %v", err)
}
}
func TestConvertAndVerifyJSON_WithMalformedJSON(t *testing.T) {
_, err := ConvertAndVerifyJSON(strings.NewReader("food"), testBotToken)
if err == nil {
t.Errorf("should have returned error, but was nil")
}
}
func TestConvertAndVerifyJSON_WithWrongJSONValueType(t *testing.T) {
_, err := ConvertAndVerifyJSON(strings.NewReader(`"food"`), testBotToken)
if err == nil {
t.Errorf("should have returned error, but was nil")
}
}
func TestConvertAndVerifyJSON_WithNestedObject(t *testing.T) {
_, err := ConvertAndVerifyJSON(strings.NewReader(`{
"auth_date": {},
}`), testBotToken)
if err == nil {
t.Errorf("should have returned error, but was nil")
}
}
func TestConvertAndVerifyJSON_WithNestedArray(t *testing.T) {
_, err := ConvertAndVerifyJSON(strings.NewReader(`{
"auth_date": [],
}`), testBotToken)
if err == nil {
t.Errorf("should have returned error, but was nil")
}
}
func TestConvertAndVerifyJSON_WithEmptyString(t *testing.T) {
_, err := ConvertAndVerifyJSON(strings.NewReader(""), testBotToken)
if err == nil {
t.Errorf("should have returned error, but was nil")
}
}
func TestConvertAndVerifyJSON_MarksMissingFields(t *testing.T) {
u, err := ConvertAndVerifyJSON(strings.NewReader(`{
"auth_date": 1512345678,
"id": 12345678,
"hash": "180f7d26839de06e6ecb26148f181553d24e1c62153400da55ae31483ee62ad3"
}`), testBotToken)
if err != nil {
t.Errorf("failed to convert and verify: %v", err)
}
if u.HasFirstName {
t.Error("first name should be absent, but was present")
}
if u.HasLastName {
t.Error("last name should be absent, but was present")
}
if u.HasPhotoURL {
t.Error("photo URL should be absent, but was present")
}
if u.HasUsername {
t.Error("username should be absent, but was present")
}
}