Added an Authentications table.

Connects users to remote web services, as recommended at
http://asciicasts.com/episodes/235-omniauth-part-1

In addition, we store "token" and "secret" fields.
This commit is contained in:
Miles Gould
2013-04-04 18:48:54 +01:00
parent 2187972b47
commit 5ea91bf56a
22 changed files with 533 additions and 1 deletions

View File

@@ -0,0 +1,3 @@
# Place all the behaviors and hooks related to the matching controller here.
# All this logic will automatically be available in application.js.
# You can use CoffeeScript in this file: http://jashkenas.github.com/coffee-script/

View File

@@ -0,0 +1,83 @@
class AuthenticationsController < ApplicationController
# GET /authentications
# GET /authentications.json
def index
@authentications = Authentication.all
respond_to do |format|
format.html # index.html.erb
format.json { render json: @authentications }
end
end
# GET /authentications/1
# GET /authentications/1.json
def show
@authentication = Authentication.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.json { render json: @authentication }
end
end
# GET /authentications/new
# GET /authentications/new.json
def new
@authentication = Authentication.new
respond_to do |format|
format.html # new.html.erb
format.json { render json: @authentication }
end
end
# GET /authentications/1/edit
def edit
@authentication = Authentication.find(params[:id])
end
# POST /authentications
# POST /authentications.json
def create
@authentication = Authentication.new(params[:authentication])
respond_to do |format|
if @authentication.save
format.html { redirect_to @authentication, notice: 'Authentication was successfully created.' }
format.json { render json: @authentication, status: :created, location: @authentication }
else
format.html { render action: "new" }
format.json { render json: @authentication.errors, status: :unprocessable_entity }
end
end
end
# PUT /authentications/1
# PUT /authentications/1.json
def update
@authentication = Authentication.find(params[:id])
respond_to do |format|
if @authentication.update_attributes(params[:authentication])
format.html { redirect_to @authentication, notice: 'Authentication was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: "edit" }
format.json { render json: @authentication.errors, status: :unprocessable_entity }
end
end
end
# DELETE /authentications/1
# DELETE /authentications/1.json
def destroy
@authentication = Authentication.find(params[:id])
@authentication.destroy
respond_to do |format|
format.html { redirect_to authentications_url }
format.json { head :no_content }
end
end
end

View File

@@ -0,0 +1,2 @@
module AuthenticationsHelper
end

View File

@@ -0,0 +1,3 @@
class Authentication < ActiveRecord::Base
attr_accessible :member_id, :provider, :secret, :uid
end

View File

@@ -0,0 +1,22 @@
= form_for @authentication do |f|
- if @authentication.errors.any?
#error_explanation
%h2= "#{pluralize(@authentication.errors.count, "error")} prohibited this authentication from being saved:"
%ul
- @authentication.errors.full_messages.each do |msg|
%li= msg
.field
= f.label :member_id
= f.number_field :member_id
.field
= f.label :provider
= f.text_field :provider
.field
= f.label :uid
= f.text_field :uid
.field
= f.label :secret
= f.text_field :secret
.actions
= f.submit 'Save'

View File

@@ -0,0 +1,7 @@
%h1 Editing authentication
= render 'form'
= link_to 'Show', @authentication
\|
= link_to 'Back', authentications_path

View File

@@ -0,0 +1,25 @@
%h1 Listing authentications
%table
%tr
%th Member
%th Provider
%th Uid
%th Secret
%th
%th
%th
- @authentications.each do |authentication|
%tr
%td= authentication.member_id
%td= authentication.provider
%td= authentication.uid
%td= authentication.secret
%td= link_to 'Show', authentication
%td= link_to 'Edit', edit_authentication_path(authentication)
%td= link_to 'Destroy', authentication, :method => :delete, :data => { :confirm => 'Are you sure?' }
%br
= link_to 'New Authentication', new_authentication_path

View File

@@ -0,0 +1,5 @@
%h1 New authentication
= render 'form'
= link_to 'Back', authentications_path

View File

@@ -0,0 +1,18 @@
%p#notice= notice
%p
%b Member:
= @authentication.member_id
%p
%b Provider:
= @authentication.provider
%p
%b Uid:
= @authentication.uid
%p
%b Secret:
= @authentication.secret
= link_to 'Edit', edit_authentication_path(@authentication)
\|
= link_to 'Back', authentications_path

View File

@@ -2,6 +2,7 @@ Growstuff::Application.routes.draw do
devise_for :members, :controllers => { :registrations => "registrations" }
resources :authentications
resources :plantings
resources :gardens
resources :posts

View File

@@ -0,0 +1,13 @@
class CreateAuthentications < ActiveRecord::Migration
def change
create_table :authentications do |t|
t.integer :member_id, :null => false
t.string :provider, :null => false
t.string :uid
t.string :token
t.string :secret
t.timestamps
end
add_index :authentications, :member_id
end
end

View File

@@ -11,7 +11,19 @@
#
# It's strongly recommended to check this file into your version control system.
ActiveRecord::Schema.define(:version => 20130329045744) do
ActiveRecord::Schema.define(:version => 20130404174459) do
create_table "authentications", :force => true do |t|
t.integer "member_id", :null => false
t.string "provider", :null => false
t.string "uid"
t.string "token"
t.string "secret"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end
add_index "authentications", ["member_id"], :name => "index_authentications_on_member_id"
create_table "comments", :force => true do |t|
t.integer "post_id", :null => false

View File

@@ -0,0 +1,164 @@
require 'spec_helper'
# This spec was generated by rspec-rails when you ran the scaffold generator.
# It demonstrates how one might use RSpec to specify the controller code that
# was generated by Rails when you ran the scaffold generator.
#
# It assumes that the implementation code is generated by the rails scaffold
# generator. If you are using any extension libraries to generate different
# controller code, this generated spec may or may not pass.
#
# It only uses APIs available in rails and/or rspec-rails. There are a number
# of tools you can use to make these specs even more expressive, but we're
# sticking to rails and rspec-rails APIs to keep things simple and stable.
#
# Compared to earlier versions of this generator, there is very limited use of
# stubs and message expectations in this spec. Stubs are only used when there
# is no simpler way to get a handle on the object needed for the example.
# Message expectations are only used when there is no simpler way to specify
# that an instance is receiving a specific message.
describe AuthenticationsController do
# This should return the minimal set of attributes required to create a valid
# Authentication. As you add validations to Authentication, be sure to
# update the return value of this method accordingly.
def valid_attributes
{ "member_id" => "1" }
end
# This should return the minimal set of values that should be in the session
# in order to pass any filters (e.g. authentication) defined in
# AuthenticationsController. Be sure to keep this updated too.
def valid_session
{}
end
describe "GET index" do
it "assigns all authentications as @authentications" do
authentication = Authentication.create! valid_attributes
get :index, {}, valid_session
assigns(:authentications).should eq([authentication])
end
end
describe "GET show" do
it "assigns the requested authentication as @authentication" do
authentication = Authentication.create! valid_attributes
get :show, {:id => authentication.to_param}, valid_session
assigns(:authentication).should eq(authentication)
end
end
describe "GET new" do
it "assigns a new authentication as @authentication" do
get :new, {}, valid_session
assigns(:authentication).should be_a_new(Authentication)
end
end
describe "GET edit" do
it "assigns the requested authentication as @authentication" do
authentication = Authentication.create! valid_attributes
get :edit, {:id => authentication.to_param}, valid_session
assigns(:authentication).should eq(authentication)
end
end
describe "POST create" do
describe "with valid params" do
it "creates a new Authentication" do
expect {
post :create, {:authentication => valid_attributes}, valid_session
}.to change(Authentication, :count).by(1)
end
it "assigns a newly created authentication as @authentication" do
post :create, {:authentication => valid_attributes}, valid_session
assigns(:authentication).should be_a(Authentication)
assigns(:authentication).should be_persisted
end
it "redirects to the created authentication" do
post :create, {:authentication => valid_attributes}, valid_session
response.should redirect_to(Authentication.last)
end
end
describe "with invalid params" do
it "assigns a newly created but unsaved authentication as @authentication" do
# Trigger the behavior that occurs when invalid params are submitted
Authentication.any_instance.stub(:save).and_return(false)
post :create, {:authentication => { "member_id" => "invalid value" }}, valid_session
assigns(:authentication).should be_a_new(Authentication)
end
it "re-renders the 'new' template" do
# Trigger the behavior that occurs when invalid params are submitted
Authentication.any_instance.stub(:save).and_return(false)
post :create, {:authentication => { "member_id" => "invalid value" }}, valid_session
response.should render_template("new")
end
end
end
describe "PUT update" do
describe "with valid params" do
it "updates the requested authentication" do
authentication = Authentication.create! valid_attributes
# Assuming there are no other authentications in the database, this
# specifies that the Authentication created on the previous line
# receives the :update_attributes message with whatever params are
# submitted in the request.
Authentication.any_instance.should_receive(:update_attributes).with({ "member_id" => "1" })
put :update, {:id => authentication.to_param, :authentication => { "member_id" => "1" }}, valid_session
end
it "assigns the requested authentication as @authentication" do
authentication = Authentication.create! valid_attributes
put :update, {:id => authentication.to_param, :authentication => valid_attributes}, valid_session
assigns(:authentication).should eq(authentication)
end
it "redirects to the authentication" do
authentication = Authentication.create! valid_attributes
put :update, {:id => authentication.to_param, :authentication => valid_attributes}, valid_session
response.should redirect_to(authentication)
end
end
describe "with invalid params" do
it "assigns the authentication as @authentication" do
authentication = Authentication.create! valid_attributes
# Trigger the behavior that occurs when invalid params are submitted
Authentication.any_instance.stub(:save).and_return(false)
put :update, {:id => authentication.to_param, :authentication => { "member_id" => "invalid value" }}, valid_session
assigns(:authentication).should eq(authentication)
end
it "re-renders the 'edit' template" do
authentication = Authentication.create! valid_attributes
# Trigger the behavior that occurs when invalid params are submitted
Authentication.any_instance.stub(:save).and_return(false)
put :update, {:id => authentication.to_param, :authentication => { "member_id" => "invalid value" }}, valid_session
response.should render_template("edit")
end
end
end
describe "DELETE destroy" do
it "destroys the requested authentication" do
authentication = Authentication.create! valid_attributes
expect {
delete :destroy, {:id => authentication.to_param}, valid_session
}.to change(Authentication, :count).by(-1)
end
it "redirects to the authentications list" do
authentication = Authentication.create! valid_attributes
delete :destroy, {:id => authentication.to_param}, valid_session
response.should redirect_to(authentications_url)
end
end
end

View File

@@ -0,0 +1,10 @@
# Read about factories at https://github.com/thoughtbot/factory_girl
FactoryGirl.define do
factory :authentication do
member_id 1
provider "MyString"
uid "MyString"
secret "MyString"
end
end

View File

@@ -0,0 +1,15 @@
require 'spec_helper'
# Specs in this file have access to a helper object that includes
# the AuthenticationsHelper. For example:
#
# describe AuthenticationsHelper do
# describe "string concat" do
# it "concats two strings with spaces" do
# helper.concat_strings("this","that").should == "this that"
# end
# end
# end
describe AuthenticationsHelper do
pending "add some examples to (or delete) #{__FILE__}"
end

View File

@@ -0,0 +1,5 @@
require 'spec_helper'
describe Authentication do
pending "add some examples to (or delete) #{__FILE__}"
end

View File

@@ -0,0 +1,11 @@
require 'spec_helper'
describe "Authentications" do
describe "GET /authentications" do
it "works! (now write some real specs)" do
# Run the generator again with the --webrat flag if you want to use webrat methods/matchers
get authentications_path
response.status.should be(200)
end
end
end

View File

@@ -0,0 +1,35 @@
require "spec_helper"
describe AuthenticationsController do
describe "routing" do
it "routes to #index" do
get("/authentications").should route_to("authentications#index")
end
it "routes to #new" do
get("/authentications/new").should route_to("authentications#new")
end
it "routes to #show" do
get("/authentications/1").should route_to("authentications#show", :id => "1")
end
it "routes to #edit" do
get("/authentications/1/edit").should route_to("authentications#edit", :id => "1")
end
it "routes to #create" do
post("/authentications").should route_to("authentications#create")
end
it "routes to #update" do
put("/authentications/1").should route_to("authentications#update", :id => "1")
end
it "routes to #destroy" do
delete("/authentications/1").should route_to("authentications#destroy", :id => "1")
end
end
end

View File

@@ -0,0 +1,24 @@
require 'spec_helper'
describe "authentications/edit" do
before(:each) do
@authentication = assign(:authentication, stub_model(Authentication,
:member_id => 1,
:provider => "MyString",
:uid => "MyString",
:secret => "MyString"
))
end
it "renders the edit authentication form" do
render
# Run the generator again with the --webrat flag if you want to use webrat matchers
assert_select "form", :action => authentications_path(@authentication), :method => "post" do
assert_select "input#authentication_member_id", :name => "authentication[member_id]"
assert_select "input#authentication_provider", :name => "authentication[provider]"
assert_select "input#authentication_uid", :name => "authentication[uid]"
assert_select "input#authentication_secret", :name => "authentication[secret]"
end
end
end

View File

@@ -0,0 +1,29 @@
require 'spec_helper'
describe "authentications/index" do
before(:each) do
assign(:authentications, [
stub_model(Authentication,
:member_id => 1,
:provider => "Provider",
:uid => "Uid",
:secret => "Secret"
),
stub_model(Authentication,
:member_id => 1,
:provider => "Provider",
:uid => "Uid",
:secret => "Secret"
)
])
end
it "renders a list of authentications" do
render
# Run the generator again with the --webrat flag if you want to use webrat matchers
assert_select "tr>td", :text => 1.to_s, :count => 2
assert_select "tr>td", :text => "Provider".to_s, :count => 2
assert_select "tr>td", :text => "Uid".to_s, :count => 2
assert_select "tr>td", :text => "Secret".to_s, :count => 2
end
end

View File

@@ -0,0 +1,24 @@
require 'spec_helper'
describe "authentications/new" do
before(:each) do
assign(:authentication, stub_model(Authentication,
:member_id => 1,
:provider => "MyString",
:uid => "MyString",
:secret => "MyString"
).as_new_record)
end
it "renders new authentication form" do
render
# Run the generator again with the --webrat flag if you want to use webrat matchers
assert_select "form", :action => authentications_path, :method => "post" do
assert_select "input#authentication_member_id", :name => "authentication[member_id]"
assert_select "input#authentication_provider", :name => "authentication[provider]"
assert_select "input#authentication_uid", :name => "authentication[uid]"
assert_select "input#authentication_secret", :name => "authentication[secret]"
end
end
end

View File

@@ -0,0 +1,21 @@
require 'spec_helper'
describe "authentications/show" do
before(:each) do
@authentication = assign(:authentication, stub_model(Authentication,
:member_id => 1,
:provider => "Provider",
:uid => "Uid",
:secret => "Secret"
))
end
it "renders attributes in <p>" do
render
# Run the generator again with the --webrat flag if you want to use webrat matchers
rendered.should match(/1/)
rendered.should match(/Provider/)
rendered.should match(/Uid/)
rendered.should match(/Secret/)
end
end