Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

introduce mobilizon docker #11

Open
wants to merge 16 commits into
base: main
Choose a base branch
from
6 changes: 3 additions & 3 deletions .env
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
username="mobilibr@mt2015.com"
password="experiment"
api="https://mobilizon.libr.events/api"
email="your@email.com"
password="Y0urP4ssw0rd"
api="http://localhost:4000/api"
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,3 @@ groups.json
.identities.json
.vscode
*.ics
.env
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -200,3 +200,16 @@ shared.fetchVariables
shared.getToken
upload.uploadToMobilizon
```


## To run the tests

To set up the local Mobilizon instance, please have [docker](https://docs.docker.com/get-docker/) and [docker-compose](https://docs.docker.com/compose/install/) installed. The integration tests expect a running instance of mobilizon available.

To run the Mobilizon install inside docker:

```docker-compose -f docker-compose-test.yml```

To run the test suites:

```npm tests```
20 changes: 10 additions & 10 deletions bin/login.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,27 @@ const fs = require('fs');
const shared = require('../lib/shared');
const login = require('../lib/login');

nconf.argv().env().file({file: "config.json"});
nconf.argv().env()

async function connectAndSaveTokens() {

shared.integrityChecks({
login: '<login>',
email: '<email>',
password: '<password>',
});

const eventvars = shared.fetchVariables({
login: _.toString,
email: _.toString,
password: _.toString,
api: _.toString,
})

const token = await login.perform(eventvars);
const token = await login.perform(eventvars.email, eventvars.password, eventvars.api);
// the token expire quite often, so a new login every time,
// before posting, would be necessary.
debug("retrived authentication token! ");
debug("retrieved authentication token! ");

const accountInfo = await login.getInfo(token);
const accountInfo = await login.getInfo(token, eventvars.api);

let existing = [];
try {
Expand All @@ -39,19 +39,19 @@ async function connectAndSaveTokens() {
debug(`file ${shared.identity_filename} not found`);
}

const newcontent = _.reject(existing, {server: nconf.get('api')});
const newContent = _.reject(existing, {server: nconf.get('api')});
debug(`Saving token and account info in ${shared.identity_filename} file; this would be used as default`);

newcontent.push({
newContent.push({
identities: accountInfo,
server: nconf.get('api'),
date: moment().toISOString(),
token,
});

fs.writeFileSync(shared.identity_filename,
JSON.stringify(newcontent, undefined, 2), 'utf-8');
console.log(`Saved authentication token in ${shared.identity_filename}. servers supported: [${_.map(newcontent, 'server')}]`);
JSON.stringify(newContent, undefined, 2), 'utf-8');
console.log(`Saved authentication token in ${shared.identity_filename}. Servers supported: [${_.map(newContent, 'server')}]`);
}

connectAndSaveTokens();
Expand Down
26 changes: 26 additions & 0 deletions docker-compose-test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
version: "3"

services:
mobilizon:
image: framasoft/mobilizon
volumes:
- ./public/uploads:/var/lib/mobilizon/uploads
# - ${PWD}/config.exs:/etc/mobilizon/config.exs:ro
# - ${PWD}/GeoLite2-City.mmdb:/var/lib/mobilizon/geo_db/GeoLite2-City.mmdb
ports:
- "4000:4000"
env_file:
docker.env
db:
image: postgis/postgis:13-3.1
volumes:
- ./db:/var/lib/postgresql/data
env_file:
docker.env
ports:
- 5432:5432

networks:
default:
ipam:
driver: default
33 changes: 33 additions & 0 deletions docker.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Copy this file to .env, then update it with your own settings

# Database settings
POSTGRES_USER=mobilizon
POSTGRES_PASSWORD=changethis
POSTGRES_DB=mobilizon

MOBILIZON_DATABASE_USERNAME=mobilizon
MOBILIZON_DATABASE_PASSWORD=changethis
MOBILIZON_DATABASE_DBNAME=mobilizon
MOBILIZON_DATABASE_HOST=db



# Instance configuration
MOBILIZON_INSTANCE_REGISTRATIONS_OPEN=true
MOBILIZON_INSTANCE_NAME=My Mobilizon Instance
MOBILIZON_INSTANCE_HOST=mobilizon.lan
MOBILIZON_INSTANCE_PORT=4000

MOBILIZON_INSTANCE_SECRET_KEY_BASE=changethis
MOBILIZON_INSTANCE_SECRET_KEY=changethis

[email protected]
[email protected]

# Email settings
MOBILIZON_SMTP_SERVER=localhost
MOBILIZON_SMTP_PORT=25
MOBILIZON_SMTP_HOSTNAME=localhost
[email protected]
MOBILIZON_SMTP_PASSWORD=password
MOBILIZON_SMTP_SSL=false
3 changes: 3 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ const jestConfig = {
'^.+\\.jsx?$': 'babel-jest',
},
testMatch: ['**/tests/**/*.js?(x)'],
testPathIgnorePatterns: ["setup.js"],
setupFiles: ["dotenv/config"],
globalSetup: "./tests/setup"
}


module.exports = jestConfig
87 changes: 32 additions & 55 deletions lib/login.js
Original file line number Diff line number Diff line change
@@ -1,66 +1,43 @@
const _ = require('lodash');
const nconf = require('nconf');
const debug = require('debug')('lib:login');

const shared = require('./shared');
var { GraphQLClient, gql } = require('graphql-request')

nconf.argv().env().file({file: "config.json"});

async function perform(userparms) {

const connectToAuthQ = {
"operationName":"Login",
"variables": {
"email": userparms.login,
"password": userparms.password
},
"query":"mutation Login($email: String!, $password: String!) {\n login(email: $email, password: $password) {\n accessToken\n refreshToken\n user {\n id\n email\n role\n __typename\n }\n __typename\n }\n}\n"
};

if(userparms.api) {
debug("Setting as API endpoint %s", userparms.api);
nconf.set('api', userparms.api);
}

let response = null, responseBody = null;
try {
response = await shared.mobilizoneHTTPAPIfetch(connectToAuthQ, null);
responseBody = await response.json();
} catch(error) {
debug(`Login error ${error.message}`);
debug(`Response code ${response.status}`);
throw error;
}

const accessToken = _.get(responseBody, 'data.login.accessToken');
if(!accessToken.length) {
debug("Error, not found the accessToken! %j", responseBody);
throw new Error("accessToken not found");
}
return accessToken;
const loginQuery = gql`mutation Login($email: String!, $password: String!)
{ login(email: $email, password: $password)
{ accessToken refreshToken user
{ id
email
role
__typename }
__typename}\n}\n`
const getIdentityInfoQuery=gql`{ identities { id avatar
{ id url __typename }
type preferredUsername name __typename

}}`
/**
* Performs a login and returns the token
* @param email
* @param password
* @param graphql_endpoint
* @return {String} The token returned by the mobilizon instance
*/
async function perform(email, password, graphql_endpoint) {

const client = new GraphQLClient(graphql_endpoint)
const data = await client.request(loginQuery,{email,password})
return data.login.accessToken
}

async function getInfo(token) {
const getInfoQ = {
"operationName":null,
"variables":{},
"query":"{\n identities {\n id\n avatar {\n id\n url\n __typename\n }\n type\n preferredUsername\n name\n __typename\n }\n}\n"
};
async function getIdentityInfo(token, graphql_endpoint) {

let responseBody = null;
try {
const response = await shared.mobilizoneHTTPAPIfetch(getInfoQ, token);
responseBody = await response.json();
} catch(error) {
console.log(".json decode error?", error);
process.exit(1)
}
const retval = _.get(responseBody, 'data.identities');
debug("Retrieved user info: %j", retval);
return retval;
const client = new GraphQLClient(graphql_endpoint, {headers: {
authorization: 'Bearer '+token,
}})
return await client.request(getIdentityInfoQuery)
}

module.exports = {
perform,
getInfo,
getInfo: getIdentityInfo,
}
10 changes: 6 additions & 4 deletions lib/shared.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,12 @@ const debug = require('debug')('lib:shared');
const IOlog = require('debug')('lib:shared(IO)');
const fetch = require('node-fetch');
const fs = require('fs');
const os = require('os');
const path = require('path');

const IDENTITY_PATH=os.tmpdir()
const IDENTITY_FILENAME = path.join(IDENTITY_PATH,'.identities.json')

const IDENTITY_FILENAME = '.identities.json'
function integrityChecks(specific={}) {
/* this function performs integrity checks to verify everything it is ready to work */
const docs ="https://libr.events/mobilizon-poster";
Expand Down Expand Up @@ -106,8 +110,6 @@ function fetchVariables(varmap) {
const lapz = f(readv);
if(!lapz)
return "--" + name + " invalid format? " + lapz;

console.log(name, lapz);
retval[name] = lapz;
return null;
}));
Expand All @@ -126,5 +128,5 @@ module.exports = {
getToken,
fetchOrganizerId,
identity_filename: IDENTITY_FILENAME,
fetchOrganizerId,
identity_file_path: IDENTITY_PATH,
}
Loading