1 | var should = require('should')
|
2 | , url = require('url')
|
3 | , nixt = require("nixt")
|
4 | , gravatar = require('../lib/gravatar')
|
5 |
|
6 | describe('gravatar', function() {
|
7 | var baseNoProtocolURL = "//www.gravatar.com/avatar/";
|
8 | var baseUnsecureURL = "http://www.gravatar.com/avatar/";
|
9 | var baseSecureURL = "https://s.gravatar.com/avatar/";
|
10 | var profileURL = "http://www.gravatar.com/";
|
11 | var profileSecureURL = "https://secure.gravatar.com/";
|
12 | var unspecifiedHash = 'd415f0e30c471dfdd9bc4f827329ef48';
|
13 |
|
14 | it('should gererate correct uri given an email', function() {
|
15 | gravatar.url('emerleite@gmail.com').should.be.equal(baseNoProtocolURL + "93e9084aa289b7f1f5e4ab6716a56c3b");
|
16 | gravatar.url('emerleite@yahoo.com.br').should.be.equal(baseNoProtocolURL + "6c47672b0d58bd6aae4fa70920cb3ee4");
|
17 | gravatar.url('93E9084AA289B7F1F5E4AB6716A56C3B@gmail.com').should.be.equal(baseNoProtocolURL + "45503aaa7bc259c0ef5bba9997b77875");
|
18 | });
|
19 |
|
20 | it('should generate same uri ignoring case', function() {
|
21 | gravatar.url('EMERLEITE@gmAil.com').should.be.equal(baseNoProtocolURL + "93e9084aa289b7f1f5e4ab6716a56c3b");
|
22 | gravatar.url('emerleite@YAHOO.com.BR').should.be.equal(baseNoProtocolURL + "6c47672b0d58bd6aae4fa70920cb3ee4");
|
23 | });
|
24 |
|
25 | it('should detect MD5 hashes and not hash them again', function() {
|
26 | gravatar.url('93e9084aa289b7f1f5e4ab6716a56c3b').should.be.equal(baseNoProtocolURL + "93e9084aa289b7f1f5e4ab6716a56c3b");
|
27 | gravatar.url('93E9084AA289B7F1F5E4AB6716A56C3B').should.be.equal(baseNoProtocolURL + "93e9084aa289b7f1f5e4ab6716a56c3b");
|
28 | });
|
29 |
|
30 | it('should generate uri with user passed parameters', function() {
|
31 | var gravatarURL = gravatar.url('emerleite@gmail.com', { s: '200', f: 'y', r: 'g', d: '404'});
|
32 | var queryString = url.parse(gravatarURL, true).query;
|
33 | queryString.s.should.equal('200');
|
34 | queryString.f.should.equal('y');
|
35 | queryString.r.should.equal('g');
|
36 | queryString.d.should.equal('404');
|
37 | });
|
38 |
|
39 | it('should force http protocol on gravatar uri generation', function() {
|
40 | gravatar.url('emerleite@gmail.com', {}, false).should.be.equal(baseUnsecureURL + "93e9084aa289b7f1f5e4ab6716a56c3b");
|
41 | gravatar.url('emerleite@yahoo.com.br', {}, false).should.be.equal(baseUnsecureURL + "6c47672b0d58bd6aae4fa70920cb3ee4");
|
42 | });
|
43 |
|
44 | it('should force https protocol on gravatar uri generation', function() {
|
45 | var gravatarURL = gravatar.url('emerleite@gmail.com', {}, true);
|
46 | gravatar.url('emerleite@gmail.com', {}, true).should.equal(baseSecureURL + "93e9084aa289b7f1f5e4ab6716a56c3b");
|
47 | });
|
48 |
|
49 | it('should handle falsey values for the email property', function () {
|
50 | gravatar.url(null).should.be.ok;
|
51 | gravatar.url(undefined).should.be.ok;
|
52 | gravatar.url('').should.be.ok;
|
53 | });
|
54 |
|
55 | it('should handle non string values for the email property', function () {
|
56 | gravatar.url({}, {}, true).should.equal(baseSecureURL + unspecifiedHash);
|
57 | gravatar.url(3, {}, true).should.equal(baseSecureURL + unspecifiedHash);
|
58 | gravatar.url(true, {}, true).should.equal(baseSecureURL + unspecifiedHash);
|
59 | });
|
60 |
|
61 | it('should generate profile url', function() {
|
62 | gravatar.profile_url('emerleite@gmail.com', {}, true).should.equal(profileSecureURL + "93e9084aa289b7f1f5e4ab6716a56c3b.json");
|
63 | gravatar.profile_url('emerleite@gmail.com', {format:'xml'}, true).should.equal(profileSecureURL + "93e9084aa289b7f1f5e4ab6716a56c3b.xml");
|
64 | gravatar.profile_url('emerleite@gmail.com', {format:'qr'}, true).should.equal(profileSecureURL + "93e9084aa289b7f1f5e4ab6716a56c3b.qr");
|
65 | gravatar.profile_url('emerleite@gmail.com').should.equal(profileURL + "93e9084aa289b7f1f5e4ab6716a56c3b.json");
|
66 | });
|
67 |
|
68 | it('should generate unspecified profile url when email is null', function() {
|
69 | gravatar.profile_url(null, {}, true).should.equal(profileSecureURL + unspecifiedHash + ".json");
|
70 | gravatar.profile_url(undefined, {}, true).should.equal(profileSecureURL + unspecifiedHash + ".json");
|
71 | });
|
72 |
|
73 | it('should force http protocol on gravatar uri generation via options', function() {
|
74 | gravatar.url('emerleite@gmail.com', {protocol: 'http'}).should.be.equal(baseUnsecureURL + "93e9084aa289b7f1f5e4ab6716a56c3b");
|
75 | gravatar.url('emerleite@yahoo.com.br', {protocol: 'http'}).should.be.equal(baseUnsecureURL + "6c47672b0d58bd6aae4fa70920cb3ee4");
|
76 | });
|
77 |
|
78 | it('should force https protocol on gravatar uri generation via options', function() {
|
79 | gravatar.url('emerleite@gmail.com', {protocol: 'https'}).should.equal(baseSecureURL + "93e9084aa289b7f1f5e4ab6716a56c3b");
|
80 | });
|
81 |
|
82 | it('should generate uri with user passed parameters and protocol in options', function() {
|
83 | var gravatarURL = gravatar.url('emerleite@gmail.com', {protocol: 'https', s: '200', f: 'y', r: 'g', d: '404'});
|
84 | var queryString = url.parse(gravatarURL, true).query;
|
85 | queryString.s.should.equal('200');
|
86 | queryString.f.should.equal('y');
|
87 | queryString.r.should.equal('g');
|
88 | queryString.d.should.equal('404');
|
89 | });
|
90 |
|
91 | it('should generate profile url with protocol in options', function() {
|
92 | gravatar.profile_url('emerleite@gmail.com', {protocol: 'https'}).should.equal(profileSecureURL + "93e9084aa289b7f1f5e4ab6716a56c3b.json");
|
93 | gravatar.profile_url('emerleite@gmail.com', {protocol: 'https', format:'xml'}).should.equal(profileSecureURL + "93e9084aa289b7f1f5e4ab6716a56c3b.xml");
|
94 | gravatar.profile_url('emerleite@gmail.com', {protocol: 'http', format:'qr'}).should.equal(profileURL + "93e9084aa289b7f1f5e4ab6716a56c3b.qr");
|
95 | gravatar.profile_url('emerleite@gmail.com').should.equal(profileURL + "93e9084aa289b7f1f5e4ab6716a56c3b.json");
|
96 | });
|
97 |
|
98 | it('should generate profile url with cdn in options', function() {
|
99 | var cdn = 'http://cdn-gravatar.wuweixing.com'
|
100 | gravatar.profile_url('emerleite@gmail.com', {cdn: cdn}).should.equal(cdn + "/93e9084aa289b7f1f5e4ab6716a56c3b.json");
|
101 | gravatar.profile_url('emerleite@gmail.com', {cdn: cdn, format:'xml'}).should.equal(cdn + "/93e9084aa289b7f1f5e4ab6716a56c3b.xml");
|
102 | gravatar.profile_url('emerleite@gmail.com', {cdn: cdn, format:'qr'}).should.equal(cdn + "/93e9084aa289b7f1f5e4ab6716a56c3b.qr");
|
103 | gravatar.url({}, {cdn: cdn}, true).should.equal(cdn + '/avatar/'+ unspecifiedHash);
|
104 | gravatar.url(3, {cdn: cdn}, true).should.equal(cdn + '/avatar/'+ unspecifiedHash);
|
105 | gravatar.url(true, {cdn: cdn}, true).should.equal(cdn + '/avatar/' + unspecifiedHash);
|
106 | });
|
107 |
|
108 | });
|
109 |
|
110 | describe("CLI", function() {
|
111 |
|
112 | describe("general: ", function() {
|
113 |
|
114 | it("accepts an email argument with options and writes gravatar URL to STDOUT", function(done) {
|
115 | nixt()
|
116 | .run('node ./cli.js zeke@sikelianos.com -p https -s 500 -d retro')
|
117 | .stdout('Gravatar (avatar):\nhttps://s.gravatar.com/avatar/8f344b1c4bdcfc28bd848e97e94c3523?default=retro&size=500')
|
118 | .end(done)
|
119 | })
|
120 | it("outputs usage if -h arg is present", function(done) {
|
121 | nixt()
|
122 | .run('node ./cli.js -h')
|
123 | .stdout(/Usage/)
|
124 | .end(done)
|
125 | })
|
126 |
|
127 | })
|
128 |
|
129 | describe("avatar command: ", function() {
|
130 |
|
131 | it("accepts an email argument with options and writes gravatar URL to STDOUT", function(done) {
|
132 | nixt()
|
133 | .run('node ./cli.js avatar zeke@sikelianos.com -p https -s 500 -d retro')
|
134 | .stdout('Gravatar (avatar):\nhttps://s.gravatar.com/avatar/8f344b1c4bdcfc28bd848e97e94c3523?default=retro&size=500')
|
135 | .end(done)
|
136 | })
|
137 |
|
138 | })
|
139 |
|
140 | describe("profile command: ", function() {
|
141 |
|
142 | it("accepts an email argument with options and writes gravatar profile URL to STDOUT", function(done) {
|
143 | nixt()
|
144 | .run('node ./cli.js profile zeke@sikelianos.com -p https -c doSomething')
|
145 | .stdout('Gravatar (profile):\nhttps://secure.gravatar.com/8f344b1c4bdcfc28bd848e97e94c3523.json?callback=doSomething')
|
146 | .end(done)
|
147 | })
|
148 |
|
149 | })
|
150 |
|
151 | })
|