Unverified Commit 1d10865c authored by Mark Stenglein's avatar Mark Stenglein
Browse files

DirectoryEntryReference: implement chapter 3.2.6



This commit adds in the implementation for RFC5545's chapter 3.2.6.

Moving forward, there are two TODO's that should be considered:

  1. When the CAL-ADDRESS type is implemented, there needs to be
  something added to this and other classes like it that enforce the
  restriction on which content line types are allowed to possess this
  parameter type.

  2. When this project is able to be ported over in such a way that it
  can be browser packaged, there might be an issue with using the NodeJS
  implemented URL class. I'm not sure about if it will work or not, but
  future me looking at git blames for why it is not working should take
  some direction from this message. Keep going, future me, keep going!
Signed-off-by: Mark Stenglein's avatarMark Stenglein <mark@stengle.in>
parent bce1cdd9
Pipeline #1223 passed with stage
in 45 seconds
......@@ -16,6 +16,8 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import Parameter from "./Parameter";
import * as url from "url";
const URL = url.URL;
/**
* DelegatorsParam Class (Chapter 3.2.4)
......@@ -34,7 +36,7 @@ import Parameter from "./Parameter";
* - Specifies a reference to the directory entry associated with the
* calendar user specified by the property.
* - The parameter value is a URI.
* - TODO: enforce that the URI is valid
* - DONE: enforce that the URI is valid
* - The URI MUST be specified in a quoted-string.
*
* - Example:
......@@ -45,7 +47,57 @@ import Parameter from "./Parameter";
* @since 0.1.0
* @author Mark Stenglein <mark@stengle.in>
*/
export default class DelegatorsParam extends Parameter {
export default class DirectoryEntryReference extends Parameter {
/** @access private */
public _uri: url.URL;
/**
* Builds a new DirectoryEntryReference object.
*
* @author Mark Stenglein <mark@stengle.in>
* @since 0.1.0
* @param uri Valid URI String (throws no error on malformed, but still valid URI)
* @access public
*/
constructor(uri: string) {
if (!uri) {
throw new TypeError("uri must be defined for DirectoryEntryReference");
}
else {
super("DIR", []);
this.uri = uri;
this.reqContentValue = true;
}
}
/**
* Returns the href form of the stored URI
*
* @author Mark Stenglein <mark@stengle.in>
* @since 0.1.0
* @returns String href of stored URI
* @access public
*/
get uri(): string {
return this._uri.href;
}
/**
* Allows you to set the URI to ANY valid URI.
*
* - This method does NOT make any assumptions over what you meant to say,
* if by happenstance your input string can be interpreted as a valid URI,
* lib-ical will not complain.
*
* @author Mark Stenglein <mark@stengle.in>
* @since 0.1.0
* @param newUri string value of the new URI which is to be validated
* @returns void
* @access public
*/
set uri(newUri: string) {
this._uri = new URL(newUri);
this.paramValues = [`"${this._uri}"`];
}
}
......@@ -26,5 +26,67 @@ describe("DirectoryEntryReference:", () => {
it("Should exist", () => {
expect(DirectoryEntryReference).to.exist;
});
describe("constructor:", () => {
it("should create an object", () => {
const param = new DirectoryEntryReference("ldap://valid.com:444");
expect(param).to.exist;
});
it("should complain on empty uri", () => {
expect(() => {
const param = new DirectoryEntryReference("");
}).to.throw("uri must be defined for DirectoryEntryReference");
});
});
describe("get/set methods:", () => {
describe("uri:", () => {
it("should properly set the URI and give the same return", () => {
const URI = "ldap://me@example.com:6666/0=ABC%20Industries,c=US???"
+ "(cn=Jim%20Dolittle)";
const param = new DirectoryEntryReference(URI);
param.uri = URI;
expect(param.uri).to.be.equal(URI);
});
describe("throw exception on invalid uri:", () => {
it("leading dash", () => {
expect(() => {
const URI = "-invalid://example.com:66/t/e/s/t";
const param = new DirectoryEntryReference(URI);
}).to.throw("Invalid URL");
});
it("space", () => {
expect(() => {
const URI = "in valid://example.com:66/t/e/s/t";
const param = new DirectoryEntryReference(URI);
}).to.throw("Invalid URL");
});
});
});
});
describe("generator:", () => {
it("generates properly", () => {
const URI = "ldap://example.com:6666/o=ABC%20Industries,c=US???"
+ "(cn=Jim%20Dolittle)";
const param = new DirectoryEntryReference(URI);
expect(param.generate()).to.equal(`DIR="${URI}"`);
});
it("escapes spaces and other special chars", () => {
const URI = "http://example.com/test/with/a space/inside";
const param = new DirectoryEntryReference(URI);
const expectedURI = "http://example.com/test/with/a%20space/inside";
expect(param.generate()).to.equal(`DIR="${expectedURI}"`);
});
});
});
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment