require 'helper' module Heft class Users require File.join '.', 'adapters', 'internet' class << self def add(opts={}) headers = {'Authorization' => "HOIST #{ENV['APIKEY']}"} reply = Adapters::Internet.new.execute T::Request.new( :headers => headers, :uri => 'https://auth.hoi.io/user', :body => opts, :verb => :post ) fail "Failed => #{reply.body}" if reply.code > 299 end def contain?(email) headers = {'Authorization' => "HOIST #{ENV['APIKEY']}"} reply = Adapters::Internet.new.execute T::Request.new( :headers => headers, :uri => 'https://auth.hoi.io/user', :body => {:email => email, :password => "******"}, :verb => :post ) json = JSON.parse reply.body error_message = json['message'] reply.code === 400 and error_message.match /already a user with that email address/ end private def all headers = {'Authorization' => "HOIST #{ENV['APIKEY']}", 'Accept' => 'application/json'} reply = Adapters::Internet.new.execute T::Request.new( :headers => headers, :uri => 'https://auth.hoi.io/users', ) fail "Failed => #{reply.body}" if reply.code > 299 puts reply.body end end end end class UniqueEmail class << self def next require 'securerandom' "#{SecureRandom.uuid.to_s}@example.test.com" end end end describe "Hoistapps and users" do it "can be asked to add a new one" do # see: http://docs.hoi.io/authentication/overview/api/authentication-api email = UniqueEmail.next Heft::Users.add :email => email, :password => "xxx_not_a_password_xxx" expect(Heft::Users.contain? email).to be_true, "User was not found" end it "fails when user exists already with the same email" it "can be asked to delete a user" it "fails unless password is <= 6 characters long" end