-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
106 additions
and
92 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
# Represents the author of a comment. | ||
export class Author | ||
|
||
# Creates a new author. | ||
constructor: (options = {}) -> | ||
|
||
# The author's mail address. If you set it to `"[email protected]"`, Akismet will always return `true`. | ||
@email = options.email ? "" | ||
|
||
# The author's IP address. | ||
@ipAddress = options.ipAddress ? "" | ||
|
||
# The author's name. If you set it to `"viagra-test-123"`, Akismet will always return `true`. | ||
@name = options.name ? "" | ||
|
||
# The author's role. If you set it to `"administrator"`, Akismet will always return `false`. | ||
@role = options.role ? "" | ||
|
||
# The URL of the author's website. | ||
@url = if options.url then new URL options.url else null | ||
|
||
# The author's user agent, that is the string identifying the Web browser used to submit comments. | ||
@userAgent = options.userAgent ? "" | ||
|
||
# Creates a new author from the specified JSON object. | ||
@fromJson: (json) -> new @ | ||
email: if typeof json.comment_author_email == "string" then json.comment_author_email else "" | ||
ipAddress: if typeof json.user_ip == "string" then json.user_ip else "" | ||
name: if typeof json.comment_author == "string" then json.comment_author else "" | ||
role: if typeof json.user_role == "string" then json.user_role else "" | ||
url: if typeof json.comment_author_url == "string" then json.comment_author_url else "" | ||
userAgent: if typeof json.user_agent == "string" then json.user_agent else "" | ||
|
||
# Returns a JSON representation of this object. | ||
toJSON: -> | ||
map = user_ip: @ipAddress | ||
map.comment_author_email = @email if @email | ||
map.comment_author = @name if @name | ||
map.user_role = @role if @role | ||
map.comment_author_url = @url.href if @url | ||
map.user_agent = @userAgent if @userAgent | ||
map | ||
|
||
# Specifies the role of an author. | ||
export AuthorRole = Object.freeze | ||
|
||
# The author is an administrator. | ||
administrator: "administrator" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import {Author, AuthorRole} from "@cedx/akismet" | ||
import {equal, ok} from "node:assert/strict" | ||
import {describe, it} from "node:test" | ||
|
||
# Tests the features of the `Author` class. | ||
describe "Author", -> | ||
describe "fromJson()", -> | ||
it "should return an empty instance with an empty map", -> | ||
author = Author.fromJson {} | ||
equal author.email.length, 0 | ||
equal author.ipAddress.length, 0 | ||
equal author.name.length, 0 | ||
equal author.role.length, 0 | ||
equal author.url, null | ||
equal author.userAgent.length, 0 | ||
|
||
it "should return an initialized instance with a non-empty map", -> | ||
author = Author.fromJson | ||
comment_author: "Cédric Belin" | ||
comment_author_email: "[email protected]" | ||
comment_author_url: "https://belin.io" | ||
user_agent: "Mozilla/5.0" | ||
user_ip: "127.0.0.1" | ||
user_role: "administrator" | ||
|
||
equal author.email, "[email protected]" | ||
equal author.ipAddress, "127.0.0.1" | ||
equal author.role, AuthorRole.administrator | ||
ok author.url instanceof URL | ||
equal author.url.href, "https://belin.io/" | ||
equal author.userAgent, "Mozilla/5.0" | ||
|
||
describe "toJSON()", -> | ||
it "should return only the IP address with a newly created instance", -> | ||
json = new Author(ipAddress: "127.0.0.1").toJSON() | ||
equal Object.keys(json).length, 1 | ||
equal json.user_ip, "127.0.0.1" | ||
|
||
it "should return a non-empty map with an initialized instance", -> | ||
json = new Author( | ||
email: "[email protected]" | ||
ipAddress: "192.168.0.1" | ||
name: "Cédric Belin" | ||
url: "https://belin.io" | ||
userAgent: "Mozilla/5.0" | ||
).toJSON() | ||
|
||
equal Object.keys(json).length, 5 | ||
equal json.comment_author, "Cédric Belin" | ||
equal json.comment_author_email, "[email protected]" | ||
equal json.comment_author_url, "https://belin.io/" | ||
equal json.user_agent, "Mozilla/5.0" | ||
equal json.user_ip, "192.168.0.1" |
This file was deleted.
Oops, something went wrong.