|
1 | 1 | import { getConnections, PgTestClient } from 'pgsql-test'; |
2 | 2 |
|
3 | | -const validUrls = [ |
4 | | - 'http://foo.com/blah_blah', |
5 | | - 'http://foo.com/blah_blah/', |
6 | | - 'http://foo.com/blah_blah_(wikipedia)', |
7 | | - 'http://foo.com/blah_blah_(wikipedia)_(again)', |
8 | | - 'http://www.example.com/wpstyle/?p=364', |
9 | | - 'https://www.example.com/foo/?bar=baz&inga=42&quux', |
10 | | - 'http://✪df.ws/123', |
11 | | - 'http://foo.com/blah_(wikipedia)#cite-1', |
12 | | - 'http://foo.com/blah_(wikipedia)_blah#cite-1', |
13 | | - 'http://foo.com/(something)?after=parens', |
14 | | - 'http://code.google.com/events/#&product=browser', |
15 | | - 'http://j.mp', |
16 | | - 'http://foo.bar/?q=Test%20URL-encoded%20stuff', |
17 | | - 'http://مثال.إختبار', |
18 | | - 'http://例子.测试', |
19 | | - 'http://उदाहरण.परीक्षा', |
20 | | - "http://-.~_!$&'()*+,;=:%40:80%2f::::::@example.com", |
21 | | - 'http://1337.net', |
22 | | - 'http://a.b-c.de', |
23 | | - 'https://foo_bar.example.com/' |
24 | | -]; |
25 | | - |
26 | | -const invalidUrls = [ |
27 | | - 'http://##', |
28 | | - 'http://##/', |
29 | | - 'http://foo.bar?q=Spaces should be encoded', |
30 | | - '//', |
31 | | - '//a', |
32 | | - '///a', |
33 | | - '///', |
34 | | - 'http:///a', |
35 | | - 'foo.com', |
36 | | - 'rdar://1234', |
37 | | - 'h://test', |
38 | | - 'http:// shouldfail.com', |
39 | | - ':// should fail', |
40 | | - 'http://foo.bar/foo(bar)baz quux', |
41 | | - 'ftps://foo.bar/', |
42 | | - 'http://.www.foo.bar/', |
43 | | - 'http://.www.foo.bar./' |
44 | | -]; |
45 | | - |
46 | | -const validAttachments = [ |
47 | | - 'http://www.foo.bar/some.jpg', |
48 | | - 'https://foo.bar/some.PNG' |
49 | | -]; |
50 | | - |
51 | | -const invalidAttachments = [ |
52 | | - 'hi there', |
53 | | - 'ftp://foo.bar/some.png', |
54 | | - 'https:///foo.bar/some.png' |
55 | | -]; |
| 3 | +// With simplified validation, all text values are valid for url, hostname, attachment, email |
| 4 | +// Only structural checks remain: image requires 'url' key, upload requires 'url' OR 'id' OR 'key' |
56 | 5 |
|
57 | 6 | const validImages = [ |
58 | | - { url: 'http://www.foo.bar/some.jpg', mime: 'image/jpg' }, |
59 | | - { url: 'https://foo.bar/some.PNG', mime: 'image/jpg' } |
| 7 | + { url: 'http://www.foo.bar/some.jpg' }, |
| 8 | + { url: 'https://foo.bar/some.PNG' }, |
| 9 | + { url: 'any-string-is-fine' } |
60 | 10 | ]; |
61 | 11 |
|
62 | 12 | const invalidImages = [ |
63 | | - { url: 'hi there' }, |
64 | | - { url: 'https://foo.bar/some.png' } |
| 13 | + { notUrl: 'missing url key' }, |
| 14 | + { id: 'has id but not url' } |
65 | 15 | ]; |
66 | 16 |
|
67 | 17 | const validUploads = [ |
68 | | - { url: 'http://www.foo.bar/some.jpg', mime: 'image/jpg' }, |
69 | | - { url: 'https://foo.bar/some.PNG', mime: 'image/png' } |
| 18 | + { url: 'http://www.foo.bar/some.jpg' }, |
| 19 | + { url: 'https://foo.bar/some.PNG' }, |
| 20 | + { id: 'some-id' }, |
| 21 | + { key: 'some-key' }, |
| 22 | + { url: 'any-string', id: 'with-id' } |
70 | 23 | ]; |
71 | 24 |
|
72 | 25 | const invalidUploads = [ |
73 | | - { url: 'hi there' }, |
74 | | - { url: 'https://foo.bar/some.png' }, |
75 | | - { url: 'ftp://foo.bar/some.png', mime: 'image/png' } |
| 26 | + { notUrl: 'missing required keys' }, |
| 27 | + { mime: 'only mime, no url/id/key' } |
76 | 28 | ]; |
77 | 29 |
|
78 | 30 | let pg: PgTestClient; |
@@ -109,129 +61,96 @@ afterAll(async () => { |
109 | 61 | }); |
110 | 62 |
|
111 | 63 | describe('types', () => { |
112 | | - it('valid attachment and image', async () => { |
113 | | - for (const attachment of validAttachments) { |
114 | | - await pg.any(`INSERT INTO customers (attachment) VALUES ($1);`, [attachment]); |
115 | | - } |
116 | | - |
117 | | - for (const image of validImages) { |
118 | | - await pg.any(`INSERT INTO customers (image) VALUES ($1::json);`, [image]); |
119 | | - } |
120 | | - }); |
121 | | - |
122 | | - it('invalid attachment and image', async () => { |
123 | | - for (const attachment of invalidAttachments) { |
124 | | - let failed = false; |
125 | | - try { |
126 | | - await pg.any(`INSERT INTO customers (attachment) VALUES ($1);`, [attachment]); |
127 | | - } catch (e) { |
128 | | - failed = true; |
129 | | - } |
130 | | - expect(failed).toBe(true); |
131 | | - } |
132 | | - |
133 | | - for (const image of invalidImages) { |
134 | | - let failed = false; |
135 | | - try { |
136 | | - await pg.any(`INSERT INTO customers (image) VALUES ($1::json);`, [image]); |
137 | | - } catch (e) { |
138 | | - failed = true; |
| 64 | + describe('url domain (plain text, no validation)', () => { |
| 65 | + it('accepts any text value', async () => { |
| 66 | + const values = [ |
| 67 | + 'http://foo.com/blah', |
| 68 | + 'https://example.com', |
| 69 | + 'not-a-url', |
| 70 | + 'ftp://something', |
| 71 | + 'random text' |
| 72 | + ]; |
| 73 | + for (const value of values) { |
| 74 | + await pg.any(`INSERT INTO customers (url) VALUES ($1);`, [value]); |
139 | 75 | } |
140 | | - expect(failed).toBe(true); |
141 | | - } |
142 | | - }); |
143 | | - |
144 | | - it('valid upload', async () => { |
145 | | - for (const upload of validUploads) { |
146 | | - await pg.any(`INSERT INTO customers (upload) VALUES ($1::json);`, [upload]); |
147 | | - } |
| 76 | + }); |
148 | 77 | }); |
149 | 78 |
|
150 | | - it('invalid upload', async () => { |
151 | | - for (const upload of invalidUploads) { |
152 | | - let failed = false; |
153 | | - try { |
154 | | - await pg.any(`INSERT INTO customers (upload) VALUES ($1::json);`, [upload]); |
155 | | - } catch (e) { |
156 | | - failed = true; |
| 79 | + describe('hostname domain (plain text, no validation)', () => { |
| 80 | + it('accepts any text value', async () => { |
| 81 | + const values = [ |
| 82 | + 'google.com', |
| 83 | + 'www.example.com', |
| 84 | + 'not-a-hostname', |
| 85 | + 'http://with-protocol.com' |
| 86 | + ]; |
| 87 | + for (const value of values) { |
| 88 | + await pg.any(`INSERT INTO customers (domain) VALUES ($1);`, [value]); |
157 | 89 | } |
158 | | - expect(failed).toBe(true); |
159 | | - } |
| 90 | + }); |
160 | 91 | }); |
161 | 92 |
|
162 | | - it('valid url', async () => { |
163 | | - for (const value of validUrls) { |
164 | | - await pg.any(`INSERT INTO customers (url) VALUES ($1);`, [value]); |
165 | | - } |
166 | | - }); |
167 | | - |
168 | | - it('invalid url', async () => { |
169 | | - for (const value of invalidUrls) { |
170 | | - let failed = false; |
171 | | - try { |
172 | | - await pg.any(`INSERT INTO customers (url) VALUES ($1);`, [value]); |
173 | | - } catch (e) { |
174 | | - failed = true; |
| 93 | + describe('attachment domain (plain text, no validation)', () => { |
| 94 | + it('accepts any text value', async () => { |
| 95 | + const values = [ |
| 96 | + 'http://www.foo.bar/some.jpg', |
| 97 | + 'https://foo.bar/some.PNG', |
| 98 | + 'not-a-url', |
| 99 | + 'random text' |
| 100 | + ]; |
| 101 | + for (const value of values) { |
| 102 | + await pg.any(`INSERT INTO customers (attachment) VALUES ($1);`, [value]); |
175 | 103 | } |
176 | | - expect(failed).toBe(true); |
177 | | - } |
178 | | - }); |
179 | | - |
180 | | - it('email', async () => { |
181 | | - await pg.any(` |
182 | | - INSERT INTO customers (email) VALUES |
183 | | - ('d@google.com'), |
184 | | - ('d@google.in'), |
185 | | - ('d@google.in'), |
186 | | - ('d@www.google.in'), |
187 | | - ('d@google.io'), |
188 | | - ('dan@google.some.other.com')`); |
| 104 | + }); |
189 | 105 | }); |
190 | 106 |
|
191 | | - it('not email', async () => { |
192 | | - let failed = false; |
193 | | - try { |
| 107 | + describe('email domain (citext, no validation)', () => { |
| 108 | + it('accepts any text value', async () => { |
194 | 109 | await pg.any(` |
195 | 110 | INSERT INTO customers (email) VALUES |
196 | | - ('http://google.some.other.com')`); |
197 | | - } catch (e) { |
198 | | - failed = true; |
199 | | - } |
200 | | - expect(failed).toBe(true); |
201 | | - }); |
202 | | - |
203 | | - it('hostname', async () => { |
204 | | - await pg.any(` |
205 | | - INSERT INTO customers (domain) VALUES |
206 | | - ('google.com'), |
207 | | - ('google.in'), |
208 | | - ('google.in'), |
209 | | - ('www.google.in'), |
210 | | - ('google.io'), |
211 | | - ('google.some.other.com')`); |
| 111 | + ('d@google.com'), |
| 112 | + ('not-an-email'), |
| 113 | + ('random text')`); |
| 114 | + }); |
212 | 115 | }); |
213 | 116 |
|
214 | | - it('not hostname', async () => { |
215 | | - let failed = false; |
216 | | - try { |
217 | | - await pg.any(` |
218 | | - INSERT INTO customers (domain) VALUES |
219 | | - ('http://google.some.other.com')`); |
220 | | - } catch (e) { |
221 | | - failed = true; |
222 | | - } |
223 | | - expect(failed).toBe(true); |
| 117 | + describe('image domain (jsonb requiring url key)', () => { |
| 118 | + it('accepts valid images with url key', async () => { |
| 119 | + for (const image of validImages) { |
| 120 | + await pg.any(`INSERT INTO customers (image) VALUES ($1::json);`, [image]); |
| 121 | + } |
| 122 | + }); |
| 123 | + |
| 124 | + it('rejects images without url key', async () => { |
| 125 | + for (const image of invalidImages) { |
| 126 | + let failed = false; |
| 127 | + try { |
| 128 | + await pg.any(`INSERT INTO customers (image) VALUES ($1::json);`, [image]); |
| 129 | + } catch (e) { |
| 130 | + failed = true; |
| 131 | + } |
| 132 | + expect(failed).toBe(true); |
| 133 | + } |
| 134 | + }); |
224 | 135 | }); |
225 | 136 |
|
226 | | - it('not hostname 2', async () => { |
227 | | - let failed = false; |
228 | | - try { |
229 | | - await pg.any(` |
230 | | - INSERT INTO customers (domain) VALUES |
231 | | - ('google.some.other.com/a/b/d')`); |
232 | | - } catch (e) { |
233 | | - failed = true; |
234 | | - } |
235 | | - expect(failed).toBe(true); |
| 137 | + describe('upload domain (jsonb requiring url OR id OR key)', () => { |
| 138 | + it('accepts valid uploads with url, id, or key', async () => { |
| 139 | + for (const upload of validUploads) { |
| 140 | + await pg.any(`INSERT INTO customers (upload) VALUES ($1::json);`, [upload]); |
| 141 | + } |
| 142 | + }); |
| 143 | + |
| 144 | + it('rejects uploads without url, id, or key', async () => { |
| 145 | + for (const upload of invalidUploads) { |
| 146 | + let failed = false; |
| 147 | + try { |
| 148 | + await pg.any(`INSERT INTO customers (upload) VALUES ($1::json);`, [upload]); |
| 149 | + } catch (e) { |
| 150 | + failed = true; |
| 151 | + } |
| 152 | + expect(failed).toBe(true); |
| 153 | + } |
| 154 | + }); |
236 | 155 | }); |
237 | 156 | }); |
0 commit comments