From 6d5169d7071e27b3e18376b4c7129f4e5dbfe69d Mon Sep 17 00:00:00 2001
From: Raphael Sofaer <raphael@joindiaspora.com>
Date: Thu, 9 Jun 2011 14:50:30 -0700
Subject: [PATCH] Chubbies is working with local diaspora-client gem

---
 Gemfile.lock                                  | 10 +++
 .../20110526184644_add_oauth2_tables.rb       |  1 +
 db/schema.rb                                  |  2 +
 features/oauth.feature                        | 32 ++++-----
 features/step_definitions/oauth_steps.rb      | 26 ++++---
 spec/chubbies/app.rb                          | 70 ++++++++++++-------
 6 files changed, 88 insertions(+), 53 deletions(-)

diff --git a/Gemfile.lock b/Gemfile.lock
index bd597abff1..4b7d8d3018 100644
--- a/Gemfile.lock
+++ b/Gemfile.lock
@@ -37,6 +37,15 @@ GIT
       addressable (>= 2.1.1)
       eventmachine (>= 0.12.9)
 
+PATH
+  remote: ~/workspace/diaspora-client
+  specs:
+    diaspora-client (0.0.0)
+      activerecord
+      faraday
+      oauth2
+      sinatra
+
 GEM
   remote: http://rubygems.org/
   specs:
@@ -414,6 +423,7 @@ DEPENDENCIES
   database_cleaner (= 0.6.0)
   devise (~> 1.3.1)
   devise_invitable (= 0.5.0)
+  diaspora-client!
   em-websocket!
   excon (= 0.2.4)
   factory_girl_rails
diff --git a/db/migrate/20110526184644_add_oauth2_tables.rb b/db/migrate/20110526184644_add_oauth2_tables.rb
index 7d20f7418b..4d6b87fe4b 100644
--- a/db/migrate/20110526184644_add_oauth2_tables.rb
+++ b/db/migrate/20110526184644_add_oauth2_tables.rb
@@ -5,6 +5,7 @@ class AddOauth2Tables < ActiveRecord::Migration
       t.string   'oauth_identifier', :limit => 32, :null => false
       t.string   'oauth_secret',     :limit => 32, :null => false
     end
+    add_index :oauth_clients, :name, :unique => true
 
     create_table 'oauth_authorization_codes', :force => true do |t|
       t.integer  'authorization_id',    :null => false
diff --git a/db/schema.rb b/db/schema.rb
index 9f7bfa013a..2799dbc43c 100644
--- a/db/schema.rb
+++ b/db/schema.rb
@@ -230,6 +230,8 @@ ActiveRecord::Schema.define(:version => 20110603212633) do
     t.string "icon_url"
   end
 
+  add_index "oauth_clients", ["name"], :name => "index_oauth_clients_on_name", :unique => true
+
   create_table "people", :force => true do |t|
     t.string   "guid",                  :null => false
     t.text     "url",                   :null => false
diff --git a/features/oauth.feature b/features/oauth.feature
index 4826252361..831394e51a 100644
--- a/features/oauth.feature
+++ b/features/oauth.feature
@@ -5,38 +5,36 @@ Feature: oauth
   Background:
     Given Chubbies has been killed
     And Chubbies is running
-    When I visit "/" on Chubbies
+    And I visit "/reset" on Chubbies
     And a user with username "bob" and password "secret"
 
   Scenario: Authorize Chubbies
-    When I visit "/" on Chubbies
-    And I try to authorize Chubbies
-    Then I should see "Authorize Chubbies?"
-    And I should see "Chubbies tests Diaspora's OAuth capabilities."
+    When I try to authorize Chubbies
+    Then I should see "Chubbies"
+    And I should see "The best way to chub."
 
-    When I press "Yes"
+    When I press "Authorize"
     Then I should be on "/account" on Chubbies
     And I should see my "profile.birthday"
     And I should see my "name"
 
   Scenario: Not authorize Chubbies
-    When I visit "/" on Chubbies
-    And I try to authorize Chubbies
-    Then I should see "Authorize Chubbies?"
-    And I should see "Chubbies tests Diaspora's OAuth capabilities."
+    When I try to authorize Chubbies
+    Then I should see "Chubbies"
+    And I should see "The best way to chub."
 
     When I press "No"
-    Then I should be on "/callback" on Chubbies
-    Then I should see "What is your major malfunction?"
+    Then I should be on "/account" on Chubbies
+    Then I should see "No access token."
 
   Scenario: Authorize Chubbies
     Given Chubbies is registered on my pod
-    When I visit "/" on Chubbies
-    And I try to authorize Chubbies
-    Then I should see "Authorize Chubbies?"
-    And I should see "Chubbies tests Diaspora's OAuth capabilities."
+    When I try to authorize Chubbies
+    Then I should see "Chubbies"
+    And I should see "The best way to chub."
+    And there is only one Chubbies
 
-    When I press "Yes"
+    When I press "Authorize"
     Then I should be on "/account" on Chubbies
     And I should see my "profile.birthday"
     And I should see my "name"
diff --git a/features/step_definitions/oauth_steps.rb b/features/step_definitions/oauth_steps.rb
index 326b8b14be..2960ff7b93 100644
--- a/features/step_definitions/oauth_steps.rb
+++ b/features/step_definitions/oauth_steps.rb
@@ -7,23 +7,28 @@ Given /^Chubbies has been killed$/ do
 end
 
 Given /^Chubbies is registered on my pod$/ do
-  OAuth2::Provider.client_class.create! :name => 'Chubbies',
-    :oauth_identifier => 'abcdefgh12345678',
-    :oauth_secret => 'secret'
+  client = OAuth2::Provider.client_class.create_from_manifest!("localhost:#{Chubbies::PORT}/manifest.json")
+  params = {:client_id => client.oauth_identifier,
+            :client_secret => client.oauth_secret,
+            :host => "localhost:9887"}
+  RestClient.post("localhost:#{Chubbies::PORT}/register", params)
 end
 
 And /^I should see my "([^"]+)"/ do |code|
   page.should have_content(@me.person.instance_eval(code).to_s)
 end
 
+And /^there is only one Chubbies$/ do
+  OAuth2::Provider.client_class.where(:name => "Chubbies").count.should == 1
+end
+
 When /^I try to authorize Chubbies$/ do
   # We need to reset the tokens saved in Chubbies,
   # as we are clearing the Diaspora DB every scenario
-  Then 'I visit "/reset" on Chubbies'
-  Then 'I visit "/" on Chubbies'
+  Then 'I visit "/new" on Chubbies'
   ###
   And "I fill in \"Diaspora Handle\" with \"#{@me.diaspora_handle}\""
-  And 'I press "Log in with Diaspora"'
+  And 'I press "Connect to Diaspora"'
   Then 'I should be on the new user session page'
   And "I fill in \"Username\" with \"#{@me.username}\""
   And "I fill in \"Password\" with \"#{@me.password}\""
@@ -44,13 +49,13 @@ class Chubbies
 
   def self.run
     @pid = fork do
-      Process.exec "cd #{Rails.root}/spec/chubbies/ && BUNDLE_GEMFILE=Gemfile DIASPORA_PORT=9887 bundle exec rackup -p #{PORT} 2> /dev/null"
+      Process.exec "cd #{Rails.root}/spec/chubbies/ && BUNDLE_GEMFILE=Gemfile bundle exec rackup -p #{PORT} 2> /dev/null 1> /dev/null"
     end
 
     at_exit do
       Chubbies.kill
     end
-    
+
     while(!running?) do
       sleep(1)
     end
@@ -69,7 +74,10 @@ class Chubbies
 
   def self.running?
     begin
-      RestClient.get("localhost:#{PORT}")
+      begin
+      RestClient.get("localhost:#{PORT}/running")
+      rescue RestClient::ResourceNotFound
+      end
       true
     rescue Errno::ECONNREFUSED
       false
diff --git a/spec/chubbies/app.rb b/spec/chubbies/app.rb
index 49d8e32313..ad6db84742 100644
--- a/spec/chubbies/app.rb
+++ b/spec/chubbies/app.rb
@@ -1,36 +1,41 @@
 module Chubbies
   require 'active_record'
   require 'diaspora-client'
-  `rm -f #{File.expand_path('../chubbies.sqlite3', __FILE__)}`
-  ActiveRecord::Base.establish_connection(
-      :adapter => "sqlite3",
-      :database  => "chubbies.sqlite3"
-  )
 
-  ActiveRecord::Schema.define do
-    create_table :resource_servers do |t|
-      t.string :client_id,     :limit => 40,  :null => false
-      t.string :client_secret, :limit => 40,  :null => false
-      t.string :host,          :limit => 127, :null => false
-      t.timestamps
-    end
-    add_index :resource_servers, :host, :unique => true
+  def self.reset_db
+    `rm -f #{File.expand_path('../chubbies.sqlite3', __FILE__)}`
+    ActiveRecord::Base.establish_connection(
+        :adapter => "sqlite3",
+        :database  => "chubbies.sqlite3"
+    )
 
-    create_table :access_tokens do |t|
-      t.integer :user_id, :null => false
-      t.integer :resource_server_id, :null => false
-      t.string  :access_token, :limit => 40, :null => false
-      t.string  :refresh_token, :limit => 40, :null => false
-      t.string  :uid, :limit => 40, :null => false
-      t.datetime :expires_at
-      t.timestamps
-    end
-    add_index :access_tokens, :user_id, :unique => true
-    create_table :users do |t|
-      t.timestamps
+    ActiveRecord::Schema.define do
+      create_table :resource_servers do |t|
+        t.string :client_id,     :limit => 40,  :null => false
+        t.string :client_secret, :limit => 40,  :null => false
+        t.string :host,          :limit => 127, :null => false
+        t.timestamps
+      end
+      add_index :resource_servers, :host, :unique => true
+
+      create_table :access_tokens do |t|
+        t.integer :user_id, :null => false
+        t.integer :resource_server_id, :null => false
+        t.string  :access_token, :limit => 40, :null => false
+        t.string  :refresh_token, :limit => 40, :null => false
+        t.string  :uid, :limit => 40, :null => false
+        t.datetime :expires_at
+        t.timestamps
+      end
+      add_index :access_tokens, :user_id, :unique => true
+      create_table :users do |t|
+        t.timestamps
+      end
     end
   end
 
+  self.reset_db
+
   class User < ActiveRecord::Base
     has_one :access_token, :class_name => "DiasporaClient::AccessToken", :dependent => :destroy
   end
@@ -51,8 +56,12 @@ module Chubbies
 
     get '/account' do
       if params['id'] && user = User.where(:id => params['id']).first
-        @resource_response = user.access_token.token.get("/api/v0/me")
-        haml :response
+        if user.access_token
+          @resource_response = user.access_token.token.get("/api/v0/me")
+          haml :response
+        else
+          "No access token."
+        end
       else
         "No user with id #{params['id']}"
       end
@@ -71,5 +80,12 @@ module Chubbies
         "icon_url"     => "#"
       }.to_json
     end
+    get '/reset' do
+      Chubbies.reset_db
+    end
+
+    post '/register' do
+      DiasporaClient::ResourceServer.create!(params)
+    end
   end
 end
-- 
GitLab