Parameter.spec.ts 16.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12
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;
    });

13 14 15 16 17 18
    /*
     * 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.
     */
19 20
    describe("constructor()", () => {
        it("Should create an object", () => {
21
            const result: Parameter = new Parameter("TestName", ["value"]);
22 23
            expect(result).to.exist;
        });
24 25 26 27 28

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

29 30 31 32 33
            expect(testParam).to.have.property("_paramName");
            expect(testParam).to.have.property("_paramValues");
            expect(testParam).to.have.property("paramName");
            expect(testParam).to.have.property("paramValues");
            expect(testParam).to.have.property("generate");
34
        });
35 36
    });

37 38 39 40 41
    /*
     * 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.
     */
42 43
    describe("GET Methods", () => {
        describe("paramName", () => {
44 45 46 47 48 49 50 51
            it("Should return the correct name", () => {
                const testName: string = "TEST-NAME";
                const testValues: string[] = ["value1", "value2"];
                const testParam: Parameter = new Parameter(testName, testValues);
                const result: string = testParam.paramName;

                expect(result).to.equal(testName);
            });
52 53 54
        });

        describe("paramValues", () => {
55 56 57 58 59 60 61 62
            it("Should return the correct array of values", () => {
                const testName: string = "TEST-NAME";
                const testValues: string[] = ["value1", "value2"];
                const testParam: Parameter = new Parameter(testName, testValues);
                const result: string[] = testParam.paramValues;

                expect(result).to.deep.equal(testValues);
            });
63 64 65
        });
    });

66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
    /*
     * 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.
     */
82 83
    describe("SET Methods", () => {
        describe("paramName", () => {
84
            it("Correctly sets iana-token names", () => {
Mark Stenglein's avatar
Mark Stenglein committed
85 86 87
                const name: string = "A0-TEST";
                const values: string[] = ["value"];
                const testParam: Parameter = new Parameter(name, values);
88

Mark Stenglein's avatar
Mark Stenglein committed
89
                expect(testParam.paramName).to.be.equal(name);
90 91 92
            });

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

Mark Stenglein's avatar
Mark Stenglein committed
97
                expect(testParam.paramName).to.be.equal(name);
98 99 100
            });

            it("Throws an exception on incorrect name type", () => {
Mark Stenglein's avatar
Mark Stenglein committed
101 102 103 104
                expect(() => {
                    const name: string = "!NVALID-N@ME";
                    const values: string[] = ["value"];
                    const testParam: Parameter = new Parameter(name, values);
105
                }).to.throw("Parameter must be valid iana-token or x-name");
106
            });
107 108
        });

109
        describe("paramValues", () => {
110
            it("Correctly sets paramtext values", () => {
111 112 113 114
                const name: string = "TEST";
                const values: string[] = ["value", "value2"];
                const param: Parameter = new Parameter(name, values);
                const result: string[] = param.paramValues;
115

116
                expect(result).to.deep.equal(values);
117 118 119
            });

            it("Correctly sets quoted-string values", () => {
120 121 122 123
                const name: string = "TEST";
                const values: string[] = ["\"value\"", "\"value2\""];
                const param: Parameter = new Parameter(name, values);
                const result: string[] = param.paramValues;
124

125
                expect(result).to.deep.equal(values);
126 127
            });

128 129 130 131 132 133 134 135
            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);
            });
136

137 138 139 140 141 142 143
            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");
144
            });
145 146 147
        });
    });

148 149 150 151 152 153 154 155 156 157 158 159 160
    /**
     * 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.
     */
161
    describe("***generate()", () => {
162
        it("Correctly generates single-valued parameters", () => {
163 164 165
            const param: Parameter = new Parameter("NAME", ["value1"]);
            const result: string = param.generate();
            const expected: string = "NAME=value1";
166

167
            expect(result).to.be.equal(expected);
168 169 170
        });

        it("Correctly generates double-valued parameters", () => {
171 172 173
            const param: Parameter = new Parameter("NAME", ["value1", "value2"]);
            const result: string = param.generate();
            const expected: string = "NAME=value1,value2";
174

175
            expect(result).to.be.equal(expected);
176
        });
177

178
        it("Correctly generates multi-valued parameters", () => {
179 180 181 182
            const param: Parameter = new Parameter("NAME", [
                "value1", "value2", "value3"]);
            const result: string = param.generate();
            const expected: string = "NAME=value1,value2,value3";
183

184
            expect(result).to.be.equal(expected);
185
        });
186 187
    });

188 189 190 191 192 193
    /**
     * 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.
     */
