34 Commits

Author SHA1 Message Date
8903ae2624 Merge pull request 'Fix XMPP API POST request' (#17) from bugfix/faraday_post into master
All checks were successful
continuous-integration/drone/push Build is passing
continuous-integration/drone/tag Build is passing
Reviewed-on: #17
2020-12-13 13:17:57 +00:00
26e9073674 Fix XMPP API POST request
All checks were successful
continuous-integration/drone/pr Build is passing
continuous-integration/drone/push Build is passing
Faraday does not turn hashes into JSON by itself apparently.
2020-12-13 14:07:25 +01:00
73a89c2601 Merge pull request 'Add missing port number to ejabberd API base URL' (#16) from bugfix/ejabberd_http_port into master
All checks were successful
continuous-integration/drone/push Build is passing
Reviewed-on: #16
2020-12-13 12:57:34 +00:00
7d4dee17b7 Add missing port number to ejabberd API base URL
All checks were successful
continuous-integration/drone/pr Build is passing
continuous-integration/drone/push Build is passing
2020-12-13 13:54:33 +01:00
602ca6ee94 Merge pull request 'Exchange XMPP contacts when invitee signs up' (#13) from feature/automatic_xmpp_roster into master
All checks were successful
continuous-integration/drone/push Build is passing
Reviewed-on: #13
2020-12-09 20:52:14 +00:00
69fc1ca57e Add production dotenv config
All checks were successful
continuous-integration/drone/pr Build is passing
continuous-integration/drone/push Build is passing
2020-12-08 20:34:13 +01:00
ee72a32c7e Exchange XMPP contacts when invitee signs up
All checks were successful
continuous-integration/drone/push Build is passing
continuous-integration/drone/pr Build is passing
2020-12-08 19:16:08 +01:00
8a0d89ef60 Add webmock gem 2020-12-08 18:16:41 +01:00
54af949c7d Add faraday for HTTP requests 2020-12-08 18:16:41 +01:00
6dac732a7f Move invitation invalidation to service 2020-12-08 17:52:53 +01:00
e8c1a6066a Move user db creation to service 2020-12-08 17:39:54 +01:00
44fadb12d6 Merge pull request 'Update link to Chat service' (#11) from chore/update_service_link into master
All checks were successful
continuous-integration/drone/push Build is passing
Reviewed-on: #11
2020-12-04 15:18:49 +00:00
533452469b Update link to Chat service
All checks were successful
continuous-integration/drone/pr Build is passing
continuous-integration/drone/push Build is passing
The wiki page has been moved and improved for new users.
2020-12-04 16:15:17 +01:00
efe168b205 Merge pull request 'Sign up for new account via invitation' (#9) from feature/signup_from_invite into master
All checks were successful
continuous-integration/drone/push Build is passing
continuous-integration/drone/tag Build is passing
Reviewed-on: #9
2020-12-04 12:30:28 +00:00
5b6d6bbd00 Explain ApplicationService
All checks were successful
continuous-integration/drone/pr Build is passing
continuous-integration/drone/push Build is passing
2020-12-04 13:29:07 +01:00
458b585cdb Check off invitation signup feature in README
All checks were successful
continuous-integration/drone/push Build is passing
continuous-integration/drone/pr Build is passing
2020-12-03 15:14:13 +01:00
f651289410 Add mailer host config for test
All checks were successful
continuous-integration/drone/push Build is passing
continuous-integration/drone/pr Build is passing
So Devise can build URLs in confirmation emails
2020-12-03 14:59:21 +01:00
7ca91cf882 Don't run caching steps on CI when not master or PR
Some checks failed
continuous-integration/drone/push Build is passing
continuous-integration/drone/pr Build is failing
2020-12-03 14:56:11 +01:00
022094ce51 Add feature spec for whole signup process
Some checks failed
continuous-integration/drone/push Build is passing
continuous-integration/drone/pr Build is failing
2020-12-03 14:50:02 +01:00
2a2b0a90dc Validate email address properly 2020-12-03 14:49:37 +01:00
e44535daee Don't use deprecated method 2020-12-03 14:49:16 +01:00
c8ccb418b2 Add indexes for invitations table 2020-12-03 14:49:02 +01:00
a792d66c90 Show unused invitations list 2020-12-03 14:48:43 +01:00
f7e48ad3a6 Accept non-existing terms
Legal how does it work
2020-12-03 14:47:57 +01:00
8a7d809b92 Add scopes for invitations 2020-12-03 14:04:58 +01:00
b8e75c7c4a Re-order services on dashboard
All checks were successful
continuous-integration/drone/push Build is passing
Docs and forums are more important for most users than Gitea and Drone.
2020-12-03 13:50:07 +01:00
7a58babd8e Remove obsolete argument
All checks were successful
continuous-integration/drone/push Build is passing
2020-12-03 13:48:23 +01:00
ba31ed559a Improve wording
All checks were successful
continuous-integration/drone/push Build is passing
2020-12-03 00:53:43 +01:00
9cebfd3f58 Signup steps with validation 2020-12-03 00:53:25 +01:00
7aadb5cb51 Require valid invitation to start sign-up process
All checks were successful
continuous-integration/drone/push Build is passing
2020-12-02 19:20:01 +01:00
69b99711e5 Remove fixtures, configure factory generation
All checks were successful
continuous-integration/drone/push Build is passing
2020-12-02 15:40:41 +01:00
e5fe843814 Add task for generating invitations
All checks were successful
continuous-integration/drone/push Build is passing
2020-12-02 15:23:18 +01:00
d7fbda0855 Add basic invitations 2020-12-02 15:22:58 +01:00
18df8fe449 Add account creation service 2020-11-29 17:31:08 +01:00
43 changed files with 934 additions and 40 deletions

View File

@@ -12,6 +12,9 @@ steps:
restore: true
mount:
- vendor
when:
branch:
- master
- name: rspec
image: guildeducation/rails:2.7.1-12.19.0
commands:
@@ -30,6 +33,9 @@ steps:
rebuild: true
mount:
- vendor
when:
branch:
- master
volumes:
- name: cache

View File

@@ -1,8 +1 @@
LDAP_HOST=192.168.33.10
LDAP_PORT=389
#
# Production LDAP server:
#
# LDAP_HOST=ldap.kosmos.org
# LDAP_PORT=636
# LDAP_USE_TLS=true
EJABBERD_API_URL='https://xmpp.kosmos.org/api'

1
.env.production Normal file
View File

@@ -0,0 +1 @@
EJABBERD_API_URL='https://xmpp.kosmos.org:5443/api'

1
.env.test Normal file
View File

@@ -0,0 +1 @@
EJABBERD_API_URL='http://xmpp.example.com/api'

View File

@@ -21,13 +21,15 @@ gem 'jbuilder', '~> 2.7'
# Reduces boot times through caching; required in config/boot.rb
gem 'bootsnap', '>= 1.4.2', require: false
gem 'dotenv-rails', groups: [:development, :test]
gem 'dotenv-rails'
gem 'warden'
gem 'devise'
gem 'devise_ldap_authenticatable'
gem 'net-ldap'
gem 'faraday'
group :development, :test do
# Use sqlite3 as the database for Active Record
gem 'sqlite3', '~> 1.4'
@@ -51,6 +53,7 @@ group :test do
gem 'factory_bot_rails'
gem 'capybara'
gem 'database_cleaner'
gem 'webmock'
end
group :production do

View File

@@ -73,6 +73,8 @@ GEM
regexp_parser (~> 1.5)
xpath (~> 3.2)
concurrent-ruby (1.1.7)
crack (0.4.3)
safe_yaml (~> 1.0.0)
crass (1.0.6)
database_cleaner (1.8.5)
devise (4.7.3)
@@ -95,9 +97,12 @@ GEM
factory_bot_rails (6.1.0)
factory_bot (~> 6.1.0)
railties (>= 5.0.0)
faraday (0.17.0)
multipart-post (>= 1.2, < 3)
ffi (1.13.1)
globalid (0.4.2)
activesupport (>= 4.2.0)
hashdiff (0.4.0)
i18n (1.8.5)
concurrent-ruby (~> 1.0)
jbuilder (2.10.1)
@@ -126,6 +131,7 @@ GEM
mini_portile2 (2.4.0)
minitest (5.14.2)
msgpack (1.3.3)
multipart-post (2.1.1)
net-ldap (0.16.3)
nio4r (2.5.4)
nokogiri (1.10.10)
@@ -191,6 +197,7 @@ GEM
rspec-mocks (~> 3.9)
rspec-support (~> 3.9)
rspec-support (3.10.0)
safe_yaml (1.0.5)
sass-rails (6.0.0)
sassc-rails (~> 2.1, >= 2.1.1)
sassc (2.4.0)
@@ -228,6 +235,10 @@ GEM
activemodel (>= 6.0.0)
bindex (>= 0.4.0)
railties (>= 6.0.0)
webmock (3.6.0)
addressable (>= 2.3.6)
crack (>= 0.3.2)
hashdiff (>= 0.4.0, < 2.0.0)
webpacker (4.3.0)
activesupport (>= 4.2)
rack-proxy (>= 0.6.1)
@@ -251,6 +262,7 @@ DEPENDENCIES
devise_ldap_authenticatable
dotenv-rails
factory_bot_rails
faraday
jbuilder (~> 2.7)
letter_opener
letter_opener_web
@@ -268,6 +280,7 @@ DEPENDENCIES
tzinfo-data
warden
web-console (>= 3.3.0)
webmock
webpacker (~> 4.0)
BUNDLED WITH

View File

@@ -10,9 +10,9 @@ credentials, invites, donations, etc..
* [x] Reset account password when logged in, via reset email
* [x] Log in with admin permissions
* [x] View LDAP users as admin
* [x] Sign up for a new account via invitation
* [ ] List my donations
* [ ] Invite new users from your account
* [ ] Sign up for a new account via invite
* [ ] Sign up for a new account by donating upfront
* [ ] Sign up for a new account via proving contributions (via cryptographic signature)
* [ ] ...

View File

@@ -0,0 +1,35 @@
form {
.field_with_errors {
display: inline-block;
}
}
.layout-signup {
label {
display: none;
}
input[type=text], input[type=email], input[type=password] {
font-size: 1.25rem;
padding: 0.5rem 1rem;
}
span.at-sign, span.domain {
font-size: 1.25rem;
}
.error-msg {
color: #bc0101;
}
.actions {
margin-top: 2rem;
}
.accept-terms {
margin-top: 2rem;
font-size: 0.85rem;
line-height: 1.5em;
color: #888;
}
}

View File

@@ -9,6 +9,12 @@ class ApplicationController < ActionController::Base
end
end
def require_user_signed_out
if user_signed_in?
redirect_to root_path and return
end
end
def authorize_admin
http_status :forbidden unless current_user.is_admin?
end

View File

@@ -0,0 +1,49 @@
class InvitationsController < ApplicationController
before_action :require_user_signed_in, except: ["show"]
before_action :require_user_signed_out, only: ["show"]
layout "signup", only: ["show"]
# GET /invitations
def index
@invitations_unused = current_user.invitations.unused
@invitations_used = current_user.invitations.used
end
# GET /invitations/a-random-invitation-token
def show
token = session[:invitation_token] = params[:id]
if Invitation.where(token: token, used_at: nil).exists?
redirect_to signup_path and return
else
flash.now[:alert] = "This invitation either doesn't exist or has already been used."
http_status :unauthorized
end
end
# POST /invitations
def create
@invitation = Invitation.new(user: current_user)
respond_to do |format|
if @invitation.save
format.html { redirect_to @invitation, notice: 'Invitation was successfully created.' }
format.json { render :show, status: :created, location: @invitation }
else
format.html { render :new }
format.json { render json: @invitation.errors, status: :unprocessable_entity }
end
end
end
# DELETE /invitations/1
def destroy
@invitation = current_user.invitations.find(params[:id])
@invitation.destroy
respond_to do |format|
format.html { redirect_to invitations_url }
format.json { head :no_content }
end
end
end

View File

@@ -0,0 +1,108 @@
class SignupController < ApplicationController
before_action :require_user_signed_out
before_action :require_invitation
before_action :set_invitation
before_action :set_new_user, only: ["steps", "validate"]
layout "signup"
def index
@invited_by_name = @invitation.user.address
end
def steps
@step = params[:step].to_i
http_status :not_found unless [1,2,3].include?(@step)
@validation_error = session[:validation_error]
end
def validate
session[:validation_error] = nil
case user_params.keys.first
when "cn"
@user.cn = user_params[:cn]
@user.valid?
session[:new_user] = @user
if @user.errors[:cn].present?
session[:validation_error] = @user.errors[:cn].first # Store user including validation errors
redirect_to signup_steps_path(1) and return
else
redirect_to signup_steps_path(2) and return
end
when "email"
@user.email = user_params[:email]
@user.valid?
session[:new_user] = @user
if @user.errors[:email].present?
session[:validation_error] = @user.errors[:email].first # Store user including validation errors
redirect_to signup_steps_path(2) and return
else
redirect_to signup_steps_path(3) and return
end
when "password"
@user.password = user_params[:password]
@user.password_confirmation = user_params[:password]
@user.valid?
session[:new_user] = @user
if @user.errors[:password].present?
session[:validation_error] = @user.errors[:password].first # Store user including validation errors
redirect_to signup_steps_path(3) and return
else
complete_signup
msg = "Almost done! We have sent you an email to confirm your address."
redirect_to(check_your_email_path, notice: msg) and return
end
end
end
private
def user_params
params.require(:user).permit(:cn, :email, :password)
end
def require_invitation
if session[:invitation_token].blank?
flash.now[:alert] = "You need an invitation to sign up for an account."
http_status :unauthorized
elsif !valid_invitation?(session[:invitation_token])
flash.now[:alert] = "This invitation either doesn't exist or has already been used."
http_status :unauthorized
end
@invitation = Invitation.find_by(token: session[:invitation_token])
end
def valid_invitation?(token)
Invitation.where(token: session[:invitation_token], used_at: nil).exists?
end
def set_invitation
@invitation = Invitation.find_by(token: session[:invitation_token])
end
def set_new_user
if session[:new_user].present?
@user = User.new(session[:new_user])
else
@user = User.new(ou: "kosmos.org")
end
end
def complete_signup
session[:new_user] = nil
session[:validation_error] = nil
CreateAccount.call(
username: @user.cn,
domain: "kosmos.org",
email: @user.email,
password: @user.password,
invitation: @invitation
)
end
end

View File

@@ -0,0 +1,2 @@
module InvitationsHelper
end

View File

@@ -0,0 +1,2 @@
module SignupHelper
end

View File

@@ -0,0 +1,15 @@
require 'mail'
module EmailValidatable
extend ActiveSupport::Concern
class EmailValidator < ActiveModel::EachValidator
def validate_each(record, attribute, value)
begin
a = Mail::Address.new(value)
rescue Mail::Field::ParseError
record.errors[attribute] << (options[:message] || "is not a valid address")
end
end
end
end

20
app/models/invitation.rb Normal file
View File

@@ -0,0 +1,20 @@
class Invitation < ApplicationRecord
# Relations
belongs_to :user
# Validations
validates_presence_of :user
# Hooks
before_create :generate_token
# Scopes
scope :unused, -> { where(used_at: nil) }
scope :used, -> { where.not(used_at: nil) }
private
def generate_token
self.token = SecureRandom.hex(8)
end
end

View File

@@ -1,4 +1,14 @@
class User < ApplicationRecord
include EmailValidatable
# Relations
has_many :invitations, dependent: :destroy
validates_uniqueness_of :cn
validates_length_of :cn, :minimum => 3
validates_uniqueness_of :email
validates :email, email: true
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
devise :ldap_authenticatable,
@@ -33,4 +43,13 @@ class User < ApplicationRecord
false
end
end
def address
"#{self.cn}@#{self.ou}"
end
def valid_attribute?(attribute_name)
self.valid?
self.errors[attribute_name].blank?
end
end

View File

@@ -0,0 +1,7 @@
class ApplicationService
# This enables executing a service's `#call` method directly via
# `MyService.call(args)`, without creating a class instance it first.
def self.call(*args, &block)
new(*args, &block).call
end
end

View File

@@ -0,0 +1,83 @@
class CreateAccount < ApplicationService
def initialize(args)
@username = args[:username]
@domain = args[:ou] || "kosmos.org"
@email = args[:email]
@password = args[:password]
@invitation = args[:invitation]
end
def call
user = create_user_in_database
add_ldap_document
if @invitation.present?
update_invitation(user.id)
exchange_xmpp_contacts
end
end
private
def create_user_in_database
User.create!(
cn: @username,
ou: @domain,
email: @email,
password: @password,
password_confirmation: @password
)
end
def update_invitation(user_id)
@invitation.update! invited_user_id: user_id, used_at: DateTime.now
end
def add_ldap_document
dn = "cn=#{@username},ou=kosmos.org,cn=users,dc=kosmos,dc=org"
attr = {
objectclass: ["top", "account", "person", "extensibleObject"],
cn: @username,
sn: @username,
uid: @username,
mail: @email,
userPassword: Devise.ldap_auth_password_builder.call(@password)
}
ldap_client.add(dn: dn, attributes: attr)
end
def ldap_client
ldap_client ||= Net::LDAP.new host: ldap_config['host'],
port: ldap_config['port'],
encryption: ldap_config['ssl'],
auth: {
method: :simple,
username: ldap_config['admin_user'],
password: ldap_config['admin_password']
}
end
def ldap_config
ldap_config ||= YAML.load(ERB.new(File.read("#{Rails.root}/config/ldap.yml")).result)[Rails.env]
end
def exchange_xmpp_contacts
#TODO enable in development when we have easy setup of ejabberd etc.
return if Rails.env.development?
ejabberd = EjabberdApiClient.new
inviter = @invitation.user
ejabberd.add_rosteritem({
"localuser": @username, "localhost": @domain,
"user": inviter.cn, "host": inviter.ou,
"nick": inviter.cn, "group": "Friends", "subs": "both"
})
ejabberd.add_rosteritem({
"localuser": inviter.cn, "localhost": inviter.ou,
"user": @username, "host": @domain,
"nick": @username, "group": "Friends", "subs": "both"
})
end
end

View File

@@ -0,0 +1,20 @@
class EjabberdApiClient
def initialize
@base_url = ENV["EJABBERD_API_URL"]
end
def post(endpoint, payload)
res = Faraday.post("#{@base_url}/#{endpoint}", payload.to_json,
"Content-Type" => "application/json")
if res.status != 200
Rails.logger.error "[ejabberd] API request failed:"
Rails.logger.error res.body
#TODO add some kind of exception tracking/notifications
end
end
def add_rosteritem(payload)
post "add_rosteritem", payload
end
end

View File

@@ -6,23 +6,11 @@
</p>
<div class="grid services">
<div class="grid-item chat">
<h3><%= link_to "Chat", "https://wiki.kosmos.org/Services:XMPP" %></h3>
<h3><%= link_to "Chat", "https://wiki.kosmos.org/Services:Chat" %></h3>
<p>
Chat rooms and instant messaging (XMPP/Jabber)
</p>
</div>
<div class="grid-item gitea">
<h3><%= link_to "Gitea", "https://gitea.kosmos.org" %></h3>
<p>
Code hosting and collaboration for software projects
</p>
</div>
<div class="grid-item gitea">
<h3><%= link_to "Drone CI", "https://drone.kosmos.org" %></h3>
<p>
Continuous integration for software projects, tied to our Gitea
</p>
</div>
<div class="grid-item wiki">
<h3><%= link_to "Wiki", "https://wiki.kosmos.org" %></h3>
<p>
@@ -35,6 +23,18 @@
Kosmos community forums and user support/help site
</p>
</div>
<div class="grid-item gitea">
<h3><%= link_to "Gitea", "https://gitea.kosmos.org" %></h3>
<p>
Code hosting and collaboration for software projects
</p>
</div>
<div class="grid-item gitea">
<h3><%= link_to "Drone CI", "https://drone.kosmos.org" %></h3>
<p>
Continuous integration for software projects, tied to our Gitea
</p>
</div>
</div>
</section>

View File

@@ -0,0 +1,46 @@
<section>
<h2>Invitations</h2>
<% if @invitations_unused.any? %>
<table>
<thead>
<tr>
<th>URL</th>
</tr>
</thead>
<tbody>
<% @invitations_unused.each do |invitation| %>
<tr>
<td><%= invitation_url(invitation.token) %></td>
</tr>
<% end %>
</tbody>
</table>
<% else %>
<p>
You do not have any invitations to give away yet. All good
things come in time.
</p>
<% end %>
</section>
<% if @invitations_used.any? %>
<h3>Accepted Invitations</h3>
<table>
<thead>
<tr>
<th>URL</th>
<th>Used at</th>
<th>Invited user</th>
</tr>
</thead>
<tbody>
<% @invitations_used.each do |invitation| %>
<tr>
<td><%= invitation_url(invitation.token) %></td>
<td><%= invitation.used_at %></td>
<td><%= User.find(invitation.invited_user_id).address %></td>
</tr>
<% end %>
</tbody>
</table>
<% end %>

View File

@@ -0,0 +1,41 @@
<!DOCTYPE html>
<html>
<head>
<title>Sign up | Kosmos Accounts</title>
<%= csrf_meta_tags %>
<%= csp_meta_tag %>
<meta name="viewport" content="width=device-width, initial-scale=1">
<%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track': 'reload' %>
<%= javascript_pack_tag 'application', 'data-turbolinks-track': 'reload' %>
</head>
<body class="layout-signup">
<div id="wrapper">
<header>
<h1>
<span class ="project-name">Kosmos</span>
<span class ="site-name">Sign Up</span>
<!-- <span class="beta"><span class="bolt">⚡</span> beta</span> -->
</h1>
<% if user_signed_in? %>
<p class="current-user">
Signed in as <strong><%= current_user.cn %>@kosmos.org</strong>.
<%= link_to "Log out", destroy_user_session_path, method: :delete %>
</p>
<% end %>
</header>
<% flash.each do |type, msg| %>
<div class="flash-msg <%= type %>">
<p><%= msg %></p>
</div>
<% end %>
<main>
<%= yield %>
</main>
</div>
</body>
</html>

View File

@@ -1,2 +1,2 @@
<h2>Access forbidden</h2>
<p>Not with those shoes, buddy.</p>
<p>Sorry, you're not allowed to access this page.</p>

View File

@@ -0,0 +1,2 @@
<h2>Not found</h2>
<p>Sorry, this page does not exist.</p>

View File

@@ -0,0 +1,12 @@
<h2>Welcome</h2>
<p>
Hey there! You were invited to sign up for a Kosmos account by
<strong><%= @invited_by_name %></strong>.
</p>
<p>
This invitation can only be used once, and sign-up is currently only possible
by invitation. Seems like you have good friends!
</p>
<p>
<%= link_to "Get started", signup_steps_path(1), class: "next-step" %>
</p>

View File

@@ -0,0 +1,61 @@
<% case @step %>
<% when 1 %>
<h2>Choose a username</h2>
<%= form_for @user, :url => signup_validate_url do |f| %>
<div class="field">
<p>
<%= f.label :cn, 'Username' %><br />
<%= f.text_field :cn, autofocus: true, autocomplete: "username" %>
<span class="at-sign">@</span>
<span class="domain">kosmos.org</span>
</p>
<% if @validation_error.present? %>
<p class="error-msg">Username <%= @validation_error %></p>
<% end %>
</div>
<div class="actions">
<p><%= f.submit "Continue" %></p>
</div>
<% end %>
<% when 2 %>
<h2>What's your email?</h2>
<%= form_for @user, :url => signup_validate_url do |f| %>
<div class="field">
<p>
<%= f.label :email, 'Email address' %><br />
<%= f.email_field :email, autofocus: true, autocomplete: "email" %>
</p>
<% if @validation_error.present? %>
<p class="error-msg">Email <%= @validation_error %></p>
<% end %>
</div>
<div class="actions">
<p><%= f.submit "Continue" %></p>
</div>
<% end %>
<% when 3 %>
<h2>Choose a password</h2>
<%= form_for @user, :url => signup_validate_url do |f| %>
<div class="field">
<p>
<%= f.label :password, 'Password' %><br />
<%= f.password_field :password, autofocus: true %>
</p>
<% if @validation_error.present? %>
<p class="error-msg">Password <%= @validation_error %></p>
<% end %>
</div>
<p class="accept-terms">
<small>
By clicking the button below, you accept our future Terms of Service
and Privacy Policy. Don't worry, they will be excellent!
</small>
</p>
<div class="actions">
<p><%= f.submit "Create account" %></p>
</div>
<% end %>
<% end %>

View File

@@ -33,10 +33,11 @@ module Akkounts
config.generators.system_tests = nil
config.generators do |g|
g.orm :active_record
g.template_engine :erb
g.test_framework :rspec, fixture: true
g.stylesheets false
g.orm :active_record
g.template_engine :erb
g.test_framework :rspec, fixture: true
g.fixture_replacement :factory_bot, suffix_factory: 'factory', dir: 'spec/factories'
g.stylesheets false
end
end
end

View File

@@ -43,4 +43,10 @@ Rails.application.configure do
# Raises error for missing translations.
# config.action_view.raise_on_missing_translations = true
config.action_mailer.default_url_options = {
host: "accounts.kosmos.org",
protocol: "https",
from: "accounts@kosmos.org"
}
end

View File

@@ -3,7 +3,7 @@
en:
devise:
confirmations:
confirmed: "Your email address has been confirmed. You can now log in below."
confirmed: "Thanks for confirming your email address! Your account has been activated."
send_instructions: "You will receive an email with instructions for how to confirm your email address in a moment."
send_paranoid_instructions: "If your email address exists in our database, you will receive an email with instructions for how to confirm your email address in a few minutes."
failure:
@@ -15,7 +15,7 @@ en:
not_found_in_database: "Invalid %{authentication_keys} or password."
timeout: "Your session expired. Please sign in again to continue."
unauthenticated: "You need to sign in or sign up before continuing."
unconfirmed: "You have to confirm your email address before continuing."
unconfirmed: "Please confirm your email address before continuing."
mailer:
confirmation_instructions:
subject: "Confirmation instructions"

View File

@@ -1,11 +1,17 @@
Rails.application.routes.draw do
devise_for :users
get 'welcome', to: 'welcome#index'
get 'check_your_email', to: 'welcome#check_your_email'
get 'signup', to: 'signup#index'
match 'signup/:step', to: 'signup#steps', as: :signup_steps, via: [:get, :post]
post 'signup_validate', to: 'signup#validate'
get 'settings', to: 'settings#index'
post 'settings_reset_password', to: 'settings#reset_password'
get 'welcome', to: 'welcome#index'
get 'check_your_email', to: 'welcome#check_your_email'
resources :invitations, only: ['index', 'show', 'create', 'destroy']
namespace :admin do
root to: 'dashboard#index'

View File

@@ -0,0 +1,14 @@
class CreateInvitations < ActiveRecord::Migration[6.0]
def change
create_table :invitations do |t|
t.string :token
t.integer :user_id
t.integer :invited_user_id
t.datetime :used_at
t.timestamps
end
add_index :invitations, :user_id
add_index :invitations, :invited_user_id
end
end

View File

@@ -10,7 +10,16 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 2020_11_09_090739) do
ActiveRecord::Schema.define(version: 2020_11_30_132533) do
create_table "invitations", force: :cascade do |t|
t.string "token"
t.integer "user_id"
t.integer "invited_user_id"
t.datetime "used_at"
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
end
create_table "users", force: :cascade do |t|
t.string "cn"

View File

@@ -0,0 +1,13 @@
namespace :invitations do
desc "Generate invitations for all users"
task :generate_for_all_users, [:amount_per_user] => :environment do |t, args|
count = 0
User.all.each do |user|
args[:amount_per_user].to_i.times do
user.invitations << Invitation.create(user: user)
count += 1
end
end
puts "Created #{count} new invitations"
end
end

View File

@@ -0,0 +1,6 @@
FactoryBot.define do
factory :invitation do
token { "abcdef123456" }
user
end
end

View File

@@ -0,0 +1,107 @@
require "rails_helper"
RSpec.describe "Signup", type: :feature do
let(:user) { create :user }
describe "Invitation handling" do
before do
@unused_invitation = Invitation.create(user: user)
@used_invitation = Invitation.create(user: user)
@used_invitation.update_attribute :used_at, DateTime.now - 1.day
end
scenario "Follow link for non-existing invitation" do
visit invitation_url(id: "123")
within ".flash-msg.alert" do
expect(page).to have_content("doesn't exist")
end
end
scenario "Follow link for used invitation" do
visit invitation_url(id: @used_invitation.token)
within ".flash-msg.alert" do
expect(page).to have_content("has already been used")
end
end
scenario "Follow link for unused invitation" do
visit invitation_url(id: @unused_invitation.token)
expect(current_url).to eq(signup_url)
expect(page).to have_content("Welcome")
end
end
describe "Signup steps" do
before do
@invitation = Invitation.create(user: user)
visit invitation_url(id: @invitation.token)
click_link "Get started"
end
scenario "Successful signup (happy path galore)" do
expect(page).to have_content("Choose a username")
fill_in "user_cn", with: "tony"
click_button "Continue"
expect(page).to have_content("What's your email?")
fill_in "user_email", with: "tony@example.com"
click_button "Continue"
expect(page).to have_content("Choose a password")
expect(CreateAccount).to receive(:call)
.with(
username: "tony", domain: "kosmos.org",
email: "tony@example.com", password: "a-valid-password",
invitation: Invitation.last
).and_return(true)
fill_in "user_password", with: "a-valid-password"
click_button "Create account"
within ".flash-msg.notice" do
expect(page).to have_content("confirm your address")
end
expect(page).to have_content("close this window or tab now")
end
scenario "Validation errors" do
fill_in "user_cn", with: "t"
click_button "Continue"
expect(page).to have_content("Username is too short")
fill_in "user_cn", with: "jimmy"
click_button "Continue"
expect(page).to have_content("Username has already been taken")
fill_in "user_cn", with: "tony"
click_button "Continue"
fill_in "user_email", with: "tony@"
click_button "Continue"
expect(page).to have_content("Email is not a valid address")
fill_in "user_email", with: ""
click_button "Continue"
expect(page).to have_content("Email can't be blank")
fill_in "user_email", with: "tony@example.com"
click_button "Continue"
fill_in "user_password", with: "123456"
click_button "Create account"
expect(page).to have_content("Password is too short")
expect(CreateAccount).to receive(:call)
.with(
username: "tony", domain: "kosmos.org",
email: "tony@example.com", password: "a-valid-password",
invitation: Invitation.last
).and_return(true)
fill_in "user_password", with: "a-valid-password"
click_button "Create account"
within ".flash-msg.notice" do
expect(page).to have_content("confirm your address")
end
end
end
end

View File

@@ -1,7 +0,0 @@
# Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
one:
uid: MyString
two:
uid: MyString

View File

@@ -0,0 +1,15 @@
require 'rails_helper'
# Specs in this file have access to a helper object that includes
# the InvitationsHelper. For example:
#
# describe InvitationsHelper do
# describe "string concat" do
# it "concats two strings with spaces" do
# expect(helper.concat_strings("this","that")).to eq("this that")
# end
# end
# end
RSpec.describe InvitationsHelper, type: :helper do
pending "add some examples to (or delete) #{__FILE__}"
end

View File

@@ -0,0 +1,15 @@
require 'rails_helper'
# Specs in this file have access to a helper object that includes
# the SignupHelper. For example:
#
# describe SignupHelper do
# describe "string concat" do
# it "concats two strings with spaces" do
# expect(helper.concat_strings("this","that")).to eq("this that")
# end
# end
# end
RSpec.describe SignupHelper, type: :helper do
pending "add some examples to (or delete) #{__FILE__}"
end

View File

@@ -0,0 +1,44 @@
require 'rails_helper'
RSpec.describe Invitation, type: :model do
let(:user) { build :user }
describe "tokens" do
it "requires a user" do
expect { Invitation.create! }.to raise_error(ActiveRecord::RecordInvalid)
end
it "generates a random token when created" do
invitation = Invitation.new(user: user)
invitation.save!
token = invitation.token
expect(token).to be_a(String)
expect(token.length).to eq(16)
invitation_2 = Invitation.create(user: user)
expect(token).not_to eq(invitation_2.token)
end
end
describe "scopes" do
before do
@unused_invitation = create :invitation, user: user
@used_invitation = create :invitation, user: user, used_at: DateTime.now
@used_invitation_2 = create :invitation, user: user, used_at: DateTime.now
end
describe "#unused" do
it "returns unused invitations" do
expect(Invitation.unused.count).to eq(1)
expect(Invitation.unused.first).to eq(@unused_invitation)
end
end
describe "#used" do
it "returns used invitations" do
expect(Invitation.used.count).to eq(2)
expect(Invitation.used.first).to eq(@used_invitation)
end
end
end
end

View File

@@ -20,4 +20,12 @@ RSpec.describe User, type: :model do
expect(user.is_admin?).to be false
end
end
describe "#address" do
let(:user) { build :user, cn: "jimmy", ou: "kosmos.org" }
it "returns the user address" do
expect(user.address).to eq("jimmy@kosmos.org")
end
end
end

View File

@@ -0,0 +1,14 @@
require 'rails_helper'
RSpec.describe "Signups", type: :request do
describe "GET /index" do
context "without invitation" do
it "returns http unauthorized" do
get "/signup"
expect(response).to have_http_status(:unauthorized)
end
end
end
end

View File

@@ -0,0 +1,98 @@
require 'rails_helper'
require 'webmock/rspec'
require 'json'
RSpec.describe CreateAccount, type: :model do
let(:ldap_client_mock) { instance_double(Net::LDAP) }
before do
allow(service).to receive(:ldap_client).and_return(ldap_client_mock)
end
describe "#create_user_in_database" do
let(:service) { CreateAccount.new(
username: 'isaacnewton',
email: 'isaacnewton@example.com',
password: 'bright-ideas-in-autumn'
)}
it "creates a new user record in the akkounts database" do
expect(User.count).to eq(0)
service.send(:create_user_in_database)
expect(User.count).to eq(1)
expect(User.last.cn).to eq("isaacnewton")
expect(User.last.email).to eq("isaacnewton@example.com")
end
end
describe "#update_invitation" do
let(:invitation) { create :invitation }
let(:service) { CreateAccount.new(
username: 'isaacnewton',
email: 'isaacnewton@example.com',
password: 'bright-ideas-in-autumn',
invitation: invitation
)}
before(:each) do
service.send(:update_invitation, 23)
end
it "marks the invitation as used" do
expect(invitation.used_at).not_to be_nil
end
it "saves the invited user's ID" do
expect(invitation.invited_user_id).to eq(23)
end
end
describe "#add_ldap_document" do
let(:service) { CreateAccount.new(
username: 'halfinney',
email: 'halfinney@example.com',
password: 'remember-remember-the-5th-of-november'
)}
it "creates a new document with the correct attributes" do
expect(ldap_client_mock).to receive(:add).with(
dn: "cn=halfinney,ou=kosmos.org,cn=users,dc=kosmos,dc=org",
attributes: {
objectclass: ["top", "account", "person", "extensibleObject"],
cn: "halfinney",
sn: "halfinney",
uid: "halfinney",
mail: "halfinney@example.com",
userPassword: /^{SSHA512}.{171}=/
}
)
service.send(:add_ldap_document)
end
end
describe "#exchange_xmpp_contacts" do
let(:inviter) { create :user, cn: "willherschel", ou: "kosmos.org" }
let(:invitation) { create :invitation, user: inviter }
let(:service) { CreateAccount.new(
username: 'isaacnewton',
email: 'isaacnewton@example.com',
password: 'bright-ideas-in-autumn',
invitation: invitation
)}
before do
stub_request(:post, "http://xmpp.example.com/api/add_rosteritem")
.to_return(status: 200, body: "", headers: {})
end
it "posts add_rosteritem commands to the ejabberd API" do
service.send(:exchange_xmpp_contacts)
expect(WebMock).to have_requested(:post, "http://xmpp.example.com/api/add_rosteritem")
.with { |req| req.body == '{"localuser":"isaacnewton","localhost":"kosmos.org","user":"willherschel","host":"kosmos.org","nick":"willherschel","group":"Friends","subs":"both"}' }
expect(WebMock).to have_requested(:post, "http://xmpp.example.com/api/add_rosteritem")
.with { |req| req.body == '{"localuser":"willherschel","localhost":"kosmos.org","user":"isaacnewton","host":"kosmos.org","nick":"isaacnewton","group":"Friends","subs":"both"}' }
end
end
end