From 5ea91bf56afeeca4ef8860919ee544f9afa9e205 Mon Sep 17 00:00:00 2001 From: Miles Gould Date: Thu, 4 Apr 2013 18:48:54 +0100 Subject: [PATCH] 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. --- .../javascripts/authentications.js.coffee | 3 + app/controllers/authentications_controller.rb | 83 +++++++++ app/helpers/authentications_helper.rb | 2 + app/models/authentication.rb | 3 + app/views/authentications/_form.html.haml | 22 +++ app/views/authentications/edit.html.haml | 7 + app/views/authentications/index.html.haml | 25 +++ app/views/authentications/new.html.haml | 5 + app/views/authentications/show.html.haml | 18 ++ config/routes.rb | 1 + .../20130404174459_create_authentications.rb | 13 ++ db/schema.rb | 14 +- .../authentications_controller_spec.rb | 164 ++++++++++++++++++ spec/factories/authentications.rb | 10 ++ spec/helpers/authentications_helper_spec.rb | 15 ++ spec/models/authentication_spec.rb | 5 + spec/requests/authentications_spec.rb | 11 ++ spec/routing/authentications_routing_spec.rb | 35 ++++ .../authentications/edit.html.haml_spec.rb | 24 +++ .../authentications/index.html.haml_spec.rb | 29 ++++ .../authentications/new.html.haml_spec.rb | 24 +++ .../authentications/show.html.haml_spec.rb | 21 +++ 22 files changed, 533 insertions(+), 1 deletion(-) create mode 100644 app/assets/javascripts/authentications.js.coffee create mode 100644 app/controllers/authentications_controller.rb create mode 100644 app/helpers/authentications_helper.rb create mode 100644 app/models/authentication.rb create mode 100644 app/views/authentications/_form.html.haml create mode 100644 app/views/authentications/edit.html.haml create mode 100644 app/views/authentications/index.html.haml create mode 100644 app/views/authentications/new.html.haml create mode 100644 app/views/authentications/show.html.haml create mode 100644 db/migrate/20130404174459_create_authentications.rb create mode 100644 spec/controllers/authentications_controller_spec.rb create mode 100644 spec/factories/authentications.rb create mode 100644 spec/helpers/authentications_helper_spec.rb create mode 100644 spec/models/authentication_spec.rb create mode 100644 spec/requests/authentications_spec.rb create mode 100644 spec/routing/authentications_routing_spec.rb create mode 100644 spec/views/authentications/edit.html.haml_spec.rb create mode 100644 spec/views/authentications/index.html.haml_spec.rb create mode 100644 spec/views/authentications/new.html.haml_spec.rb create mode 100644 spec/views/authentications/show.html.haml_spec.rb diff --git a/app/assets/javascripts/authentications.js.coffee b/app/assets/javascripts/authentications.js.coffee new file mode 100644 index 000000000..761567942 --- /dev/null +++ b/app/assets/javascripts/authentications.js.coffee @@ -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/ diff --git a/app/controllers/authentications_controller.rb b/app/controllers/authentications_controller.rb new file mode 100644 index 000000000..8676dfdd8 --- /dev/null +++ b/app/controllers/authentications_controller.rb @@ -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 diff --git a/app/helpers/authentications_helper.rb b/app/helpers/authentications_helper.rb new file mode 100644 index 000000000..2c19ef818 --- /dev/null +++ b/app/helpers/authentications_helper.rb @@ -0,0 +1,2 @@ +module AuthenticationsHelper +end diff --git a/app/models/authentication.rb b/app/models/authentication.rb new file mode 100644 index 000000000..cb415bd91 --- /dev/null +++ b/app/models/authentication.rb @@ -0,0 +1,3 @@ +class Authentication < ActiveRecord::Base + attr_accessible :member_id, :provider, :secret, :uid +end diff --git a/app/views/authentications/_form.html.haml b/app/views/authentications/_form.html.haml new file mode 100644 index 000000000..210501534 --- /dev/null +++ b/app/views/authentications/_form.html.haml @@ -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' diff --git a/app/views/authentications/edit.html.haml b/app/views/authentications/edit.html.haml new file mode 100644 index 000000000..4fc381835 --- /dev/null +++ b/app/views/authentications/edit.html.haml @@ -0,0 +1,7 @@ +%h1 Editing authentication + += render 'form' + += link_to 'Show', @authentication +\| += link_to 'Back', authentications_path diff --git a/app/views/authentications/index.html.haml b/app/views/authentications/index.html.haml new file mode 100644 index 000000000..eeb90243f --- /dev/null +++ b/app/views/authentications/index.html.haml @@ -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 diff --git a/app/views/authentications/new.html.haml b/app/views/authentications/new.html.haml new file mode 100644 index 000000000..39d803c13 --- /dev/null +++ b/app/views/authentications/new.html.haml @@ -0,0 +1,5 @@ +%h1 New authentication + += render 'form' + += link_to 'Back', authentications_path diff --git a/app/views/authentications/show.html.haml b/app/views/authentications/show.html.haml new file mode 100644 index 000000000..dacaa2be8 --- /dev/null +++ b/app/views/authentications/show.html.haml @@ -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 diff --git a/config/routes.rb b/config/routes.rb index 871c85670..a4efa4413 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -2,6 +2,7 @@ Growstuff::Application.routes.draw do devise_for :members, :controllers => { :registrations => "registrations" } + resources :authentications resources :plantings resources :gardens resources :posts diff --git a/db/migrate/20130404174459_create_authentications.rb b/db/migrate/20130404174459_create_authentications.rb new file mode 100644 index 000000000..3acdf6080 --- /dev/null +++ b/db/migrate/20130404174459_create_authentications.rb @@ -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 diff --git a/db/schema.rb b/db/schema.rb index f13ce0fd1..279d0780e 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -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 diff --git a/spec/controllers/authentications_controller_spec.rb b/spec/controllers/authentications_controller_spec.rb new file mode 100644 index 000000000..bb8f53f76 --- /dev/null +++ b/spec/controllers/authentications_controller_spec.rb @@ -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 diff --git a/spec/factories/authentications.rb b/spec/factories/authentications.rb new file mode 100644 index 000000000..94588b6f9 --- /dev/null +++ b/spec/factories/authentications.rb @@ -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 diff --git a/spec/helpers/authentications_helper_spec.rb b/spec/helpers/authentications_helper_spec.rb new file mode 100644 index 000000000..1294169a4 --- /dev/null +++ b/spec/helpers/authentications_helper_spec.rb @@ -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 diff --git a/spec/models/authentication_spec.rb b/spec/models/authentication_spec.rb new file mode 100644 index 000000000..7a30df466 --- /dev/null +++ b/spec/models/authentication_spec.rb @@ -0,0 +1,5 @@ +require 'spec_helper' + +describe Authentication do + pending "add some examples to (or delete) #{__FILE__}" +end diff --git a/spec/requests/authentications_spec.rb b/spec/requests/authentications_spec.rb new file mode 100644 index 000000000..01843d6bc --- /dev/null +++ b/spec/requests/authentications_spec.rb @@ -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 diff --git a/spec/routing/authentications_routing_spec.rb b/spec/routing/authentications_routing_spec.rb new file mode 100644 index 000000000..1a639dead --- /dev/null +++ b/spec/routing/authentications_routing_spec.rb @@ -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 diff --git a/spec/views/authentications/edit.html.haml_spec.rb b/spec/views/authentications/edit.html.haml_spec.rb new file mode 100644 index 000000000..725f39986 --- /dev/null +++ b/spec/views/authentications/edit.html.haml_spec.rb @@ -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 diff --git a/spec/views/authentications/index.html.haml_spec.rb b/spec/views/authentications/index.html.haml_spec.rb new file mode 100644 index 000000000..5e5748e97 --- /dev/null +++ b/spec/views/authentications/index.html.haml_spec.rb @@ -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 diff --git a/spec/views/authentications/new.html.haml_spec.rb b/spec/views/authentications/new.html.haml_spec.rb new file mode 100644 index 000000000..89e54b43d --- /dev/null +++ b/spec/views/authentications/new.html.haml_spec.rb @@ -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 diff --git a/spec/views/authentications/show.html.haml_spec.rb b/spec/views/authentications/show.html.haml_spec.rb new file mode 100644 index 000000000..c0f0c1179 --- /dev/null +++ b/spec/views/authentications/show.html.haml_spec.rb @@ -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

" 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