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

:tada: saas_public #17

@@ -1,2 +1,2 @@
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html).
from . import test_saas
from . import test_saas, common_saas_test
@@ -0,0 +1,63 @@
# Copyright 2019 Denis Mudarisov <https://it-projects.info/team/trojikman>
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html).
DB_TEMPLATE_1 = 'db_template_1'
DB_TEMPLATE_2 = 'db_template_2'
MODULE_TO_INSTALL = 'mail'
TEMPLATE_TEST_SUBJECT = 'Dummy subject name to test that code is applied on template database'
BUILD_TEST_SUBJECT = 'Dummy subject name to test that code is applied on build database'
DEFAULT_BUILD_PYTHON_CODE = """# Available variables:
# - env: Odoo Environment on which the action is triggered
# - time, datetime, dateutil, timezone: useful Python libraries
# - log: log(message, level='info'): logging function to record debug information in ir.logging table
# - Warning: Warning Exception to use with raise
# To return an action, assign: action = {{...}}
# You can specify places for variables that can be passed when creating a build like this:
# env['{key_name_1}'].create({{'subject': '{key_name_2}', }})
# When you need curly braces in build post init code use doubling for escaping\n\n\n\n"""


class Common(object):
def setup_saas_env(self):
self.env = self.env(context=dict(
self.env.context,
test_queue_job_no_delay=True,
))

self.saas_template_1 = self.env['saas.template'].create({
'template_module_ids': [(0, 0, {
'name': MODULE_TO_INSTALL,
})],
'template_post_init': 'env[\'mail.message\'].create({\'subject\': \'' + TEMPLATE_TEST_SUBJECT + '\', })',
'build_post_init': DEFAULT_BUILD_PYTHON_CODE + 'env[\'{mail_message}\'].create({{\'subject\': \'' + BUILD_TEST_SUBJECT + '\', }})',
})

self.saas_template_2 = self.env['saas.template'].create({
'template_module_ids': [(0, 0, {
'name': 'mail',
})],
'template_post_init': 'env[\'mail.message\'].create({\'subject\': \'' + TEMPLATE_TEST_SUBJECT + '\', })',
})

self.saas_operator_1 = self.env['saas.operator'].create({
'type': 'local',
'db_url_template': 'http://{db_name}.{db_id}.127.0.0.1.nip.io:8069',
'master_url': 'http://saas.127.0.0.1.nip.io:8069',
})

self.saas_operator_2 = self.env['saas.operator'].create({
'type': 'local',
'db_url_template': 'http://{db_name}.{db_id}.127.0.0.1.nip.io:8069',
'master_url': 'http://saas.127.0.0.1.nip.io:8069',
})

self.saas_template_operator_1 = self.env['saas.template.operator'].create({
'template_id': self.saas_template_1.id,
'operator_id': self.saas_operator_1.id,
'operator_db_name': DB_TEMPLATE_1,
})

self.saas_template_operator_2 = self.env['saas.template.operator'].create({
'template_id': self.saas_template_2.id,
'operator_id': self.saas_operator_2.id,
'operator_db_name': DB_TEMPLATE_2,
})
@@ -1,28 +1,15 @@
# Copyright 2018-2019 Denis Mudarisov <https://it-projects.info/team/trojikman>
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html).

from .common_saas_test import Common, DB_TEMPLATE_1, DB_TEMPLATE_2, MODULE_TO_INSTALL, TEMPLATE_TEST_SUBJECT, \
BUILD_TEST_SUBJECT

import odoo
from odoo import SUPERUSER_ID
from odoo.tests.common import tagged, SavepointCase
from odoo.service import db

DB_TEMPLATE_1 = 'db_template_1'
DB_TEMPLATE_2 = 'db_template_2'
DB_INSTANCE_1 = 'db_instance_1'
DB_INSTANCE_2 = 'db_instance_2'
MODULE_TO_INSTALL = 'mail'
TEMPLATE_TEST_SUBJECT = 'Dummy subject name to test that code is applied on template database'
BUILD_TEST_SUBJECT = 'Dummy subject name to test that code is applied on build database'
DEFAULT_BUILD_PYTHON_CODE = """# Available variables:
# - env: Odoo Environment on which the action is triggered
# - time, datetime, dateutil, timezone: useful Python libraries
# - log: log(message, level='info'): logging function to record debug information in ir.logging table
# - Warning: Warning Exception to use with raise
# To return an action, assign: action = {{...}}
# You can specify places for variables that can be passed when creating a build like this:
# env['{key_name_1}'].create({{'subject': '{key_name_2}', }})
# When you need curly braces in build post init code use doubling for escaping\n\n\n\n"""
KEY_VALUES = {'mail_message': 'mail.message'}


@@ -56,49 +43,7 @@ def assert_no_error_in_db(self, dbname):
@classmethod
def setUpClass(cls):
super(TestSaas, cls).setUpClass()
cls.env = cls.env(context=dict(
cls.env.context,
test_queue_job_no_delay=True,
))

cls.saas_template_1 = cls.env['saas.template'].create({
'template_module_ids': [(0, 0, {
'name': MODULE_TO_INSTALL,
})],
'template_post_init': 'env[\'mail.message\'].create({\'subject\': \'' + TEMPLATE_TEST_SUBJECT + '\', })',
'build_post_init': DEFAULT_BUILD_PYTHON_CODE + 'env[\'{mail_message}\'].create({{\'subject\': \'' + BUILD_TEST_SUBJECT + '\', }})',
})

cls.saas_template_2 = cls.env['saas.template'].create({
'template_module_ids': [(0, 0, {
'name': 'mail',
})],
'template_post_init': 'env[\'mail.message\'].create({\'subject\': \'' + TEMPLATE_TEST_SUBJECT + '\', })',
})

cls.saas_operator_1 = cls.env['saas.operator'].create({
'type': 'local',
'db_url_template': 'http://{db_name}.{db_id}.127.0.0.1.nip.io:8069',
'master_url': 'http://saas.127.0.0.1.nip.io:8069',
})

cls.saas_operator_2 = cls.env['saas.operator'].create({
'type': 'local',
'db_url_template': 'http://{db_name}.{db_id}.127.0.0.1.nip.io:8069',
'master_url': 'http://saas.127.0.0.1.nip.io:8069',
})

cls.saas_template_operator_1 = cls.env['saas.template.operator'].create({
'template_id': cls.saas_template_1.id,
'operator_id': cls.saas_operator_1.id,
'operator_db_name': DB_TEMPLATE_1,
})

cls.saas_template_operator_2 = cls.env['saas.template.operator'].create({
'template_id': cls.saas_template_2.id,
'operator_id': cls.saas_operator_2.id,
'operator_db_name': DB_TEMPLATE_2,
})
Common.setup_saas_env(cls)
This conversation was marked as resolved by trojikman

This comment has been minimized.

Copy link
@yelizariev

yelizariev Jul 8, 2019

Member

You can inherit Common and say self.setup_saas_env()

This comment has been minimized.

Copy link
@trojikman

trojikman Jul 8, 2019

Author Member

Then I will have trouble in saas' class method

This comment has been minimized.

Copy link
@yelizariev

yelizariev Jul 8, 2019

Member

You can use multiple inheritance

This comment has been minimized.

Copy link
@trojikman

trojikman Jul 8, 2019

Author Member

You can inherit Common and say self.setup_saas_env()

I can't use self here

This comment has been minimized.

Copy link
@trojikman

trojikman Jul 8, 2019

Author Member

So, actually I can use cls.setup_saas_env(cls)

This comment has been minimized.

Copy link
@trojikman

trojikman Jul 8, 2019

Author Member

I need to pass cls because of this error TypeError: setup_saas_env() missing 1 required positional argument: 'self'

This comment has been minimized.

Copy link
@yelizariev

yelizariev Jul 8, 2019

Member

Your error doesn't mean that you cannot follow my advice. How do you inherit? Do you use class TestSaas(SavepointCase, Common):?

This comment has been minimized.

Copy link
@yelizariev

yelizariev Jul 8, 2019

Member

Also, check the decorator for setup_saas_env

This comment has been minimized.

Copy link
@yelizariev

yelizariev Jul 8, 2019

Member

I can't use self here

image

This comment has been minimized.

Copy link
@trojikman

trojikman Jul 8, 2019

Author Member

class TestSaas(SavepointCase, Common):

For now everything works with class TestSaas(SavepointCase, Common):


def test_template_operator(self):
# FIXME: that check needed when last tests didn't pass, not sure that it is correct way to drop db
@@ -6,7 +6,9 @@
SaaS Public
===============

This module can be useful when creating public builds for ready-made templates by link.
Create builds for non-authenticated users.
This conversation was marked as resolved by trojikman

This comment has been minimized.

Copy link
@yelizariev

yelizariev Jul 8, 2019

Member
Suggested change
Create builds for non-authenticated users.
Create builds for non-authenticated users:

* User opens a link and gets a build

Credits
=======
@@ -37,4 +39,4 @@ Changelog: `<doc/changelog.rst>`_

Notifications on updates: `via Atom <https://github.com/it-projects-llc/saas-addons/commits/12.0/saas_public.atom>`_, `by Email <https://blogtrottr.com/?subscribe=https://github.com/it-projects-llc/saas-addons/commits/12.0/saas_public.atom>`_

Tested on Odoo 12.0 {ODOO_COMMIT_SHA_TO_BE_UPDATED}
Tested on Odoo 12.0 4b22f1ff5e7c350fa37459c106f52305e26f2ec9
@@ -5,7 +5,7 @@
"summary": """Module for creating public builds""",
"category": "SaaS",
"images": [],
"version": "12.1.0.0",
"version": "12.0.1.0.0",
"application": False,

"author": "IT-Projects LLC, Denis Mudarisov",
@@ -3,12 +3,17 @@

from odoo.http import route, request, Controller


class SaaSPublicController(Controller):
@route('/saas_public/<model("saas.template"):template_id>/create-fast-build', type='http', auth='public')
def create_fast_build(self, template_id, **kwargs):
if not kwargs:
kwargs = {}
template_operator_id = template_id.sudo().operator_ids.random_ready_operator()
build = template_operator_id.sudo().create_db(kwargs, with_delay=False)
build_url = build.get_url()
return request.env['auth_quick_master.token'].sudo().redirect_with_token(build_url, build.id, build_login='admin')
if template_id and template_id.sudo().public_access:
template_operator_id = template_id.sudo().operator_ids.random_ready_operator()
build = template_operator_id.sudo().create_db(kwargs, with_delay=False)
build_url = build.get_url()
return request.env['auth_quick_master.token'].sudo().redirect_with_token(build_url, build.id,
build_login='admin')
else:
return False
This conversation was marked as resolved by trojikman

This comment has been minimized.

Copy link
@yelizariev

yelizariev Jul 8, 2019

Member

Use 404\403

@@ -1,7 +1,7 @@
<!-- Copyright 2019 Denis Mudarisov <https://it-projects.info/team/trojikman>
License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html).-->
<odoo>
<record id="saas.saas_template_operator" model="saas.template.operator">
<record id="saas.saas_template" model="saas.template">
<field name="public_access" eval="True"/>
</record>
</odoo>
@@ -4,7 +4,10 @@

Installation
============
* Installation is similar to SaaS Base module

* Follow instruction of `Job Queue <https://github.com/OCA/queue/tree/12.0/queue_job>`__ module.
* `Install <https://odoo-development.readthedocs.io/en/latest/odoo/usage/install-module.html>`__ this module in a usual way
* Restart the odoo as required by the Job Queue module
This conversation was marked as resolved by trojikman

This comment has been minimized.

Copy link
@yelizariev

yelizariev Jul 8, 2019

Member

You shall not copy-paste instruction. Just use proper reference. * Installation is similar to SaaS Base module doesn't look friendly (no link to the module). Use link to the github


Configuration
=============
@@ -18,7 +21,7 @@ Configuration
Usage
=====

* Open menu ``[[ SaaS ]]>> Templates``
* Open menu ``[[ SaaS ]] >> Templates``
* Choose or create Template that has Template's deployment(s) with public access
* Copy link from Fast URL field
This conversation was marked as resolved by trojikman

This comment has been minimized.

* Open that link in browser either as logged in or logged out user
@@ -1,16 +1,10 @@
# Copyright 2019 Denis Mudarisov <https://it-projects.info/team/trojikman>
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html).

import random
from odoo import models, fields, api
from odoo import models, fields


class SAASTemplateOPerator(models.Model):
_inherit = 'saas.template.operator'
class SAASTemplate(models.Model):
_inherit = 'saas.template'

public_access = fields.Boolean(default=False)
This conversation was marked as resolved by trojikman

This comment has been minimized.

Copy link
@yelizariev

yelizariev Jul 2, 2019

Member

It must be per template, not per template-operator


@api.multi
def random_ready_operator(self):
ready_operators = self.filtered(lambda r: r.state == 'done' and r.public_access)
return random.choice(ready_operators)
Binary file not shown.
@@ -0,0 +1,2 @@
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html).
from . import test_saas_public
@@ -1,15 +1,25 @@
# Copyright 2018-2019 Denis Mudarisov <https://it-projects.info/team/trojikman>
# Copyright 2019 Denis Mudarisov <https://it-projects.info/team/trojikman>
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html).
from odoo.addons.saas.tests.common_saas_test import Common, DB_TEMPLATE_1, DB_TEMPLATE_2
from odoo.tests.common import tagged, HttpCase
from odoo.service import db


# @tagged('post_install', 'at_install')
# class TestSaas(HttpCase):
# def test_saas_public(self):
# template_id = self.saas_template_1.id
# url = '/saas/{}/create-fast-build'.format(template_id)
# r = self.url_open(url)
# self.assertEqual(r.status_code, 200, 'User must be redirected to the build')
# self.assertIn('fast_build_001', db.list_dbs())
# self.assert_no_error_in_db('fast_build_001')
@tagged('post_install', 'at_install')
class TestSaasPublic(HttpCase, Common):
def setUp(self):
super(TestSaasPublic, self).setUp()
self.setup_saas_env()

def test_saas_public(self):
if DB_TEMPLATE_1 in db.list_dbs():
db.exp_drop(DB_TEMPLATE_1)
if DB_TEMPLATE_2 in db.list_dbs():
db.exp_drop(DB_TEMPLATE_2)
if 'template_database' in db.list_dbs():
db.exp_drop('template_database')
self.env['saas.template.operator'].preparing_template_next()
template_id = self.saas_template_1.id
url = '/saas_public/{}/create-fast-build'.format(template_id)
r = self.url_open(url)
self.assertIn(r.status_code, [200, 204], 'User must be redirected to the build')
This conversation was marked as resolved by trojikman

This comment has been minimized.

Copy link
@yelizariev

yelizariev Jul 8, 2019

Member

Add another test to check public_access setting (it should return 404\403)

This comment has been minimized.

Copy link
@trojikman

trojikman Jul 8, 2019

Author Member

In fact, I now realized that this is an incorrect test, because saas_template_1 doesn't have public_access = True

@@ -3,11 +3,11 @@

<odoo>
<record id="saas_template_operator_form_view" model="ir.ui.view">
<field name="name">saas.template.operator</field>
<field name="model">saas.template.operator</field>
<field name="inherit_id" ref="saas.saas_template_operator_form_view"/>
<field name="name">saas.template</field>
<field name="model">saas.template</field>
<field name="inherit_id" ref="saas.saas_template_form_view"/>
<field name="arch" type="xml">
<xpath expr="//field[@name='operator_id']" position="after">
<xpath expr="//field[@name='template_demo']" position="after">
<field name="public_access"/>
</xpath>
</field>
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.