From 1bf98318c8c059f0ce31c8d5e3135fa9f92553da Mon Sep 17 00:00:00 2001
From: joshua stein
Date: Sat, 30 Jun 2012 17:41:14 -0500
Subject: [PATCH] write some basic tests
---
spec/fixtures/story_pages/1.html | 77 +++++++++++++++++++++++++++
spec/fixtures/story_pages/2.html | 19 +++++++
spec/models/story_spec.rb | 91 ++++++++++++++++++++++++++++++++
spec/models/user_spec.rb | 26 +++++++++
spec/support/blueprints.rb | 12 +++++
5 files changed, 225 insertions(+)
create mode 100644 spec/fixtures/story_pages/1.html
create mode 100644 spec/fixtures/story_pages/2.html
create mode 100644 spec/models/story_spec.rb
create mode 100644 spec/models/user_spec.rb
diff --git a/spec/fixtures/story_pages/1.html b/spec/fixtures/story_pages/1.html
new file mode 100644
index 0000000..128cf07
--- /dev/null
+++ b/spec/fixtures/story_pages/1.html
@@ -0,0 +1,77 @@
+
+
+
+
+
+
+
+
+
+
+
+
+B2G demo & quick hack // by Paul Rouget
+
+
+
+ Very very quick demo of B2G followed by a quick hack of the homescreen:
+VIDEOYoutube video.
+
+You can find the code of the UI of B2g (gaia) on github . Reminder:
+
+
+
+ An Operating System;
+ Linux Kernel + drivers from Android (so B2G can run on most of the phones that are capable of running Android). Works on ARMv7, will probably work on ARMv6;
+ Gecko on top of it. The same version you'll find on a regular Firefox;
+ Apps are written in HTML, JavaScript and CSS;
+ You don't need to be online to use the Apps ;
+ A set of new JavaScript APIs:
+
+ WebAPIs , for bluetooth, sms, telephony, …
+ WebRTC , for streaming, webcam, P2P, …
+
+ We are working on standardizing these APIs ;
+ The homescreen/launcher is an HTML page. Mozilla's version is named Gaia ;
+ You can build your own homescreen/launcher;
+ The name of the project will probably change;
+ It's possible to implement Boot To Webkit too;
+ It's Open Source;
+ We (Mozilla) are working on a MarketPlace .
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/spec/fixtures/story_pages/2.html b/spec/fixtures/story_pages/2.html
new file mode 100644
index 0000000..22b9d1b
--- /dev/null
+++ b/spec/fixtures/story_pages/2.html
@@ -0,0 +1,19 @@
+Google
\ No newline at end of file
diff --git a/spec/models/story_spec.rb b/spec/models/story_spec.rb
new file mode 100644
index 0000000..937c0ab
--- /dev/null
+++ b/spec/models/story_spec.rb
@@ -0,0 +1,91 @@
+require "spec_helper"
+
+describe Story do
+ it "should get a short id" do
+ s = Story.make!(:title => "hello", :url => "http://example.com/")
+
+ s.short_id.should match(/^\A[a-zA-Z0-9]{1,10}\z/)
+ end
+
+ it "requires a url or a description" do
+ expect { Story.make!(:title => "hello", :url => "",
+ :description => "") }.to raise_error
+
+ expect { Story.make!(:title => "hello", :description => "hi", :url => nil)
+ }.to_not raise_error
+
+ expect { Story.make!(:title => "hello", :url => "http://ex.com/",
+ :description => nil) }.to_not raise_error
+ end
+
+ it "does not allow too-short titles" do
+ expect { Story.make!(:title => "") }.to raise_error
+ expect { Story.make!(:title => "hi") }.to raise_error
+ expect { Story.make!(:title => "hello") }.to_not raise_error
+ end
+
+ it "does not allow too-long titles" do
+ expect { Story.make!(:title => ("hello" * 100)) }.to raise_error
+ end
+
+ it "checks for invalid urls" do
+ expect { Story.make!(:title => "test", :url => "http://gooses.com/")
+ }.to_not raise_error
+
+ expect { Story.make!(:title => "test", url => "ftp://gooses/")
+ }.to raise_error
+ end
+
+ it "checks for a previously posted story with same url" do
+ Story.count.should == 0
+
+ Story.make!(:title => "flim flam", :url => "http://example.com/")
+ Story.count.should == 1
+
+ expect { Story.make!(:title => "flim flam 2",
+ :url => "http://example.com/") }.to raise_error
+
+ Story.count.should == 1
+ end
+
+ it "parses domain properly" do
+ s = Story.make!(:url => "http://example.com")
+ s.domain.should == "example.com"
+
+ s = Story.make!(:url => "http://www3.example.com")
+ s.domain.should == "example.com"
+
+ s = Story.make!(:url => "http://flub.example.com")
+ s.domain.should == "flub.example.com"
+ end
+
+ it "converts a title to a url properly" do
+ s = Story.make!(:title => "Hello there, this is a title")
+ s.title_as_url.should == "hello_there_this_is_a_title"
+
+ s = Story.make!(:title => "Hello _ underscore")
+ s.title_as_url.should == "hello_underscore"
+ end
+
+ it "is not editable by another non-admin user" do
+ u = User.make!
+
+ s = Story.make!(:user_id => u.id)
+ s.is_editable_by_user?(u).should == true
+
+ u = User.make!
+ s.is_editable_by_user?(u).should == false
+ end
+
+ it "can fetch its title properly" do
+ s = Story.make
+ s.fetched_content = File.read(Rails.root +
+ "spec/fixtures/story_pages/1.html")
+ s.fetched_title.should == "B2G demo & quick hack // by Paul Rouget"
+
+ s = Story.make
+ s.fetched_content = File.read(Rails.root +
+ "spec/fixtures/story_pages/2.html")
+ s.fetched_title.should == "Google"
+ end
+end
diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb
new file mode 100644
index 0000000..0b5f63e
--- /dev/null
+++ b/spec/models/user_spec.rb
@@ -0,0 +1,26 @@
+require "spec_helper"
+
+describe User do
+ it "has a valid username" do
+ expect { User.make!(:username => nil) }.to raise_error
+ expect { User.make!(:username => "") }.to raise_error
+ expect { User.make!(:username => "*") }.to raise_error
+
+ User.make!(:username => "test")
+ expect { User.make!(:username => "test") }.to raise_error
+ end
+
+ it "has a valid email address" do
+ User.make!(:email => "user@example.com")
+ expect { User.make!(:email => "user@example.com") }.to raise_error
+ end
+
+ it "authenticates properly" do
+ u = User.make!(:password => "pilgrim")
+
+ u.password_digest.length.should > 20
+
+ u.authenticate("pilgrim").should == u
+ u.authenticate("pilgriM").should == false
+ end
+end
diff --git a/spec/support/blueprints.rb b/spec/support/blueprints.rb
index 2c5efe3..36ab12e 100644
--- a/spec/support/blueprints.rb
+++ b/spec/support/blueprints.rb
@@ -4,4 +4,16 @@ User.blueprint do
email { "user-#{sn}@example.com" }
password { "blah blah" }
password_confirmation { object.password }
+ username { "username#{sn}" }
+end
+
+Story.blueprint do
+ user_id { User.make }
+ title { "story title #{sn}" }
+ url { "http://example.com/#{sn}" }
+end
+
+Tag.blueprint do
+ tag { "tag-#{sn}" }
+ description { "tag #{sn}" }
end