From b2a654a2e4325d89e219e3e9276b1759df3a46cc Mon Sep 17 00:00:00 2001 From: Hubert Lenoir Date: Tue, 4 Jun 2024 17:26:19 +0200 Subject: [PATCH] feat: add tests (#15) --- .github/workflows/ci.yml | 28 ++++++++++++++++++------ .github/workflows/publish.yml | 33 +++++++++++++++++++++++++++++ Gemfile | 6 ++++++ Gemfile.lock | 40 +++++++++++++++++++++++++++++++++++ README.md | 5 +++++ spec/fixtures/mail.txt | 5 +++++ spec/test.rb | 28 ++++++++++++++++++++++++ 7 files changed, 139 insertions(+), 6 deletions(-) create mode 100644 .github/workflows/publish.yml create mode 100644 Gemfile create mode 100644 Gemfile.lock create mode 100644 spec/fixtures/mail.txt create mode 100644 spec/test.rb diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index d13af2f..b947cf5 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -2,12 +2,14 @@ name: 'Continuous Integration' on: push: - tags: - - '*.*.*' + branches: ['master'] + pull_request: + branches: ['master'] + workflow_call: jobs: push: - name: 'Push image' + name: 'Run tests' runs-on: 'ubuntu-latest' permissions: contents: 'read' @@ -24,7 +26,21 @@ jobs: - name: 'Build' uses: 'docker/build-push-action@v5' with: - pull: false - push: true - tags: 'jeanberu/mailcatcher:${{ github.ref_name }},jeanberu/mailcatcher:latest' + tags: 'jeanberu/mailcatcher:latest' platforms: 'linux/386,linux/amd64,linux/arm/v6,linux/arm/v7,linux/arm64/v8' + cache-from: 'type=gha' + cache-to: 'type=gha,mode=max' + - name: 'Start mailcatcher' + run: 'docker run --detach --publish 1025:1025 --publish 1080:1080 jeanberu/mailcatcher:latest' + - name: 'Install Ruby dependencies' + uses: 'ruby/setup-ruby@v1' + with: + ruby-version: '3.3' + bundler-cache: true + - name: 'Wait for mailcatcher' + run: | + while ! nc -z 0.0.0.0 1025 = 3.3' +source 'https://rubygems.org' + +gem 'rspec', '>= 3.13' +gem 'net-smtp', '>= 0.5' +gem 'net-http', '>= 0.4' diff --git a/Gemfile.lock b/Gemfile.lock new file mode 100644 index 0000000..bd55af7 --- /dev/null +++ b/Gemfile.lock @@ -0,0 +1,40 @@ +GEM + remote: https://rubygems.org/ + specs: + diff-lcs (1.5.1) + net-http (0.4.1) + uri + net-protocol (0.2.2) + timeout + net-smtp (0.5.0) + net-protocol + rspec (3.13.0) + rspec-core (~> 3.13.0) + rspec-expectations (~> 3.13.0) + rspec-mocks (~> 3.13.0) + rspec-core (3.13.0) + rspec-support (~> 3.13.0) + rspec-expectations (3.13.0) + diff-lcs (>= 1.2.0, < 2.0) + rspec-support (~> 3.13.0) + rspec-mocks (3.13.1) + diff-lcs (>= 1.2.0, < 2.0) + rspec-support (~> 3.13.0) + rspec-support (3.13.1) + timeout (0.4.1) + uri (0.13.0) + +PLATFORMS + ruby + x86_64-linux + +DEPENDENCIES + net-http (>= 0.4) + net-smtp (>= 0.5) + rspec (>= 3.13) + +RUBY VERSION + ruby 3.3.2p78 + +BUNDLED WITH + 2.5.9 diff --git a/README.md b/README.md index 36482f4..924e2f6 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,10 @@ # Mailcatcher +[![Latest Version](https://img.shields.io/github/release/Jean-Beru/docker-mailcatcher.svg?style=flat-square)](https://github.com/Jean-Beru/docker-mailcatcher/releases) +[![Software License](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square)](LICENCE) +[![Tests](https://github.com/Jean-Beru/docker-mailcatcher/actions/workflows/ci.yml/badge.svg?branch=master)](https://github.com/Jean-Beru/docker-mailcatcher/actions/workflows/ci.yml?query=branch%3Amaster) + + [Mailcatcher](http://mailcatcher.me) catches mail and serves it through a dream. Run with : `docker run -d -p 1025:1025 -p 1080:1080 --name mailcatcher jeanberu/mailcatcher` diff --git a/spec/fixtures/mail.txt b/spec/fixtures/mail.txt new file mode 100644 index 0000000..937b875 --- /dev/null +++ b/spec/fixtures/mail.txt @@ -0,0 +1,5 @@ +Subject: test message +Date: Sat, 23 Jun 2001 16:26:43 +0900 +Message-Id: + +This is a test message. diff --git a/spec/test.rb b/spec/test.rb new file mode 100644 index 0000000..0375df2 --- /dev/null +++ b/spec/test.rb @@ -0,0 +1,28 @@ +require 'json' +require 'net/http' +require 'net/smtp' + +fixtures = File.dirname(__FILE__) + '/fixtures/' +api = Net::HTTP.new('0.0.0.0', 1080) + +RSpec.describe 'Mailcatcher' do + before :all do + api.delete('/messages') + end + + it "can receive an email" do + content = File.open(fixtures + 'mail.txt').read + Net::SMTP.start('0.0.0.0', 1025) do |smtp| + smtp.send_message content, 'to@example.com', 'cc@example.com' + end + + response = JSON.parse(api.get('/messages').body) + + expect(response.count).to eq 1 + expect(response[0]['id']).to eq 1 + expect(response[0]['sender']).to eq '' + expect(response[0]['recipients']).to eq [''] + expect(response[0]['subject']).to eq 'test message' + expect(response[0]['size']).to eq "141" + end +end