From 6e1a67345990631f25e60434181be9f650c44a98 Mon Sep 17 00:00:00 2001 From: theworldbright Date: Sun, 2 Aug 2015 13:07:04 +0900 Subject: [PATCH] Replace let!() with factory girl --- .../authorizations_controller_spec.rb | 12 +---- spec/factories.rb | 48 +++++++++++++++++++ .../api/user_info_controller_spec.rb | 24 +++------- .../protected_resource_endpoint_spec.rb | 12 +---- .../api/openid_connect/token_endpoint_spec.rb | 6 +-- 5 files changed, 58 insertions(+), 44 deletions(-) diff --git a/spec/controllers/api/openid_connect/authorizations_controller_spec.rb b/spec/controllers/api/openid_connect/authorizations_controller_spec.rb index 74aa1ed02..d60ec9c1e 100644 --- a/spec/controllers/api/openid_connect/authorizations_controller_spec.rb +++ b/spec/controllers/api/openid_connect/authorizations_controller_spec.rb @@ -1,16 +1,8 @@ require "spec_helper" describe Api::OpenidConnect::AuthorizationsController, type: :controller do - let!(:client) do - Api::OpenidConnect::OAuthApplication.create!( - client_name: "Diaspora Test Client", redirect_uris: ["http://localhost:3000/"]) - end - let!(:client_with_multiple_redirects) do - Api::OpenidConnect::OAuthApplication.create!( - client_name: "Diaspora Test Client", redirect_uris: ["http://localhost:3000/", "http://localhost/"]) - end - - # TODO: jhass - "Might want to setup some factories in spec/factories.rb, see factory_girl's docs." + let!(:client) { FactoryGirl.create(:o_auth_application) } + let!(:client_with_multiple_redirects) { FactoryGirl.create(:o_auth_application_with_multiple_redirects) } before do sign_in :user, alice diff --git a/spec/factories.rb b/spec/factories.rb index 9adf5534b..946985a95 100644 --- a/spec/factories.rb +++ b/spec/factories.rb @@ -310,6 +310,54 @@ FactoryGirl.define do factory(:status, :parent => :status_message) + factory :o_auth_application, class: Api::OpenidConnect::OAuthApplication do + client_name "Diaspora Test Client" + redirect_uris ["http://localhost:3000/"] + end + + factory :o_auth_application_with_ppid, class: Api::OpenidConnect::OAuthApplication do + client_name "Diaspora Test Client" + redirect_uris ["http://localhost:3000/"] + ppid true + sector_identifier_uri "https://example.com/uri" + end + + factory :o_auth_application_with_multiple_redirects, class: Api::OpenidConnect::OAuthApplication do + client_name "Diaspora Test Client" + redirect_uris ["http://localhost:3000/", "http://localhost/"] + end + + factory :auth_with_read, class: Api::OpenidConnect::Authorization do + o_auth_application + user + + after(:create) do |auth_with_read| + auth_with_read.scopes << [Api::OpenidConnect::Scope.find_or_create_by(name: "openid"), + Api::OpenidConnect::Scope.find_or_create_by(name: "read")] + end + end + + factory :auth_with_read_and_ppid, class: Api::OpenidConnect::Authorization do + association :o_auth_application, factory: :o_auth_application_with_ppid + user + + after(:create) do |auth_with_read| + auth_with_read.scopes << [Api::OpenidConnect::Scope.find_or_create_by(name: "openid"), + Api::OpenidConnect::Scope.find_or_create_by(name: "read")] + end + end + + factory :auth_with_read_and_write, class: Api::OpenidConnect::Authorization do + o_auth_application + user + + after(:create) do |auth_with_read| + auth_with_read.scopes << [Api::OpenidConnect::Scope.find_or_create_by(name: "openid"), + Api::OpenidConnect::Scope.find_or_create_by(name: "read"), + Api::OpenidConnect::Scope.find_or_create_by(name: "write")] + end + end + # Factories for the DiasporaFederation-gem factory(:federation_person_from_webfinger, class: DiasporaFederation::Entities::Person) do diff --git a/spec/integration/api/user_info_controller_spec.rb b/spec/integration/api/user_info_controller_spec.rb index ad47b486b..dd4fb95fe 100644 --- a/spec/integration/api/user_info_controller_spec.rb +++ b/spec/integration/api/user_info_controller_spec.rb @@ -1,33 +1,21 @@ require "spec_helper" - describe Api::OpenidConnect::UserInfoController do - # TODO: Replace with factory - let!(:client) do - Api::OpenidConnect::OAuthApplication.create!( - client_name: "Diaspora Test Client", - redirect_uris: ["http://localhost:3000/"], - ppid: true, sector_identifier_uri: "https://example.com/uri") - end - let(:auth_with_read) do - auth = Api::OpenidConnect::Authorization.create!(o_auth_application: client, user: alice) - auth.scopes << [Api::OpenidConnect::Scope.find_or_create_by(name: "openid"), - Api::OpenidConnect::Scope.find_or_create_by(name: "read")] - auth - end - let!(:access_token_with_read) { auth_with_read.create_access_token.to_s } + let!(:auth_with_read_and_ppid) { FactoryGirl.create(:auth_with_read_and_ppid) } + let!(:access_token_with_read) { auth_with_read_and_ppid.create_access_token.to_s } describe "#show" do before do + @user = auth_with_read_and_ppid.user get api_openid_connect_user_info_path, access_token: access_token_with_read end it "shows the info" do json_body = JSON.parse(response.body) expected_sub = - alice.pairwise_pseudonymous_identifiers.find_or_create_by(sector_identifier: "https://example.com/uri").guid + @user.pairwise_pseudonymous_identifiers.find_or_create_by(sector_identifier: "https://example.com/uri").guid expect(json_body["sub"]).to eq(expected_sub) - expect(json_body["nickname"]).to eq(alice.name) - expect(json_body["profile"]).to eq(File.join(AppConfig.environment.url, "people", alice.guid).to_s) + expect(json_body["nickname"]).to eq(@user.name) + expect(json_body["profile"]).to eq(File.join(AppConfig.environment.url, "people", @user.guid).to_s) end end end diff --git a/spec/lib/api/openid_connect/protected_resource_endpoint_spec.rb b/spec/lib/api/openid_connect/protected_resource_endpoint_spec.rb index 7cf0ccd29..ec819f13e 100644 --- a/spec/lib/api/openid_connect/protected_resource_endpoint_spec.rb +++ b/spec/lib/api/openid_connect/protected_resource_endpoint_spec.rb @@ -1,17 +1,7 @@ require "spec_helper" describe Api::OpenidConnect::ProtectedResourceEndpoint, type: :request do - # TODO: Replace with factory - let!(:client) do - Api::OpenidConnect::OAuthApplication.create!( - client_name: "Diaspora Test Client", redirect_uris: ["http://localhost:3000/"]) - end - let(:auth_with_read) do - auth = Api::OpenidConnect::Authorization.create!(o_auth_application: client, user: alice) - auth.scopes << [Api::OpenidConnect::Scope.find_by!(name: "openid"), - Api::OpenidConnect::Scope.find_by!(name: "read")] - auth - end + let(:auth_with_read) { FactoryGirl.create(:auth_with_read) } let!(:access_token_with_read) { auth_with_read.create_access_token.to_s } let(:invalid_token) { SecureRandom.hex(32).to_s } diff --git a/spec/lib/api/openid_connect/token_endpoint_spec.rb b/spec/lib/api/openid_connect/token_endpoint_spec.rb index c0b06b5be..1dad2c133 100644 --- a/spec/lib/api/openid_connect/token_endpoint_spec.rb +++ b/spec/lib/api/openid_connect/token_endpoint_spec.rb @@ -1,10 +1,6 @@ require "spec_helper" describe Api::OpenidConnect::TokenEndpoint, type: :request do - let!(:client) do - Api::OpenidConnect::OAuthApplication.create!( - redirect_uris: ["http://localhost:3000/"], client_name: "diaspora client", - ppid: true, sector_identifier_uri: "https://example.com/uri") - end + let!(:client) { FactoryGirl.create(:o_auth_application_with_ppid) } let!(:auth) do auth = Api::OpenidConnect::Authorization.find_or_create_by( o_auth_application: client, user: bob, redirect_uri: "http://localhost:3000/")