From 274a6496b13c16ab95960bbe4d2e73a77f3d5678 Mon Sep 17 00:00:00 2001 From: David Taylor Date: Thu, 29 Dec 2022 12:35:01 +0000 Subject: [PATCH] DEV: Introduce syntax_tree for ruby formatting (#63) --- .github/workflows/plugin-linting.yml | 9 ++ .github/workflows/plugin-tests.yml | 6 +- .rubocop.yml | 2 +- .streerc | 2 + Gemfile | 1 + Gemfile.lock | 4 + lib/auth/steam_authenticator.rb | 12 +- .../enable_steam_logins_validator.rb | 4 +- plugin.rb | 20 +-- .../enable_steam_logins_validator_spec.rb | 28 ++--- spec/integration/steam_auth_spec.rb | 115 +++++++++--------- spec/lib/auth/steam_authenticator_spec.rb | 6 +- 12 files changed, 111 insertions(+), 98 deletions(-) create mode 100644 .streerc diff --git a/.github/workflows/plugin-linting.yml b/.github/workflows/plugin-linting.yml index c807794..6d2bb97 100644 --- a/.github/workflows/plugin-linting.yml +++ b/.github/workflows/plugin-linting.yml @@ -55,3 +55,12 @@ jobs: - name: Rubocop if: ${{ !cancelled() }} run: bundle exec rubocop . + + - name: Syntax Tree + if: ${{ !cancelled() }} + run: | + if test -f .streerc; then + bundle exec stree check Gemfile $(git ls-files '*.rb') $(git ls-files '*.rake') + else + echo "Stree config not detected for this repository. Skipping." + fi diff --git a/.github/workflows/plugin-tests.yml b/.github/workflows/plugin-tests.yml index 9d390bc..f30a5be 100644 --- a/.github/workflows/plugin-tests.yml +++ b/.github/workflows/plugin-tests.yml @@ -80,7 +80,7 @@ jobs: - name: Get yarn cache directory id: yarn-cache-dir - run: echo "::set-output name=dir::$(yarn cache dir)" + run: echo "dir=$(yarn cache dir)" >> $GITHUB_OUTPUT - name: Yarn cache uses: actions/cache@v3 @@ -130,7 +130,7 @@ jobs: shell: bash run: | if [ 0 -lt $(find plugins/${{ github.event.repository.name }}/spec -type f -name "*.rb" 2> /dev/null | wc -l) ]; then - echo "::set-output name=files_exist::true" + echo "files_exist=true" >> $GITHUB_OUTPUT fi - name: Plugin RSpec @@ -142,7 +142,7 @@ jobs: shell: bash run: | if [ 0 -lt $(find plugins/${{ github.event.repository.name }}/test/javascripts -type f \( -name "*.js" -or -name "*.es6" \) 2> /dev/null | wc -l) ]; then - echo "::set-output name=files_exist::true" + echo "files_exist=true" >> $GITHUB_OUTPUT fi - name: Plugin QUnit diff --git a/.rubocop.yml b/.rubocop.yml index d46296c..fb14dfa 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -1,2 +1,2 @@ inherit_gem: - rubocop-discourse: default.yml + rubocop-discourse: stree-compat.yml diff --git a/.streerc b/.streerc new file mode 100644 index 0000000..0bc4379 --- /dev/null +++ b/.streerc @@ -0,0 +1,2 @@ +--print-width=100 +--plugins=plugin/trailing_comma diff --git a/Gemfile b/Gemfile index a5648b4..31d8bf7 100644 --- a/Gemfile +++ b/Gemfile @@ -4,4 +4,5 @@ source "https://rubygems.org" group :development do gem "rubocop-discourse" + gem "syntax_tree" end diff --git a/Gemfile.lock b/Gemfile.lock index 87ff84d..348ff70 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -6,6 +6,7 @@ GEM parallel (1.22.1) parser (3.1.2.1) ast (~> 2.4.1) + prettier_print (1.2.0) rainbow (3.1.1) regexp_parser (2.6.0) rexml (3.2.5) @@ -27,6 +28,8 @@ GEM rubocop-rspec (2.13.2) rubocop (~> 1.33) ruby-progressbar (1.11.0) + syntax_tree (5.1.0) + prettier_print (>= 1.2.0) unicode-display_width (2.3.0) PLATFORMS @@ -34,6 +37,7 @@ PLATFORMS DEPENDENCIES rubocop-discourse + syntax_tree BUNDLED WITH 2.1.4 diff --git a/lib/auth/steam_authenticator.rb b/lib/auth/steam_authenticator.rb index 3711209..f05a0d6 100644 --- a/lib/auth/steam_authenticator.rb +++ b/lib/auth/steam_authenticator.rb @@ -2,7 +2,7 @@ class Auth::SteamAuthenticator < ::Auth::ManagedAuthenticator def name - 'steam' + "steam" end def enabled? @@ -14,9 +14,11 @@ class Auth::SteamAuthenticator < ::Auth::ManagedAuthenticator end def register_middleware(omniauth) - omniauth.provider :steam, setup: lambda { |env| - strategy = env["omniauth.strategy"] - strategy.options[:api_key] = SiteSetting.steam_web_api_key - } + omniauth.provider :steam, + setup: + lambda { |env| + strategy = env["omniauth.strategy"] + strategy.options[:api_key] = SiteSetting.steam_web_api_key + } end end diff --git a/lib/validators/enable_steam_logins_validator.rb b/lib/validators/enable_steam_logins_validator.rb index 771a4bc..55f857d 100644 --- a/lib/validators/enable_steam_logins_validator.rb +++ b/lib/validators/enable_steam_logins_validator.rb @@ -6,14 +6,14 @@ class EnableSteamLoginsValidator end def valid_value?(val) - return true if val == 'f' + return true if val == "f" return false if SiteSetting.steam_web_api_key.blank? true end def error_message if SiteSetting.steam_web_api_key.blank? - I18n.t('site_settings.errors.steam_web_api_key_is_empty') + I18n.t("site_settings.errors.steam_web_api_key_is_empty") end end end diff --git a/plugin.rb b/plugin.rb index 9933c58..765c012 100644 --- a/plugin.rb +++ b/plugin.rb @@ -6,21 +6,21 @@ # author: J. de Faye, tgxworld # omniauth-openid is not included in core since v2.4.0.beta10 -unless defined? OmniAuth::Strategies::OpenID - gem 'ruby-openid', '2.9.2', require: false - gem 'rack-openid', '1.3.1', require: false - gem 'omniauth-openid', '1.0.1' +unless defined?(OmniAuth::Strategies::OpenID) + gem "ruby-openid", "2.9.2", require: false + gem "rack-openid", "1.3.1", require: false + gem "omniauth-openid", "1.0.1" end -gem 'omniauth-steam', '1.0.6' +gem "omniauth-steam", "1.0.6" register_svg_icon "fab-steam" if respond_to?(:register_svg_icon) -register_asset 'stylesheets/steam-login.scss' +register_asset "stylesheets/steam-login.scss" -[ - "../lib/auth/steam_authenticator.rb", - "../lib/validators/enable_steam_logins_validator.rb" +%w[ + ../lib/auth/steam_authenticator.rb + ../lib/validators/enable_steam_logins_validator.rb ].each { |path| load File.expand_path(path, __FILE__) } -auth_provider authenticator: Auth::SteamAuthenticator.new, icon: 'fab-steam' +auth_provider authenticator: Auth::SteamAuthenticator.new, icon: "fab-steam" diff --git a/spec/components/validators/enable_steam_logins_validator_spec.rb b/spec/components/validators/enable_steam_logins_validator_spec.rb index 8a2a192..bcc5f12 100644 --- a/spec/components/validators/enable_steam_logins_validator_spec.rb +++ b/spec/components/validators/enable_steam_logins_validator_spec.rb @@ -1,36 +1,34 @@ # frozen_string_literal: true -require 'rails_helper' +require "rails_helper" RSpec.describe EnableSteamLoginsValidator do subject { described_class.new } - describe '#valid_value?' do - describe 'when steam_web_api_key has not been set' do + describe "#valid_value?" do + describe "when steam_web_api_key has not been set" do it "should return true when value is false" do - expect(subject.valid_value?('f')).to eq(true) + expect(subject.valid_value?("f")).to eq(true) end it "should return false when value is true" do - expect(subject.valid_value?('t')).to eq(false) + expect(subject.valid_value?("t")).to eq(false) - expect(subject.error_message).to eq(I18n.t( - 'site_settings.errors.steam_web_api_key_is_empty' - )) + expect(subject.error_message).to eq( + I18n.t("site_settings.errors.steam_web_api_key_is_empty"), + ) end end - describe 'when steam_web_api_key has been set' do - before do - SiteSetting.steam_web_api_key = "somekey" - end + describe "when steam_web_api_key has been set" do + before { SiteSetting.steam_web_api_key = "somekey" } it "should return true when value is false" do - expect(subject.valid_value?('f')).to eq(true) + expect(subject.valid_value?("f")).to eq(true) end - it 'should return true when value is true' do - expect(subject.valid_value?('t')).to eq(true) + it "should return true when value is true" do + expect(subject.valid_value?("t")).to eq(true) end end end diff --git a/spec/integration/steam_auth_spec.rb b/spec/integration/steam_auth_spec.rb index 2ca385f..97140f8 100644 --- a/spec/integration/steam_auth_spec.rb +++ b/spec/integration/steam_auth_spec.rb @@ -1,6 +1,6 @@ # frozen_string_literal: true -describe 'Steam OpenID 2.0' do +describe "Steam OpenID 2.0" do let(:web_api_key) { "abcdef11223344" } let(:steam_user_id) { "894923402340234" } @@ -11,16 +11,12 @@ describe 'Steam OpenID 2.0' do SiteSetting.enable_steam_logins = true cert = OpenSSL::X509::Certificate.new - cert.add_extension(OpenSSL::X509::Extension.new( - "subjectAltName", - "DNS:steamcommunity.com" - )) + cert.add_extension(OpenSSL::X509::Extension.new("subjectAltName", "DNS:steamcommunity.com")) StubSocket::StubIO.any_instance.stubs(:peer_cert).returns(cert) - stub_request(:get, "http://steamcommunity.com/openid") - .to_return( - status: 200, - body: <<~XML, + stub_request(:get, "http://steamcommunity.com/openid").to_return( + status: 200, + body: <<~XML, @@ -31,25 +27,23 @@ describe 'Steam OpenID 2.0' do XML - headers: { - "Content-Type" => "text/xml" - } - ) - stub_request(:post, "https://steamcommunity.com/openid/login") - .to_return( - status: 200, - body: <<~BODY, + headers: { + "Content-Type" => "text/xml", + }, + ) + stub_request(:post, "https://steamcommunity.com/openid/login").to_return( + status: 200, + body: <<~BODY, ns:http://specs.openid.net/auth/2.0 is_valid:true BODY - headers: { - "Content-Type" => "text/plain" - } - ) - stub_request(:get, "https://steamcommunity.com/openid/id/#{steam_user_id}") - .to_return( - status: 200, - body: <<~BODY + headers: { + "Content-Type" => "text/plain", + }, + ) + stub_request(:get, "https://steamcommunity.com/openid/id/#{steam_user_id}").to_return( + status: 200, + body: <<~BODY, @@ -60,16 +54,14 @@ describe 'Steam OpenID 2.0' do BODY - ) - stub_request(:get, "https://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/?key=#{web_api_key}&steamids=#{steam_user_id}") - .with( - headers: { - "Host" => "api.steampowered.com" - } - ) - .to_return( - status: 200, - body: JSON.dump( + ) + stub_request( + :get, + "https://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/?key=#{web_api_key}&steamids=#{steam_user_id}", + ).with(headers: { "Host" => "api.steampowered.com" }).to_return( + status: 200, + body: + JSON.dump( response: { players: [ { @@ -78,20 +70,23 @@ describe 'Steam OpenID 2.0' do profilestate: 1, personaname: "SteamyPlayer", profileurl: "https://steamcommunity.com/id/SteamyPlayer/", - avatar: "https://avatars.akamai.steamstatic.com/734096e84a118c839422fa213faa6885c093374d.jpg", - avatarmedium: "https://avatars.akamai.steamstatic.com/734096e84a118c839422fa213faa6885c093374d_medium.jpg", - avatarfull: "https://avatars.akamai.steamstatic.com/734096e84a118c839422fa213faa6885c093374d_full.jpg", + avatar: + "https://avatars.akamai.steamstatic.com/734096e84a118c839422fa213faa6885c093374d.jpg", + avatarmedium: + "https://avatars.akamai.steamstatic.com/734096e84a118c839422fa213faa6885c093374d_medium.jpg", + avatarfull: + "https://avatars.akamai.steamstatic.com/734096e84a118c839422fa213faa6885c093374d_full.jpg", avatarhash: "734096e84a118c839422fa213faa6885c093374d", - lastlogoff: 1659365725, + lastlogoff: 1_659_365_725, personastate: 0, primaryclanid: "103588791859221341", - timecreated: 1524883291, - personastateflags: 0 - } - ] - } - ) - ) + timecreated: 1_524_883_291, + personastateflags: 0, + }, + ], + }, + ), + ) end it "retrieves user details from the Steam API after the authentication flow is complete" do @@ -99,19 +94,21 @@ describe 'Steam OpenID 2.0' do expect(response.status).to eq(303) expect(response.location).to start_with("https://steamcommunity.com/openid/login") - post "/auth/steam/callback", params: { - _method: "post", - "openid.ns": "http://specs.openid.net/auth/2.0", - "openid.mode": "id_res", - "openid.op_endpoint": "https://steamcommunity.com/openid/login", - "openid.claimed_id": "https://steamcommunity.com/openid/id/#{steam_user_id}", - "openid.identity": "https://steamcommunity.com/openid/id/#{steam_user_id}", - "openid.return_to": "http://test.localhost/auth/steam/callback?_method=post", - "openid.response_nonce": "#{1.minute.ago.iso8601}aZq7eHv92/2Mg4OsPKiKcTgI0j4=", - "openid.assoc_handle": "1234567890", - "openid.signed": "signed,op_endpoint,claimed_id,identity,return_to,response_nonce,assoc_handle", - "openid.sig": "ZseI1sqVHGU/f5Ye7Tcn7T3QMIg=", - } + post "/auth/steam/callback", + params: { + _method: "post", + "openid.ns": "http://specs.openid.net/auth/2.0", + "openid.mode": "id_res", + "openid.op_endpoint": "https://steamcommunity.com/openid/login", + "openid.claimed_id": "https://steamcommunity.com/openid/id/#{steam_user_id}", + "openid.identity": "https://steamcommunity.com/openid/id/#{steam_user_id}", + "openid.return_to": "http://test.localhost/auth/steam/callback?_method=post", + "openid.response_nonce": "#{1.minute.ago.iso8601}aZq7eHv92/2Mg4OsPKiKcTgI0j4=", + "openid.assoc_handle": "1234567890", + "openid.signed": + "signed,op_endpoint,claimed_id,identity,return_to,response_nonce,assoc_handle", + "openid.sig": "ZseI1sqVHGU/f5Ye7Tcn7T3QMIg=", + } expect(response.status).to eq(302) expect(response.location).to eq("http://test.localhost/") expect(session[:current_user_id]).to be_blank diff --git a/spec/lib/auth/steam_authenticator_spec.rb b/spec/lib/auth/steam_authenticator_spec.rb index 8350637..897f62c 100644 --- a/spec/lib/auth/steam_authenticator_spec.rb +++ b/spec/lib/auth/steam_authenticator_spec.rb @@ -1,14 +1,14 @@ # frozen_string_literal: true describe Auth::SteamAuthenticator do - describe '#can_revoke?' do - it 'should be false be default' do + describe "#can_revoke?" do + it "should be false be default" do authenticator = Auth::SteamAuthenticator.new expect(authenticator.can_revoke?).to eq(false) end - it 'should be true when steam_logins_allow_revoke site settings is enabled' do + it "should be true when steam_logins_allow_revoke site settings is enabled" do SiteSetting.steam_logins_allow_revoke = true authenticator = Auth::SteamAuthenticator.new