Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tests #47

Merged
merged 5 commits into from
Jan 17, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ _site
*.gem
.env
/Gemfile.lock
tmp
9 changes: 9 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,2 +1,11 @@
language: ruby
script: "./script/cibuild"

sudo: false
cache: bundler

env:
global:
- GITHUB_CLIENT_ID=FOO
- GITHUB_CLIENT_SECRET=BAR
- GITHUB_ORG_ID="balter-test-org"
24 changes: 17 additions & 7 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -1,9 +1,19 @@
# This file is auto-generated by Jekyll Auth
# Feel free to add additional Rake tasks so long as
# `rake assets:precompile` continues to generate the jekyll site
require 'rubygems/package_task'
require 'rubygems/specification'
require 'bundler'
require 'fileutils'
require 'dotenv'

namespace :assets do
task :precompile do
sh "bundle exec jekyll-auth build"
end
task :default => [:spec]

task :site do
Dotenv.load
FileUtils.chdir "templates"
`bundle exec jekyll-auth`
end

require 'rspec/core/rake_task'
desc "Run specs"
RSpec::Core::RakeTask.new do |t|
t.pattern = 'spec/**/*_spec.rb'
end
17 changes: 0 additions & 17 deletions _config.yml

This file was deleted.

2 changes: 1 addition & 1 deletion bin/jekyll-auth
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ command :new do |c|
c.syntax = 'jekyll-auth new'
c.description = "Initialize an existing Jekyll site as a Jekyll Auth site"
c.action do |args, options|
source = File.expand_path( "../", File.dirname(__FILE__) )
source = File.expand_path( "../templates", File.dirname(__FILE__) )
destination = Dir.pwd
say "Initiating new Jekyll Auth site in #{destination}"

Expand Down
14 changes: 10 additions & 4 deletions jekyll-auth.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,24 @@ Gem::Specification.new do |s|
s.email = "[email protected]"
s.homepage = "https://github.com/benbalter/jekyll-auth"
s.license = "MIT"
s.files = ["lib/jekyll-auth.rb", "bin/jekyll-auth", "config.ru", "Rakefile",
"lib/jekyll-auth/auth-site.rb", "lib/jekyll-auth/jekyll-site.rb",
"lib/jekyll-auth/version.rb", ".gitignore"]
s.executables = ["jekyll-auth"]
s.files = `git ls-files`.split("\n")
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
s.require_paths = ["lib"]

s.add_dependency("jekyll", "~> 2.0")
s.add_dependency("sinatra-index", "~> 0.0")
s.add_dependency("sinatra_auth_github", "~> 1.1")
s.add_dependency "rack", "1.5.2"

s.add_dependency("commander", "~> 4.1")
s.add_dependency("git", "~> 1.2")
s.add_dependency("dotenv", "~> 1.0")
s.add_dependency("rake", "~> 10.3")
s.add_dependency("rack-ssl-enforcer", "~> 0.2")
s.add_runtime_dependency('safe_yaml', "~> 1.0")
s.add_development_dependency "rspec"
s.add_development_dependency "rack-test"
s.add_development_dependency "webmock"

end
4 changes: 2 additions & 2 deletions lib/jekyll-auth.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ def self.site
run JekyllAuth::JekyllSite
end
end

def self.config_file
File.join(Dir.pwd, "_config.yml")
end

def self.config
@config ||= begin
config = YAML.safe_load_file(config_file)
config["jekyll_auth"]
config["jekyll_auth"] || {}
rescue
{}
end
Expand Down
5 changes: 5 additions & 0 deletions lib/jekyll-auth/auth-site.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@ class AuthSite < Sinatra::Base
ENV['WARDEN_GITHUB_VERIFIER_SECRET'] ||= SecureRandom.hex
register Sinatra::Auth::Github

use Rack::Logger

def whitelisted?
return true if request.path_info == "/logout"
JekyllAuth.whitelist && JekyllAuth.whitelist.match(request.path_info)
end

Expand All @@ -35,6 +38,8 @@ def authentication_strategy
before do
pass if whitelisted?

logger.info "Authentication strategy: #{authentication_strategy}"

case authentication_strategy
when :team
github_team_authenticate! ENV['GITHUB_TEAM_ID']
Expand Down
2 changes: 1 addition & 1 deletion lib/jekyll-auth/jekyll-site.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
class JekyllAuth
class JekyllSite < Sinatra::Base
register Sinatra::Index
set :public_folder, '_site'
set :public_folder, File.expand_path('_site', Dir.pwd)
use_static_index 'index.html'
end
end
4 changes: 1 addition & 3 deletions script/cibuild
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@

set -e

script/bootstrap

bundle exec rake spec
bundle exec gem build jekyll-auth.gemspec

bundle exec jekyll-auth --version
63 changes: 63 additions & 0 deletions spec/auth_site_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
require "spec_helper"

describe "logged in user" do
include Rack::Test::Methods

def app
JekyllAuth.site
end

before(:each) do
setup_tmp_dir
@user = make_user('login' => 'benbaltertest')
login_as @user

stub_request(:get, "https://api.github.com/orgs/#{ENV["GITHUB_ORG_ID"]}/members/benbaltertest").
to_return(:status => 200)
end

