Add config endpoint

This commit is contained in:
Chocobozzz 2019-03-22 10:53:38 +01:00
parent 0b1845a1fb
commit e864b38ec6
No known key found for this signature in database
GPG key ID: 583A612D890159BE
11 changed files with 157 additions and 11 deletions

View file

@ -12,7 +12,7 @@ config :mobilizon,
config :mobilizon, :instance, config :mobilizon, :instance,
name: System.get_env("MOBILIZON_INSTANCE_NAME") || "Localhost", name: System.get_env("MOBILIZON_INSTANCE_NAME") || "Localhost",
version: "1.0.0-dev", version: "1.0.0-dev",
registrations_open: true registrations_open: System.get_env("MOBILIZON_INSTANCE_REGISTRATIONS_OPEN") || true
config :mime, :types, %{ config :mime, :types, %{
"application/activity+json" => ["activity-json"], "application/activity+json" => ["activity-json"],

40
js/src/graphql/config.ts Normal file
View file

@ -0,0 +1,40 @@
import gql from 'graphql-tag';
export const CREATE_USER = gql`
mutation CreateUser($email: String!, $password: String!) {
createUser(email: $email, password: $password) {
email,
confirmationSentAt
}
}
`;
export const VALIDATE_USER = gql`
mutation ValidateUser($token: String!) {
validateUser(token: $token) {
token,
user {
id,
email,
defaultActor {
id
}
}
}
}
`;
export const CURRENT_USER_CLIENT = gql`
query {
currentUser @client {
id,
email
}
}
`;
export const UPDATE_CURRENT_USER_CLIENT = gql`
mutation UpdateCurrentUser($id: Int!, $email: String!) {
updateCurrentUser(id: $id, email: $email) @client
}
`;

View file

@ -0,0 +1,7 @@
import { ICurrentUser } from '@/types/current-user.model';
export interface ILogin {
user: ICurrentUser,
token: string,
}

View file

@ -0,0 +1,7 @@
defmodule Mobilizon.CommonConfig do
def registrations_open?(), do: instance_config() |> get_in([:registrations_open])
def instance_name(), do: instance_config() |> get_in([:name])
defp instance_config(), do: Application.get_env(:mobilizon, :instance)
end

View file

@ -146,13 +146,8 @@ defmodule Mobilizon.Users.User do
end end
end end
def is_confirmed(%User{confirmed_at: nil} = _user) do def is_confirmed(%User{confirmed_at: nil} = _user), do: {:error, :unconfirmed}
{:error, :unconfirmed} def is_confirmed(%User{} = user), do: {:ok, user}
end
def is_confirmed(%User{} = user) do
{:ok, user}
end
def owns_actor(%User{actors: actors}, actor_id) do def owns_actor(%User{actors: actors}, actor_id) do
case Enum.find(actors, fn a -> a.id == actor_id end) do case Enum.find(actors, fn a -> a.id == actor_id end) do

View file

@ -0,0 +1,14 @@
defmodule MobilizonWeb.Resolvers.Config do
@moduledoc """
Handles the config-related GraphQL calls
"""
require Logger
import Mobilizon.CommonConfig
@doc """
Get config
"""
def get_config(_parent, _params, _context) do
{:ok, %{name: instance_name(), registrations_open: registrations_open?()}}
end
end

View file

@ -3,6 +3,7 @@ defmodule MobilizonWeb.Resolvers.User do
Handles the user-related GraphQL calls Handles the user-related GraphQL calls
""" """
alias Mobilizon.Actors.Actor alias Mobilizon.Actors.Actor
alias Mobilizon.CommonConfig
alias Mobilizon.Users.User alias Mobilizon.Users.User
alias Mobilizon.{Actors, Users} alias Mobilizon.{Actors, Users}
alias Mobilizon.Service.Users.{ResetPassword, Activation} alias Mobilizon.Service.Users.{ResetPassword, Activation}
@ -64,15 +65,23 @@ defmodule MobilizonWeb.Resolvers.User do
end end
@doc """ @doc """
Register an user : Register an user:
- check registrations are enabled
- create the user - create the user
- send a validation email to the user - send a validation email to the user
""" """
@spec create_user(any(), map(), any()) :: tuple() @spec create_user(any(), map(), any()) :: tuple()
def create_user(_parent, args, _resolution) do def create_user(_parent, args, _resolution) do
with {:ok, %User{} = user} <- Users.register(args) do with {:registrations_open, true} <- {:registrations_open, CommonConfig.registrations_open?()},
{:ok, %User{} = user} <- Users.register(args) do
Activation.send_confirmation_email(user) Activation.send_confirmation_email(user)
{:ok, user} {:ok, user}
else
{:registrations_open, false} ->
{:error, "Registrations are not enabled"}
err ->
err
end end
end end

View file

@ -18,6 +18,7 @@ defmodule MobilizonWeb.Schema do
import_types(MobilizonWeb.Schema.Actors.PersonType) import_types(MobilizonWeb.Schema.Actors.PersonType)
import_types(MobilizonWeb.Schema.Actors.GroupType) import_types(MobilizonWeb.Schema.Actors.GroupType)
import_types(MobilizonWeb.Schema.CommentType) import_types(MobilizonWeb.Schema.CommentType)
import_types(MobilizonWeb.Schema.ConfigType)
alias MobilizonWeb.Resolvers alias MobilizonWeb.Resolvers
@ -133,6 +134,7 @@ defmodule MobilizonWeb.Schema do
import_fields(:participant_queries) import_fields(:participant_queries)
import_fields(:tag_queries) import_fields(:tag_queries)
import_fields(:address_queries) import_fields(:address_queries)
import_fields(:config_queries)
end end
@desc """ @desc """

View file

@ -0,0 +1,23 @@
defmodule MobilizonWeb.Schema.ConfigType do
@moduledoc """
Schema representation for User
"""
use Absinthe.Schema.Notation
alias MobilizonWeb.Resolvers.Config
@desc "A config object"
object :config do
# Instance name
field(:name, :string)
field(:registrations_open, :boolean)
end
object :config_queries do
@desc "Get the instance config"
field :config, :config do
resolve(&Config.get_config/3)
end
end
end

View file

@ -0,0 +1,25 @@
defmodule MobilizonWeb.Resolvers.ConfigResolverTest do
alias MobilizonWeb.AbsintheHelpers
use MobilizonWeb.ConnCase
use Bamboo.Test
describe "Resolver: Get config" do
test "get_config/3 returns the instance config", context do
query = """
{
config {
name,
registrationsOpen
}
}
"""
res =
context.conn
|> get("/api", AbsintheHelpers.query_skeleton(query, "config"))
assert json_response(res, 200)["data"]["config"]["name"] == "Localhost"
assert json_response(res, 200)["data"]["config"]["registrationsOpen"] == true
end
end
end

View file

@ -1,11 +1,12 @@
defmodule MobilizonWeb.Resolvers.UserResolverTest do defmodule MobilizonWeb.Resolvers.UserResolverTest do
use MobilizonWeb.ConnCase use MobilizonWeb.ConnCase
alias Mobilizon.{Actors, Users} alias Mobilizon.{Actors, Users, CommonConfig}
alias Mobilizon.Actors.Actor alias Mobilizon.Actors.Actor
alias Mobilizon.Users.User alias Mobilizon.Users.User
alias MobilizonWeb.AbsintheHelpers alias MobilizonWeb.AbsintheHelpers
alias Mobilizon.Service.Users.ResetPassword alias Mobilizon.Service.Users.ResetPassword
import Mobilizon.Factory import Mobilizon.Factory
import Mock
use Bamboo.Test use Bamboo.Test
@valid_actor_params %{email: "test@test.tld", password: "testest", username: "test"} @valid_actor_params %{email: "test@test.tld", password: "testest", username: "test"}
@ -389,6 +390,29 @@ defmodule MobilizonWeb.Resolvers.UserResolverTest do
assert hd(json_response(res, 200)["errors"])["message"] == assert hd(json_response(res, 200)["errors"])["message"] ==
"Email doesn't fit required format" "Email doesn't fit required format"
end end
test "test create_user/3 doesn't create a user when registration is disabled", context do
with_mock CommonConfig, registrations_open?: fn -> false end do
mutation = """
mutation {
createUser(
email: "#{@user_creation.email}",
password: "#{@user_creation.password}",
) {
id,
email
}
}
"""
res =
context.conn
|> post("/api", AbsintheHelpers.mutation_skeleton(mutation))
assert hd(json_response(res, 200)["errors"])["message"] ==
"Registrations are not enabled"
end
end
end end
describe "Resolver: Validate an user" do describe "Resolver: Validate an user" do