From 3ee8c3aaf83275ebcfc6b59827478bc9fb2b530b Mon Sep 17 00:00:00 2001 From: Constantine Nikolaou <927+constantine-nikolaou@users.noreply.github.com> Date: Sat, 28 May 2022 23:30:23 +0200 Subject: [PATCH 01/15] Create rubyonrails.yml --- .github/workflows/rubyonrails.yml | 58 +++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 .github/workflows/rubyonrails.yml diff --git a/.github/workflows/rubyonrails.yml b/.github/workflows/rubyonrails.yml new file mode 100644 index 0000000..96a69dc --- /dev/null +++ b/.github/workflows/rubyonrails.yml @@ -0,0 +1,58 @@ +# This workflow uses actions that are not certified by GitHub. They are +# provided by a third-party and are governed by separate terms of service, +# privacy policy, and support documentation. +# +# This workflow will install a prebuilt Ruby version, install dependencies, and +# run tests and linters. +name: "Ruby on Rails CI" +on: + push: + branches: [ main ] + pull_request: + branches: [ main ] +jobs: + test: + runs-on: ubuntu-latest + services: + postgres: + image: postgres:11-alpine + ports: + - "5432:5432" + env: + POSTGRES_DB: rails_test + POSTGRES_USER: rails + POSTGRES_PASSWORD: password + env: + RAILS_ENV: test + DATABASE_URL: "postgres://rails:password@localhost:5432/rails_test" + steps: + - name: Checkout code + uses: actions/checkout@v3 + # Add or replace dependency steps here + - name: Install Ruby and gems + uses: ruby/setup-ruby@8f312efe1262fb463d906e9bf040319394c18d3e # v1.92 + with: + bundler-cache: true + # Add or replace database setup steps here + - name: Set up database schema + run: bin/rails db:schema:load + # Add or replace test runners here + - name: Run tests + run: bin/rake + + lint: + runs-on: ubuntu-latest + steps: + - name: Checkout code + uses: actions/checkout@v3 + - name: Install Ruby and gems + uses: ruby/setup-ruby@8f312efe1262fb463d906e9bf040319394c18d3e # v1.92 + with: + bundler-cache: true + # Add or replace any other lints here + - name: Security audit dependencies + run: bin/bundler-audit --update + - name: Security audit application code + run: bin/brakeman -q -w2 + - name: Lint Ruby files + run: bin/rubocop --parallel From 7aaff2f65c5dd15d020cd6a0554a4a9ab32b9db3 Mon Sep 17 00:00:00 2001 From: Constantine Nikolaou Date: Mon, 30 May 2022 22:08:50 +0200 Subject: [PATCH 02/15] Use placeholder for secret_key_base in test environment --- config/secrets.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/config/secrets.yml b/config/secrets.yml index 7c44b43..609fc10 100644 --- a/config/secrets.yml +++ b/config/secrets.yml @@ -21,7 +21,7 @@ development: secret_key_base: <%= ENV["SECRET_KEY_BASE"] %> test: - secret_key_base: <%= ENV["SECRET_KEY_BASE"] %> + secret_key_base: '9489b3eee4eccf317ed77407553e8adc97baca7c74dc7ee33cd93e4c8b69477eea66eaedeb18af0be2679887c7c69c0a28c0fded0a71ea472a8c4laalal19cb' # Do not keep production secrets in the unencrypted secrets file. # Instead, either read values from the environment. From b10c4c3962e4b2cf5035a7ec729e06dcea81bed2 Mon Sep 17 00:00:00 2001 From: Constantine Nikolaou Date: Mon, 30 May 2022 22:15:57 +0200 Subject: [PATCH 03/15] Update secret_token.rb to use placeholder when running tests --- config/initializers/secret_token.rb | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/config/initializers/secret_token.rb b/config/initializers/secret_token.rb index cb0d61c..4a24e5e 100644 --- a/config/initializers/secret_token.rb +++ b/config/initializers/secret_token.rb @@ -4,4 +4,8 @@ # If you change this key, all old signed cookies will become invalid! # Make sure the secret is at least 30 characters and all random, # no regular words or you'll be exposed to dictionary attacks. -Rails.application.config.secret_key_base = ENV.fetch("SECRET_KEY_BASE") +if Rails.env.test? + Rails.application.config.secret_key_base = '9489b3eee4eccf317ed77407553e8adc97baca7c74dc7ee33cd93e4c8b69477eea66eaedeb18af0be2679887c7c69c0a28c0fded0a71ea472a8c4laalal19cb' +else + Rails.application.config.secret_key_base = ENV.fetch("SECRET_KEY_BASE") +end From 05d8ea1308ccd7cfb7477bc8be9753a5eee10f72 Mon Sep 17 00:00:00 2001 From: Constantine Nikolaou Date: Mon, 30 May 2022 22:21:59 +0200 Subject: [PATCH 04/15] Refactor setting secret_key_value and add a check on redis config --- config/initializers/secret_token.rb | 6 ++++-- config/initializers/sidekiq.rb | 6 +++++- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/config/initializers/secret_token.rb b/config/initializers/secret_token.rb index 4a24e5e..766dc96 100644 --- a/config/initializers/secret_token.rb +++ b/config/initializers/secret_token.rb @@ -5,7 +5,9 @@ # Make sure the secret is at least 30 characters and all random, # no regular words or you'll be exposed to dictionary attacks. if Rails.env.test? - Rails.application.config.secret_key_base = '9489b3eee4eccf317ed77407553e8adc97baca7c74dc7ee33cd93e4c8b69477eea66eaedeb18af0be2679887c7c69c0a28c0fded0a71ea472a8c4laalal19cb' + secret_key_base = '9489b3eee4eccf317ed77407553e8adc97baca7c74dc7ee33cd93e4c8b69477eea66eaedeb18af0be2679887c7c69c0a28c0fded0a71ea472a8c4laalal19cb' else - Rails.application.config.secret_key_base = ENV.fetch("SECRET_KEY_BASE") + secret_key_base = ENV.fetch("SECRET_KEY_BASE") end + +Rails.application.config.secret_key_base = secret_key_base \ No newline at end of file diff --git a/config/initializers/sidekiq.rb b/config/initializers/sidekiq.rb index bd43c12..5f6e536 100644 --- a/config/initializers/sidekiq.rb +++ b/config/initializers/sidekiq.rb @@ -1,4 +1,8 @@ -redis_url = ENV.fetch("REDIS_URL") +redis_url = "locahost" + +unless Rails.env.test? + redis_url = ENV.fetch("REDIS_URL") +end Sidekiq.configure_server do |config| config.redis = { url: "redis://#{redis_url}:6379/12", :namespace => "mena_devs_com" } From 8df7e03778a6987baff1dad93b213eeddee10b55 Mon Sep 17 00:00:00 2001 From: Constantine Nikolaou Date: Mon, 30 May 2022 22:29:15 +0200 Subject: [PATCH 05/15] Update Github Actions RoR config file --- .github/workflows/rubyonrails.yml | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/.github/workflows/rubyonrails.yml b/.github/workflows/rubyonrails.yml index 96a69dc..d5ecc43 100644 --- a/.github/workflows/rubyonrails.yml +++ b/.github/workflows/rubyonrails.yml @@ -34,6 +34,10 @@ jobs: with: bundler-cache: true # Add or replace database setup steps here + - name: Copy test config + run: cp config/settings/test.yml.sample config/settings/test.yml + - name: Copy env config + run: cp example.env .env - name: Set up database schema run: bin/rails db:schema:load # Add or replace test runners here From 12edc8dc3a9779a43ee3c84d97c97d5b4a01086f Mon Sep 17 00:00:00 2001 From: Constantine Nikolaou Date: Mon, 30 May 2022 22:36:56 +0200 Subject: [PATCH 06/15] revert changes made previously to secret_keys and redis url --- config/initializers/secret_token.rb | 8 +------- config/initializers/sidekiq.rb | 6 +----- 2 files changed, 2 insertions(+), 12 deletions(-) diff --git a/config/initializers/secret_token.rb b/config/initializers/secret_token.rb index 766dc96..dd99e72 100644 --- a/config/initializers/secret_token.rb +++ b/config/initializers/secret_token.rb @@ -4,10 +4,4 @@ # If you change this key, all old signed cookies will become invalid! # Make sure the secret is at least 30 characters and all random, # no regular words or you'll be exposed to dictionary attacks. -if Rails.env.test? - secret_key_base = '9489b3eee4eccf317ed77407553e8adc97baca7c74dc7ee33cd93e4c8b69477eea66eaedeb18af0be2679887c7c69c0a28c0fded0a71ea472a8c4laalal19cb' -else - secret_key_base = ENV.fetch("SECRET_KEY_BASE") -end - -Rails.application.config.secret_key_base = secret_key_base \ No newline at end of file +Rails.application.config.secret_key_base = ENV.fetch("SECRET_KEY_BASE") \ No newline at end of file diff --git a/config/initializers/sidekiq.rb b/config/initializers/sidekiq.rb index 5f6e536..bd43c12 100644 --- a/config/initializers/sidekiq.rb +++ b/config/initializers/sidekiq.rb @@ -1,8 +1,4 @@ -redis_url = "locahost" - -unless Rails.env.test? - redis_url = ENV.fetch("REDIS_URL") -end +redis_url = ENV.fetch("REDIS_URL") Sidekiq.configure_server do |config| config.redis = { url: "redis://#{redis_url}:6379/12", :namespace => "mena_devs_com" } From 0577ae40118d317de2f26b80b24f33b8471decb7 Mon Sep 17 00:00:00 2001 From: Constantine Nikolaou Date: Mon, 30 May 2022 22:39:41 +0200 Subject: [PATCH 07/15] Update test command to rspec --- .github/workflows/rubyonrails.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/rubyonrails.yml b/.github/workflows/rubyonrails.yml index d5ecc43..6cbfdc0 100644 --- a/.github/workflows/rubyonrails.yml +++ b/.github/workflows/rubyonrails.yml @@ -42,7 +42,7 @@ jobs: run: bin/rails db:schema:load # Add or replace test runners here - name: Run tests - run: bin/rake + run: bin/rails rspec lint: runs-on: ubuntu-latest From 3c5d47a83a90ec2ce9d00864d28eea6c06d5cde2 Mon Sep 17 00:00:00 2001 From: Constantine Nikolaou Date: Mon, 30 May 2022 22:49:46 +0200 Subject: [PATCH 08/15] Cleaning up to GH actions commands to run --- .github/workflows/rubyonrails.yml | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/.github/workflows/rubyonrails.yml b/.github/workflows/rubyonrails.yml index 6cbfdc0..5d538b4 100644 --- a/.github/workflows/rubyonrails.yml +++ b/.github/workflows/rubyonrails.yml @@ -34,15 +34,14 @@ jobs: with: bundler-cache: true # Add or replace database setup steps here - - name: Copy test config + - name: Copy example test.yml database config file run: cp config/settings/test.yml.sample config/settings/test.yml - - name: Copy env config + - name: Copy example env config file run: cp example.env .env - - name: Set up database schema - run: bin/rails db:schema:load - # Add or replace test runners here - - name: Run tests - run: bin/rails rspec + - name: Run DB migrate command + run: bin/rails db:migrate --trace RAILS_ENV=test + - name: Run tests rspec command + run: bin/rails spec lint: runs-on: ubuntu-latest From 839c3a35fdfd6d7957fac44106bdb7e56648754d Mon Sep 17 00:00:00 2001 From: Constantine Nikolaou Date: Mon, 30 May 2022 22:53:46 +0200 Subject: [PATCH 09/15] Add db schema load command before running db migration command under GH actions --- .github/workflows/rubyonrails.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.github/workflows/rubyonrails.yml b/.github/workflows/rubyonrails.yml index 5d538b4..3417710 100644 --- a/.github/workflows/rubyonrails.yml +++ b/.github/workflows/rubyonrails.yml @@ -38,6 +38,8 @@ jobs: run: cp config/settings/test.yml.sample config/settings/test.yml - name: Copy example env config file run: cp example.env .env + - name: Set up database schema + run: bin/rails db:schema:load - name: Run DB migrate command run: bin/rails db:migrate --trace RAILS_ENV=test - name: Run tests rspec command From adc20139693b995d76606dd3392e72d877c13da0 Mon Sep 17 00:00:00 2001 From: Constantine Nikolaou Date: Mon, 30 May 2022 23:02:14 +0200 Subject: [PATCH 10/15] Update gems and syntax of GH actions to install Ruby 2.6.5 version --- .github/workflows/rubyonrails.yml | 5 ++--- Gemfile | 4 ++-- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/.github/workflows/rubyonrails.yml b/.github/workflows/rubyonrails.yml index 3417710..c2f6f64 100644 --- a/.github/workflows/rubyonrails.yml +++ b/.github/workflows/rubyonrails.yml @@ -28,12 +28,11 @@ jobs: steps: - name: Checkout code uses: actions/checkout@v3 - # Add or replace dependency steps here - name: Install Ruby and gems - uses: ruby/setup-ruby@8f312efe1262fb463d906e9bf040319394c18d3e # v1.92 + uses: actions/setup-ruby@v1 with: + ruby-version: 2.6.5 bundler-cache: true - # Add or replace database setup steps here - name: Copy example test.yml database config file run: cp config/settings/test.yml.sample config/settings/test.yml - name: Copy example env config file diff --git a/Gemfile b/Gemfile index dd27784..2d66181 100644 --- a/Gemfile +++ b/Gemfile @@ -62,8 +62,6 @@ group :development do gem 'awesome_print' gem 'better_errors' gem 'binding_of_caller' - gem 'brakeman', require: false - gem 'bundler-audit', '>= 0.5.0', require: false gem 'guard', '>= 2.2.2', require: false gem 'guard-livereload', require: false gem 'guard-minitest', require: false @@ -91,6 +89,8 @@ group :test do end group :development, :test do + gem 'brakeman', require: false + gem 'bundler-audit', '>= 0.5.0', require: false gem 'database_cleaner' gem 'json-schema' gem 'factory_bot_rails' From f6ca15f0bab2b4e04f5c3418603b4481043770bc Mon Sep 17 00:00:00 2001 From: Constantine Nikolaou Date: Mon, 30 May 2022 23:06:32 +0200 Subject: [PATCH 11/15] Update GH actions config values --- .github/workflows/rubyonrails.yml | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/.github/workflows/rubyonrails.yml b/.github/workflows/rubyonrails.yml index c2f6f64..daf3e07 100644 --- a/.github/workflows/rubyonrails.yml +++ b/.github/workflows/rubyonrails.yml @@ -29,9 +29,9 @@ jobs: - name: Checkout code uses: actions/checkout@v3 - name: Install Ruby and gems - uses: actions/setup-ruby@v1 + uses: ruby/setup-ruby@v1 with: - ruby-version: 2.6.5 + ruby-version: '2.6.5' bundler-cache: true - name: Copy example test.yml database config file run: cp config/settings/test.yml.sample config/settings/test.yml @@ -50,13 +50,13 @@ jobs: - name: Checkout code uses: actions/checkout@v3 - name: Install Ruby and gems - uses: ruby/setup-ruby@8f312efe1262fb463d906e9bf040319394c18d3e # v1.92 + uses: ruby/setup-ruby@v1 with: + ruby-version: '2.6.5' bundler-cache: true - # Add or replace any other lints here - name: Security audit dependencies - run: bin/bundler-audit --update + run: bundler-audit --update - name: Security audit application code - run: bin/brakeman -q -w2 + run: brakeman -q -w2 - name: Lint Ruby files - run: bin/rubocop --parallel + run: rubocop --parallel From 9913738e7c2765468f85dc4977d2660ce9fdd19f Mon Sep 17 00:00:00 2001 From: Constantine Nikolaou Date: Mon, 30 May 2022 23:10:47 +0200 Subject: [PATCH 12/15] Run commands from cached gems --- .github/workflows/rubyonrails.yml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/.github/workflows/rubyonrails.yml b/.github/workflows/rubyonrails.yml index daf3e07..64882f8 100644 --- a/.github/workflows/rubyonrails.yml +++ b/.github/workflows/rubyonrails.yml @@ -55,8 +55,8 @@ jobs: ruby-version: '2.6.5' bundler-cache: true - name: Security audit dependencies - run: bundler-audit --update + run: bin/bundler-audit --update - name: Security audit application code - run: brakeman -q -w2 + run: bin/brakeman -q -w2 - name: Lint Ruby files - run: rubocop --parallel + run: bin/rubocop --parallel From 1333f54dd086fa4f60a851201c448aefc304bcf6 Mon Sep 17 00:00:00 2001 From: Constantine Nikolaou Date: Mon, 30 May 2022 23:12:10 +0200 Subject: [PATCH 13/15] Comment out lint section on Github actions --- .github/workflows/rubyonrails.yml | 32 +++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/.github/workflows/rubyonrails.yml b/.github/workflows/rubyonrails.yml index 64882f8..9266b4b 100644 --- a/.github/workflows/rubyonrails.yml +++ b/.github/workflows/rubyonrails.yml @@ -44,19 +44,19 @@ jobs: - name: Run tests rspec command run: bin/rails spec - lint: - runs-on: ubuntu-latest - steps: - - name: Checkout code - uses: actions/checkout@v3 - - name: Install Ruby and gems - uses: ruby/setup-ruby@v1 - with: - ruby-version: '2.6.5' - bundler-cache: true - - name: Security audit dependencies - run: bin/bundler-audit --update - - name: Security audit application code - run: bin/brakeman -q -w2 - - name: Lint Ruby files - run: bin/rubocop --parallel + # lint: + # runs-on: ubuntu-latest + # steps: + # - name: Checkout code + # uses: actions/checkout@v3 + # - name: Install Ruby and gems + # uses: ruby/setup-ruby@v1 + # with: + # ruby-version: '2.6.5' + # bundler-cache: true + # - name: Security audit dependencies + # run: bin/bundler-audit --update + # - name: Security audit application code + # run: bin/brakeman -q -w2 + # - name: Lint Ruby files + # run: bin/rubocop --parallel From 475071282e1e419cb58f58c8b13fa9c6523fca8d Mon Sep 17 00:00:00 2001 From: Constantine Nikolaou Date: Sat, 4 Jun 2022 08:28:10 +0200 Subject: [PATCH 14/15] Update gems. Update test assertion on users_spec.rb --- Gemfile | 2 +- Gemfile.lock | 10 ++++------ spec/requests/api/users_spec.rb | 5 +++-- 3 files changed, 8 insertions(+), 9 deletions(-) diff --git a/Gemfile b/Gemfile index 2d66181..25447bc 100644 --- a/Gemfile +++ b/Gemfile @@ -91,7 +91,7 @@ end group :development, :test do gem 'brakeman', require: false gem 'bundler-audit', '>= 0.5.0', require: false - gem 'database_cleaner' + gem 'database_cleaner-active_record' gem 'json-schema' gem 'factory_bot_rails' end \ No newline at end of file diff --git a/Gemfile.lock b/Gemfile.lock index 0a09791..b89280c 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -26,7 +26,7 @@ GEM erubi (~> 1.4) rails-dom-testing (~> 2.0) rails-html-sanitizer (~> 1.0, >= 1.0.3) - active_type (2.1.2) + active_type (2.2.0) activerecord (>= 3.2) activejob (5.2.8) activesupport (= 5.2.8) @@ -127,8 +127,6 @@ GEM countries (~> 4.2) sort_alphabetical (~> 1.1) crass (1.0.6) - database_cleaner (2.0.1) - database_cleaner-active_record (~> 2.0.0) database_cleaner-active_record (2.0.1) activerecord (>= 5.a) database_cleaner-core (~> 2.0.0) @@ -340,7 +338,7 @@ GEM minitest (5.15.0) multi_json (1.15.0) multi_xml (0.6.0) - multipart-post (2.1.1) + multipart-post (2.2.0) nenv (0.3.0) nested_form (0.3.2) netrc (0.11.0) @@ -383,7 +381,7 @@ GEM parser (3.1.2.0) ast (~> 2.4.1) pg (1.3.5) - pgcli-rails (0.6.2) + pgcli-rails (0.6.3) railties (>= 4.2.0) postmark (1.22.0) json @@ -605,7 +603,7 @@ DEPENDENCIES config connection_pool country_select - database_cleaner + database_cleaner-active_record devise (>= 4.7.1) dotenv-rails (>= 2.0.0) draper diff --git a/spec/requests/api/users_spec.rb b/spec/requests/api/users_spec.rb index db6e5ff..4e7df44 100644 --- a/spec/requests/api/users_spec.rb +++ b/spec/requests/api/users_spec.rb @@ -13,7 +13,7 @@ response '200', 'Return verified users' do schema '$ref' => '#/components/schemas/users' - + let!(:user) { create(:user) } let!(:api_key) { create(:api_key, user: user) } let!(:user_1) { create(:user) } @@ -90,13 +90,14 @@ schema schema '$ref' => '#/components/schemas/users' let!(:user) { create(:user) } - let!(:member) { create(:user) } + let!(:member) { create(:user, first_name: 'user-to-find') } let!(:api_key) { create(:api_key, user: user) } let!("query[first_name]") { member.first_name } let(:Authorization) { 'Bearer ' + api_key.access_token } run_test! do |response| data = JSON.parse(response.body) + expect(data['data'].size).to eq(1) expect(data['data'].first['id']).to eq(member.custom_identifier) expect(data['data'].first['type']).to eq("user") expect(data['data'].first['attributes']['first_name']).to eq(member.first_name) From 3f927727ce6ec34289931c68eaf2223c9704b327 Mon Sep 17 00:00:00 2001 From: Constantine Nikolaou Date: Sat, 4 Jun 2022 08:50:55 +0200 Subject: [PATCH 15/15] Mock calls to Slack API before assertions --- spec/models/profile_spec.rb | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/spec/models/profile_spec.rb b/spec/models/profile_spec.rb index fa16563..71fb31c 100644 --- a/spec/models/profile_spec.rb +++ b/spec/models/profile_spec.rb @@ -76,7 +76,7 @@ it "should clear avatar from Slack fields if user is not Slack member" do expect(incomplete_profile.avatar_from_slack).to eq('my_profile_picture.png') expect(incomplete_profile.avatar_from_slack_imported).to eq(true) - + expect(incomplete_profile.reload_avatar_from_slack).to be(true) expect(incomplete_profile.avatar_from_slack).to eq('') @@ -98,8 +98,12 @@ let(:profile) { create(:profile) } it "should import user's avatar from Slack if user is a Slack member" do + slack_user_info = file_fixture("slack_user_info.json").read + json = JSON.parse(slack_user_info) + allow(SlackApi).to receive(:get_user_info).and_return(json) slack_user_image = file_fixture("slack_profile_picture.png") allow(URI).to receive(:parse).with(anything()).and_return(slack_user_image) + expect(profile.download_slack_avatar('https://api.slack.com')).to be(true) expect(profile.avatar_from_slack_imported).to be(true) expect(profile.avatar_from_slack_updated_at).not_to be(nil)