194
    describe("STATIC Methods", () => {
195 196 197 198 199 200
        /**
         * iana-token definition from RFC 5545 (pg. 10)
         *
         *     iana-token = 1*(ALPHA / DIGIT / "-")
         *     ; iCalendar identifier registered with IANA
         */
201 202 203 204 205 206
        describe("isIanaToken()", () => {
            it("exists", () => {
                expect(Parameter).itself.respondsTo("isIanaToken");
            });

            it("returns true for valid iana-token", () => {
Mark Stenglein's avatar
Mark Stenglein committed
207 208 209
                const test: string = "ICALENDAR";
                const result: boolean = Parameter.isIanaToken(test);
                expect(result).to.be.true;
210 211 212
            });

            it("returns false for empty string", () => {
Mark Stenglein's avatar
Mark Stenglein committed
213 214 215
                const test: string = "";
                const result: boolean = Parameter.isIanaToken(test);
                expect(result).to.be.false;
216 217 218
            });

            it("returns true for string containing \"-\"", () => {
Mark Stenglein's avatar
Mark Stenglein committed
219 220 221
                const test: string = "ICAL-TEST";
                const result: boolean = Parameter.isIanaToken(test);
                expect(result).to.be.true;
222 223 224
            });

            it("returns false for string containing special chars", () => {
Mark Stenglein's avatar
Mark Stenglein committed
225 226 227
                const test: string = "!CAL-TEST";
                const result: boolean = Parameter.isIanaToken(test);
                expect(result).to.be.false;
228 229 230
            });

            it("returns false for string containing control chars", () => {
Mark Stenglein's avatar
Mark Stenglein committed
231 232 233
                const test: string = "CAL-TEST\r\n";
                const result: boolean = Parameter.isIanaToken(test);
                expect(result).to.be.false;
234 235 236 237
            });

        });

238 239 240 241 242 243 244 245 246
        /**
         * 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
         */
247 248 249 250 251 252
        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
253 254 255
                const test: string = "X-TESTING-TIME";
                const result: boolean = Parameter.isXName(test);
                expect(result).to.be.true;
256 257 258
            });

            it("returns true for x-name value containing vendorid", () => {
Mark Stenglein's avatar
Mark Stenglein committed
259 260 261
                const test: string = "X-MIC-TESTING-TIME";
                const result: boolean = Parameter.isXName(test);
                expect(result).to.be.true;
262 263 264
            });

            it("returns false for x-name containing special chars", () => {
Mark Stenglein's avatar
Mark Stenglein committed
265 266 267
                const test: string = "X-TEST!NG-TIME";
                const result: boolean = Parameter.isXName(test);
                expect(result).to.be.false;
268 269 270
            });

            it("returns false for x-name containing control chars", () => {
Mark Stenglein's avatar
Mark Stenglein committed
271 272 273
                const test: string = "X-TESTNG-TIME/r/n";
                const result: boolean = Parameter.isXName(test);
                expect(result).to.be.false;
274 275 276
            });

            it("returns false when missing \"X-\" from start" +
Mark Stenglein's avatar
Mark Stenglein committed
277 278 279 280
                    " of x-name", () => {
                const test: string = "TESTNG-TIME";
                const result: boolean = Parameter.isXName(test);
                expect(result).to.be.false;
281 282 283
            });

            it("returns false for empty string", () => {
Mark Stenglein's avatar
Mark Stenglein committed
284 285 286
                const test: string = "";
                const result: boolean = Parameter.isXName(test);
                expect(result).to.be.false;
287 288 289 290
            });

            it("returns false for vendorid containing special" +
                " chars (even \"-\")", () => {
Mark Stenglein's avatar
Mark Stenglein committed
291 292 293
                const test: string = "X-V#NDOR-TEST";
                const result: boolean = Parameter.isXName(test);
                expect(result).to.be.false;
294 295 296
            });
        });

297 298 299 300
        /**
         * param-text definition from RFC 5545 (pg. 10)
         *
         *     paramtext = *SAFE-CHAR
Mark Stenglein's avatar
Mark Stenglein committed
301 302 303 304 305
         *
         * 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.
306
         */
307
        describe("isParamText()", () => {
308 309 310 311
            it("exists", () => {
                expect(Parameter).itself.respondsTo("isParamText");
            });

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

315 316 317 318 319 320 321
        /**
         * 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, ";", ":", ","
         */
322
        describe("isSafeChar()", () => {
323 324 325
            it("exists", () => {
                expect(Parameter).itself.respondsTo("isSafeChar");
            });
Mark Stenglein's avatar
Mark Stenglein committed
326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 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

            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;
            });

377 378
        });

379 380 381 382 383
        /**
         * quoted-string definition from RFC 5545 (pg. 11)
         *
         *     quoted-string = DQUOTE *QSAFE-CHAR DQUOTE
         */
384
        describe("isQuotedString()", () => {
385 386 387
            it("exists", () => {
                expect(Parameter).itself.respondsTo("isQuotedString");
            });
Mark Stenglein's avatar
Mark Stenglein committed
388

Mark Stenglein's avatar
Mark Stenglein committed
389 390 391
            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
392

Mark Stenglein's avatar
Mark Stenglein committed
393 394 395 396 397 398 399 400
                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
401 402
            });

Mark Stenglein's avatar
Mark Stenglein committed
403 404 405 406 407 408 409
            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;
            });
410 411
        });

412 413 414 415 416 417
        /**
         * QSAFE-CHAR definition from RFC 5545 (pg. 11)
         *
         *     QSAFE-CHAR = WSP / %x21 / %x23-7E / NON-US-ASCII
         *     ; Any character except CONTROL and DQUOTE
         */
418
        describe("isQSafeChar()", () => {
419 420 421
            it("exists", () => {
                expect(Parameter).itself.respondsTo("isQSafeChar");
            });
Mark Stenglein's avatar
Mark Stenglein committed
422

Mark Stenglein's avatar
Mark Stenglein committed
423 424 425 426 427
            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
428

Mark Stenglein's avatar
Mark Stenglein committed
429
                expect(result).to.be.true;
Mark Stenglein's avatar
Mark Stenglein committed
430 431
            });

Mark Stenglein's avatar
Mark Stenglein committed
432 433 434 435 436 437
            it("Returns false for DQUOTE", () => {
                const test: string = "test this char '\"'";
                const result: boolean = Parameter.isQuotedString(test);

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

Mark Stenglein's avatar
Mark Stenglein committed
439 440 441 442 443 444 445 446 447 448 449 450 451 452
            /**
             * 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;
            });
        });
453 454
    });
});