it "shows the securocat when github returns an oauth error" do
get "/auth/github/callback?error=redirect_uri_mismatch"
expect(last_response.body).to match(%r{securocat\.png})
end

it "logs the user out" do
get "/logout"
expect(last_response.status).to eql(302)
expect(last_response.headers['Location']).to eql("http://example.org/")

get "/"
expect(last_response.status).to eql(302)
expect(last_response.headers['Location']).to match(%r{^https://github\.com/login/oauth/authorize})
end

end

describe "logged out user" do

include Rack::Test::Methods

def app
JekyllAuth.site
end

it "doesn't let you view indexes" do
get "/"
expect(last_response.status).to eql(302)
expect(last_response.headers['Location']).to match(%r{^https://github\.com/login/oauth/authorize})

get "/some_dir"
expect(last_response.status).to eql(302)
expect(last_response.headers['Location']).to match(%r{^https://github\.com/login/oauth/authorize})
end

it "doesn't let you view files" do
get "/index.html"
expect(last_response.status).to eql(302)
expect(last_response.headers['Location']).to match(%r{^https://github\.com/login/oauth/authorize})

get "/some_dir/index.html"
expect(last_response.status).to eql(302)
expect(last_response.headers['Location']).to match(%r{^https://github\.com/login/oauth/authorize})
end
end
47 changes: 47 additions & 0 deletions spec/jekyll-auth_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
require "spec_helper"

describe "JekyllAuth" do

before(:each) do
setup_tmp_dir
JekyllAuth.instance_variable_set("@config",nil)
end

it "should know the config file path" do
expected = File.expand_path "tmp/_config.yml", base_dir
expect(JekyllAuth.config_file).to eql(expected)
end

it "should return a null hash if no config file exists" do
expect(JekyllAuth.config).to eql({})
end

it "should return a null hash if config file doesn't contain jekyll_auth" do
File.write(JekyllAuth.config_file, "foo: bar\n")
expect(JekyllAuth.config).to eql({})
end

it "should return the config hash if the config files contains jekyll_auth" do
File.write(JekyllAuth.config_file, "jekyll_auth:\n ssl: true\n whitelist:\n - drafts?\n")
expect(JekyllAuth.config).to eql({"ssl"=>true, "whitelist"=>["drafts?"]})
end

it "should disable ssl by default" do
expect(JekyllAuth.ssl?).to eql(false)
end

it "should know when ssl is requested" do
File.write(JekyllAuth.config_file, "jekyll_auth:\n ssl:true\n")
expect(JekyllAuth.ssl?).to eql(true)
end

it "should know when ssl is disabled" do
File.write(JekyllAuth.config_file, "jekyll_auth:\n ssl:false\n")
expect(JekyllAuth.ssl?).to eql(true)
end

it "should parse the whitelist" do
File.write(JekyllAuth.config_file, "jekyll_auth:\n whitelist:\n - drafts?\n")
expect(JekyllAuth.whitelist).to eql(/drafts?/)
end
end
31 changes: 31 additions & 0 deletions spec/jekyll_site_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
require "spec_helper"

describe "jekyll site" do
include Rack::Test::Methods

def app
JekyllAuth::JekyllSite
end

before do
setup_tmp_dir
File.write File.expand_path("_config.yml", tmp_dir), "foo: bar"
`bundle exec jekyll build`
end

it "serves the index" do
get "/"
expect(last_response.body).to eql("My awesome site")
end

it "serves a page" do
get "/index.html"
expect(last_response.body).to eql("My awesome site")
end

it "serves a directory index" do
get "/some_dir"
expect(last_response.body).to eql("My awesome directory")
end

end
40 changes: 40 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
require "bundler/setup"
require 'fileutils'

$:.push File.join(File.dirname(__FILE__), '..', 'lib')

require 'rack/test'
require 'sinatra/auth/github'
require 'sinatra/auth/github/test/test_helper'
require 'webmock/rspec'
require 'dotenv'

def base_dir
File.expand_path "../", File.dirname(__FILE__)
end

def tmp_dir
File.expand_path "tmp", base_dir
end

def tear_down_tmp_dir
FileUtils.rm_rf tmp_dir
end

def setup_tmp_dir
tear_down_tmp_dir
FileUtils.mkdir tmp_dir
File.write File.expand_path("index.html", tmp_dir), "My awesome site"
FileUtils.mkdir "#{tmp_dir}/some_dir"
File.write File.expand_path("some_dir/index.html", tmp_dir), "My awesome directory"
Dir.chdir tmp_dir
end

Dotenv.load
setup_tmp_dir

require_relative "../lib/jekyll-auth"

RSpec.configure do |config|
config.include(Sinatra::Auth::Github::Test::Helper)
end
3 changes: 3 additions & 0 deletions templates/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
_site
.env
/Gemfile.lock
9 changes: 9 additions & 0 deletions templates/Rakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# This file is auto-generated by Jekyll Auth
# Feel free to add additional Rake tasks so long as
# `rake assets:precompile` continues to generate the jekyll site

namespace :assets do
task :precompile do
sh "bundle exec jekyll-auth build"
end
end
File renamed without changes.
File renamed without changes.