link_preview.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. process.env.NODE_ENV = 'test';
  2. let chai = require('chai');
  3. let server = require('../server');
  4. let should = chai.should();
  5. let expect = chai.expect;
  6. let getOGPreviewData = require('../lib/linkPreview/getOGPreviewData');
  7. let getPreviewHTML = require('../lib/linkPreview/getPreviewHTML');
  8. let linkPreview = require('../lib/linkPreview');
  9. let github = require('../lib/linkPreview/patterns/github');
  10. let wikipedia = require('../lib/linkPreview/patterns/wikipedia');
  11. let twitter = require('../lib/linkPreview/patterns/twitter');
  12. const Errors = require('../lib/errors.js');
  13. chai.use(require('chai-http'));
  14. chai.use(require('chai-things'));
  15. describe('link_expansion', () => {
  16. //Wait for app to start before commencing
  17. before((done) => {
  18. if(server.locals.appStarted) done();
  19. server.on('appStarted', done);
  20. });
  21. describe('getOGPreviewData', () => {
  22. it('should return an object containing relevant OG data', async () => {
  23. let data = await getOGPreviewData('https://www.theguardian.com/news/2018/mar/17/cambridge-analytica-facebook-influence-us-election')
  24. data.should.have.property(
  25. 'title',
  26. 'Revealed: 50 million Facebook profiles harvested for Cambridge Analytica in major data breach'
  27. );
  28. data.should.have.property(
  29. 'description',
  30. 'Whistleblower describes how firm linked to former Trump adviser Steve Bannon compiled user data to target American voters• How Cambridge Analytica’s algorithms turned ‘likes’ into a political tool'
  31. );
  32. data.should.have.property(
  33. 'url',
  34. 'http://www.theguardian.com/news/2018/mar/17/cambridge-analytica-facebook-influence-us-election'
  35. );
  36. data.should.have.property(
  37. 'image',
  38. 'https://i.guim.co.uk/img/media/97532076a6935a1e79eba294437ed91f3eb4df6b/0_626_4480_2688/master/4480.jpg?w=1200&h=630&q=55&auto=format&usm=12&fit=crop&crop=faces%2Centropy&bm=normal&ba=bottom%2Cleft&blend64=aHR0cHM6Ly91cGxvYWRzLmd1aW0uY28udWsvMjAxOC8wMS8zMS9mYWNlYm9va19kZWZhdWx0LnBuZw&s=365825fe053733ae12f9b050f5374594'
  39. );
  40. });
  41. it('should use other meta or title tags if there is no OG tags availible', async () => {
  42. let data = await getOGPreviewData('http://ejs.co');
  43. data.should.have.property('title', 'EJS -- Embedded JavaScript templates');
  44. data.should.have.property(
  45. 'description',
  46. "'E' is for 'effective'. EJS is a simple templating language that lets you generate HTML markup with plain JavaScript. No religiousness about how to organize things. No reinvention of iteration and control-flow. It's just plain JavaScript."
  47. );
  48. });
  49. it('should return null if there is no OG tags availible', async () => {
  50. let data = await getOGPreviewData('http://blank.org');
  51. expect(data).to.be.null;
  52. });
  53. });
  54. describe('getPreviewHTML', () => {
  55. it('should return an HTML string for given object', () => {
  56. let HTML = getPreviewHTML({
  57. url: 'http://www.example.com',
  58. description: 'description',
  59. title: 'title',
  60. image: 'image'
  61. });
  62. (typeof HTML).should.equal('string');
  63. })
  64. it('should correctly deal with the conditional', () => {
  65. let HTML = getPreviewHTML({
  66. url: 'http://www.example.com',
  67. description: 'description',
  68. title: 'title'
  69. });
  70. (typeof HTML).should.equal('string');
  71. })
  72. });
  73. describe('linkPreview', () => {
  74. it('should get a HTML string from an OG link', async () => {
  75. let HTML = await linkPreview('https://www.theguardian.com/news/2018/mar/17/cambridge-analytica-facebook-influence-us-election');
  76. (typeof HTML).should.equal('string');
  77. });
  78. it('should return an empty string from an invalid site', async () => {
  79. let HTML = await linkPreview('http://blank.org');
  80. (typeof HTML).should.equal('string');
  81. });
  82. });
  83. describe('GitHub', () => {
  84. it('should match a valid GitHub url', () => {
  85. github.matches('https://github.com/sbkwgh/forum').should.not.be.null;
  86. github.matches('http://github.com/sbkwgh/forum').should.not.be.null;
  87. expect(github.matches('http://notgithub.com/sbkwgh/forum')).to.be.null;
  88. });
  89. it('should return a data object', async () => {
  90. let data = await github.getPreviewData('https://github.com/sbkwgh/forum');
  91. data.should.have.property('title', 'sbkwgh/forum')
  92. data.should.have.property('url', 'https://github.com/sbkwgh/forum')
  93. data.should.have.property('description', 'Forum software created using Express, Vue, and Sequelize')
  94. });
  95. });
  96. describe('Wikipedia', () => {
  97. it('should match a valid Wikipedia url', () => {
  98. wikipedia.matches('https://en.wikipedia.org/wiki/google').should.not.be.null;
  99. wikipedia.matches('http://fr.wikipedia.org/wiki/google').should.not.be.null;
  100. expect(wikipedia.matches('http://en.wikipedia.org/notapage')).to.be.null;
  101. });
  102. it('should return a data object', async () => {
  103. let data = await wikipedia.getPreviewData('https://en.wikipedia.org/wiki/google');
  104. data.should.have.property('title', 'Google')
  105. data.should.have.property('url', 'https://en.wikipedia.org/wiki/Google')
  106. data.description.should.have.length(503)
  107. });
  108. });
  109. describe('Twitter', () => {
  110. it('should match a valid Wikipedia url', () => {
  111. twitter.matches('https://twitter.com/user/status/12345').should.not.be.null;
  112. expect(twitter.matches('http://twitter.com/notapage/123456')).to.be.null;
  113. expect(twitter.matches('http://twitter.com/notapage/status/qwertyu')).to.be.null;
  114. });
  115. it('should return a data object', async () => {
  116. let HTML = await twitter.getPreviewData('https://twitter.com/Interior/status/463440424141459456');
  117. (typeof HTML).should.equal('string');
  118. HTML.should.have.length.above(0);
  119. });
  120. });
  121. })