Parameter.spec.ts 17.7 KB
Newer Older
Mark Stenglein's avatar
Mark Stenglein committed
1 2
/*
 * lib-ical
Mark Stenglein's avatar
Mark Stenglein committed
3
 * Copyright (C) 2017 Mark Stenglein
Mark Stenglein's avatar
Mark Stenglein committed
4 5 6 7 8 9 10 11 12 13 14 15 16 17
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published
 * by the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
18 19 20 21 22 23 24 25 26 27 28 29
import Parameter from "../src/Parameter";
import { expect } from "chai";
import "mocha";

/**
 * Test the Parameter parent class.
 */
describe("Parameter", () => {
    it("Should exist", () => {
        expect(Parameter).to.exist;
    });

30 31 32 33 34 35
    /*
     * Note: it is not neccessary to test all of the behavior for the
     * constructor as far as validation of input data is concerned. This is
     * because the SET method for each property performs that function. Look
     * there for tests.
     */
36 37
    describe("constructor()", () => {
        it("Should create an object", () => {
38
            const result: Parameter = new Parameter("TestName", ["value"]);
39 40
            expect(result).to.exist;
        });
41 42 43 44 45

        it("Should have all the right properties", () => {
            const testName: string = "TEST-NAME";
            const testParam: Parameter = new Parameter(testName, ["value1"]);

46 47 48 49
            expect(testParam).to.have.property("_paramName");
            expect(testParam).to.have.property("_paramValues");
            expect(testParam).to.have.property("paramName");
            expect(testParam).to.have.property("paramValues");
50
            expect(testParam).to.have.property("reqContentValue");
51
            expect(testParam).to.have.property("generate");
52
        });
53 54
    });

55 56 57 58 59
    /*
     * All the GET methods are really doing is pulling the private data
     *  variables out for the requester to see. Nothing too complex is happening
     *  that needs to be tested.
     */
60 61
    describe("GET Methods", () => {
        describe("paramName", () => {
62 63 64
            it("Should return the correct name", () => {
                const testName: string = "TEST-NAME";
                const testValues: string[] = ["value1", "value2"];
65 66
                const testParam: Parameter = new Parameter(
                    testName, testValues);
67 68 69 70
                const result: string = testParam.paramName;

                expect(result).to.equal(testName);
            });
71 72 73
        });

        describe("paramValues", () => {
74 75 76
            it("Should return the correct array of values", () => {
                const testName: string = "TEST-NAME";
                const testValues: string[] = ["value1", "value2"];
77 78
                const testParam: Parameter = new Parameter(
                    testName, testValues);
79 80 81 82
                const result: string[] = testParam.paramValues;

                expect(result).to.deep.equal(testValues);
            });
83 84 85
        });
    });

86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
    /*
     * Note that the SET methods are difficult to directly test, as they are
     *  called even by the constructor function when setting the values. Because
     *  of that behavior, they can be tested directly by simply calling the
     *  class constructors with the values you want to test.
     *
     * The constructor is really only going so far as to do this:
     *
     *     constructor(inName: string, inValues: string[]) {
     *         this.paramName = inName;
     *         this.paramValues = inValues;
     *     }
     *
     * These tests implement the data validation that is missing from the
     *  constructor level tests.
     */
102 103
    describe("SET Methods", () => {
        describe("paramName", () => {
104
            it("Correctly sets iana-token names", () => {
Mark Stenglein's avatar
Mark Stenglein committed
105 106 107
                const name: string = "A0-TEST";
                const values: string[] = ["value"];
                const testParam: Parameter = new Parameter(name, values);
108

Mark Stenglein's avatar
Mark Stenglein committed
109
                expect(testParam.paramName).to.be.equal(name);
110 111 112
            });

            it("Correctly sets x-name names", () => {
Mark Stenglein's avatar
Mark Stenglein committed
113 114 115
                const name: string = "X-VENDOR-TEST";
                const values: string[] = ["value"];
                const testParam: Parameter = new Parameter(name, values);
116

Mark Stenglein's avatar
Mark Stenglein committed
117
                expect(testParam.paramName).to.be.equal(name);
118 119 120
            });

            it("Throws an exception on incorrect name type", () => {
Mark Stenglein's avatar
Mark Stenglein committed
121 122 123 124
                expect(() => {
                    const name: string = "!NVALID-N@ME";
                    const values: string[] = ["value"];
                    const testParam: Parameter = new Parameter(name, values);
125
                }).to.throw("Parameter must be valid iana-token or x-name");
126
            });
127 128
        });

129
        describe("paramValues", () => {
130
            it("Correctly sets paramtext values", () => {
131 132 133 134
                const name: string = "TEST";
                const values: string[] = ["value", "value2"];
                const param: Parameter = new Parameter(name, values);
                const result: string[] = param.paramValues;
135

136
                expect(result).to.deep.equal(values);
137 138 139
            });

            it("Correctly sets quoted-string values", () => {
140 141 142 143
                const name: string = "TEST";
                const values: string[] = ["\"value\"", "\"value2\""];
                const param: Parameter = new Parameter(name, values);
                const result: string[] = param.paramValues;
144

145
                expect(result).to.deep.equal(values);
146 147
            });

148 149 150 151 152 153 154 155
            it("Correctly sets mixed type values", () => {
                const name: string = "TEST";
                const values: string[] = ["value", "\"value2\""];
                const param: Parameter = new Parameter(name, values);
                const result: string[] = param.paramValues;

                expect(result).to.deep.equal(values);
            });
156

157 158 159 160 161 162 163
            it("Throws an exception on malformed string", () => {
                expect(() => {
                    const name: string = "TEST";
                    const values: string[] = ["V#\"!UE"];
                    const param: Parameter = new Parameter(name, values);
                }).to.throw("param-value must either be valid paramtext or" +
                    " quoted-string");
164
            });
165 166 167
        });
    });

168 169 170 171 172 173 174 175 176 177 178 179 180
    /**
     * Tests the generation of param strings to make sure they are to spec with
     *   RFC 5545's ABNF definition (pg. 10):
     *
     *     param = param-name "=" param-value *("," param-value)
     *     ; Each property defines the specific ABNF for the parameters
     *     ; allowed on the property. Refer to specific properties for
     *     ; precise parameter ABNF.
     *
     * tl;dr of the above snippet is that this is a basic structure check so
     *   that the higher level classes can simply validate proper types and use
     *   the lower level generate method.
     */
181
    describe("generate()", () => {
182
        it("Correctly generates single-valued parameters", () => {
183 184 185
            const param: Parameter = new Parameter("NAME", ["value1"]);
            const result: string = param.generate();
            const expected: string = "NAME=value1";
186

187
            expect(result).to.be.equal(expected);
188 189 190
        });

        it("Correctly generates double-valued parameters", () => {
191 192
            const param: Parameter = new Parameter(
                "NAME", ["value1", "value2"]);
193 194
            const result: string = param.generate();
            const expected: string = "NAME=value1,value2";
195

196
            expect(result).to.be.equal(expected);
197
        });
198

199
        it("Correctly generates multi-valued parameters", () => {
200 201 202 203
            const param: Parameter = new Parameter("NAME", [
                "value1", "value2", "value3"]);
            const result: string = param.generate();
            const expected: string = "NAME=value1,value2,value3";
204

205
            expect(result).to.be.equal(expected);
206
        });
207 208
    });

209 210 211 212 213 214
    /**
     * There are currently many more static methods then will be in the final
     *   class. I need to start building the other objects before I will have a
     *   better idea which of these validators are unique or need to be moved to
     *   a general utility file.
     */
215
    describe("STATIC Methods", () => {
216 217 218 219 220 221
        /**
         * iana-token definition from RFC 5545 (pg. 10)
         *
         *     iana-token = 1*(ALPHA / DIGIT / "-")
         *     ; iCalendar identifier registered with IANA
         */
222 223 224 225 226 227
        describe("isIanaToken()", () => {
            it("exists", () => {
                expect(Parameter).itself.respondsTo("isIanaToken");
            });

            it("returns true for valid iana-token", () => {
Mark Stenglein's avatar
Mark Stenglein committed
228 229 230
                const test: string = "ICALENDAR";
                const result: boolean = Parameter.isIanaToken(test);
                expect(result).to.be.true;
231 232 233
            });

            it("returns false for empty string", () => {
Mark Stenglein's avatar
Mark Stenglein committed
234 235 236
                const test: string = "";
                const result: boolean = Parameter.isIanaToken(test);
                expect(result).to.be.false;
237 238 239
            });

            it("returns true for string containing \"-\"", () => {
Mark Stenglein's avatar
Mark Stenglein committed
240 241 242
                const test: string = "ICAL-TEST";
                const result: boolean = Parameter.isIanaToken(test);
                expect(result).to.be.true;
243 244 245
            });

            it("returns false for string containing special chars", () => {
Mark Stenglein's avatar
Mark Stenglein committed
246 247 248
                const test: string = "!CAL-TEST";
                const result: boolean = Parameter.isIanaToken(test);
                expect(result).to.be.false;
249 250 251
            });

            it("returns false for string containing control chars", () => {
Mark Stenglein's avatar
Mark Stenglein committed
252 253 254
                const test: string = "CAL-TEST\r\n";
                const result: boolean = Parameter.isIanaToken(test);
                expect(result).to.be.false;
255 256 257 258
            });

        });

259 260 261 262 263 264 265 266 267
        /**
         * x-name definition from RFC 5545 (pg. 10)
         *
         *     x-name = "X-" [vendorid "-"] 1*(ALPHA / DIGIT / "-")
         *     ; Reserved for experimental use.
         *
         *     vendorid = 3*(ALPHA / DIGIT)
         *     ; Vendor identification
         */
268 269 270 271 272 273
        describe("isXName()", () => {
            it("exists", () => {
                expect(Parameter).itself.respondsTo("isXName");
            });

            it("returns true for x-name value without vendorid", () => {
Mark Stenglein's avatar
Mark Stenglein committed
274 275 276
                const test: string = "X-TESTING-TIME";
                const result: boolean = Parameter.isXName(test);
                expect(result).to.be.true;
277 278 279
            });

            it("returns true for x-name value containing vendorid", () => {
Mark Stenglein's avatar
Mark Stenglein committed
280 281 282
                const test: string = "X-MIC-TESTING-TIME";
                const result: boolean = Parameter.isXName(test);
                expect(result).to.be.true;
283 284 285
            });

            it("returns false for x-name containing special chars", () => {
Mark Stenglein's avatar
Mark Stenglein committed
286 287 288
                const test: string = "X-TEST!NG-TIME";
                const result: boolean = Parameter.isXName(test);
                expect(result).to.be.false;
289 290 291
            });

            it("returns false for x-name containing control chars", () => {
Mark Stenglein's avatar
Mark Stenglein committed
292 293 294
                const test: string = "X-TESTNG-TIME/r/n";
                const result: boolean = Parameter.isXName(test);
                expect(result).to.be.false;
295 296 297
            });

            it("returns false when missing \"X-\" from start" +
Mark Stenglein's avatar
Mark Stenglein committed
298 299 300 301
                    " of x-name", () => {
                const test: string = "TESTNG-TIME";
                const result: boolean = Parameter.isXName(test);
                expect(result).to.be.false;
302 303 304
            });

            it("returns false for empty string", () => {
Mark Stenglein's avatar
Mark Stenglein committed
305 306 307
                const test: string = "";
                const result: boolean = Parameter.isXName(test);
                expect(result).to.be.false;
308 309 310 311
            });

            it("returns false for vendorid containing special" +
                " chars (even \"-\")", () => {
Mark Stenglein's avatar
Mark Stenglein committed
312 313 314
                const test: string = "X-V#NDOR-TEST";
                const result: boolean = Parameter.isXName(test);
                expect(result).to.be.false;
315 316 317
            });
        });

318 319 320 321
        /**
         * param-text definition from RFC 5545 (pg. 10)
         *
         *     paramtext = *SAFE-CHAR
Mark Stenglein's avatar
Mark Stenglein committed
322 323 324 325 326
         *
         * Because this only exists to check and make sure that it is safe text
         *   of any length, this really only needs to be checked to be sure it
         *   exists and somebody hasn't renamed/deleted it. It's essentially an
         *   alias.
327
         */
328
        describe("isParamText()", () => {
329 330 331 332
            it("exists", () => {
                expect(Parameter).itself.respondsTo("isParamText");
            });

Mark Stenglein's avatar
Mark Stenglein committed
333
            it("see isSafeChar() for other tests; this is an alias", () => {});
334 335
        });

336 337 338 339 340 341 342
        /**
         * SAFE-CHAR definition from RFC 5545 (pg. 11)
         *
         *     SAFE-CHAR = WSP / %x21 / %x23-2B / %x2D-39 / %x3C-7E
         *               / NON-US-ASCII
         *     ; Any character except CONTROL, DQUOTE, ";", ":", ","
         */
343
        describe("isSafeChar()", () => {
344 345 346
            it("exists", () => {
                expect(Parameter).itself.respondsTo("isSafeChar");
            });
Mark Stenglein's avatar
Mark Stenglein committed
347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397

            it("Returns true for any char except ctl,dquote,;,:,\",\"", () => {
                const test: string = "abcdefghijklmnopqrstuvwxyz" +
                    " ABCDEFGHIJKLMNOPQRSTUVWXYZ 01234567890 !@#$%^&*()~`'.";
                const result: boolean = Parameter.isSafeChar(test);

                expect(result).to.be.true;
            });

            it("Returns false for \";\"", () => {
                const test: string = "test this char ';'";
                const result: boolean = Parameter.isSafeChar(test);

                expect(result).to.be.false;
            });

            it("Returns false for \":\"", () => {
                const test: string = "test this char ':'";
                const result: boolean = Parameter.isSafeChar(test);

                expect(result).to.be.false;
            });

            it("Returns false for \",\"", () => {
                const test: string = "test this char ','";
                const result: boolean = Parameter.isSafeChar(test);

                expect(result).to.be.false;
            });

            it("Returns false for DQUOTE", () => {
                const test: string = "test this char '\"'";
                const result: boolean = Parameter.isSafeChar(test);

                expect(result).to.be.false;
            });

            /**
             * This test needs to be updated for the CONTROL char. I'm
             *   not really sure how to make a CONTROL char in js escape
             *   sequences though, so I'm skipping for now.
             *
             * TODO: Come back to this!
             */
            it("Returns false for CONTROL", () => {
                const test: string = "test this char '\"'";
                const result: boolean = false; // Parameter.isSafeChar(test);

                expect(result).to.be.false;
            });

398 399
        });

400 401 402 403 404
        /**
         * quoted-string definition from RFC 5545 (pg. 11)
         *
         *     quoted-string = DQUOTE *QSAFE-CHAR DQUOTE
         */
405
        describe("isQuotedString()", () => {
406 407 408
            it("exists", () => {
                expect(Parameter).itself.respondsTo("isQuotedString");
            });
Mark Stenglein's avatar
Mark Stenglein committed
409

Mark Stenglein's avatar
Mark Stenglein committed
410 411 412
            it("returns true for valid quoted-string", () => {
                const test: string = "\"Test this valid quote-string!\"";
                const result: boolean = Parameter.isQuotedString(test);
Mark Stenglein's avatar
Mark Stenglein committed
413

Mark Stenglein's avatar
Mark Stenglein committed
414 415 416 417 418 419 420 421
                expect(result).to.be.true;
            });

            it("returns false for single quotes", () => {
                const test: string = "'Test this invalid quote-string!'";
                const result: boolean = Parameter.isQuotedString(test);

                expect(result).to.be.false;
Mark Stenglein's avatar
Mark Stenglein committed
422 423
            });

Mark Stenglein's avatar
Mark Stenglein committed
424 425 426 427 428 429 430
            it("returns false for invalid QSAFE-CHAR", () => {
                const test: string = "\"Test this invalid, \"FAKE NEWS,\"" +
                    " quote-string!\"";
                const result: boolean = Parameter.isQuotedString(test);

                expect(result).to.be.false;
            });
431 432
        });

433 434 435 436 437 438
        /**
         * QSAFE-CHAR definition from RFC 5545 (pg. 11)
         *
         *     QSAFE-CHAR = WSP / %x21 / %x23-7E / NON-US-ASCII
         *     ; Any character except CONTROL and DQUOTE
         */
439
        describe("isQSafeChar()", () => {
440 441 442
            it("exists", () => {
                expect(Parameter).itself.respondsTo("isQSafeChar");
            });
Mark Stenglein's avatar
Mark Stenglein committed
443

Mark Stenglein's avatar
Mark Stenglein committed
444 445 446 447 448
            it("returns true for valid QSAFE-CHAR", () => {
                const test: string = "abcdefghijklmnopqrstuvwxyz" +
                    " ABCDEFGHIJKLMNOPQRSTUVWXYZ 01234567890 !@#$%^&*()~`'." +
                    " ;:,";
                const result: boolean = Parameter.isQSafeChar(test);
Mark Stenglein's avatar
Mark Stenglein committed
449

Mark Stenglein's avatar
Mark Stenglein committed
450
                expect(result).to.be.true;
Mark Stenglein's avatar
Mark Stenglein committed
451 452
            });

Mark Stenglein's avatar
Mark Stenglein committed
453 454 455 456 457 458
            it("Returns false for DQUOTE", () => {
                const test: string = "test this char '\"'";
                const result: boolean = Parameter.isQuotedString(test);

                expect(result).to.be.false;
            });
459

Mark Stenglein's avatar
Mark Stenglein committed
460 461 462 463 464 465 466 467 468 469 470 471 472 473
            /**
             * This test needs to be updated for the CONTROL char. I'm
             *   not really sure how to make a CONTROL char in js escape
             *   sequences though, so I'm skipping for now.
             *
             * TODO: Come back to this!
             */
            it("Returns false for CONTROL", () => {
                const test: string = "test this char '\"'";
                const result: boolean = false; // Parameter.isSafeChar(test);

                expect(result).to.be.false;
            });
        });
474 475
    });
});