summaryrefslogtreecommitdiff
path: root/files/puppet/modules/pixelated/files/functional-tests/steps/__init__.py
blob: dbc9cc65f04874c433e26960b83f7e54da032d3c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#
# Copyright (c) 2015 ThoughtWorks, Inc.
#
# Pixelated is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Pixelated is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with Pixelated. If not, see <http://www.gnu.org/licenses/>.
import os
import subprocess
import couchdb
import shutil
LEAP_HOME_FOLDER = os.environ.get('LEAP_HOME_FOLDER', '/var/lib/pixelated/.leap/')


def detect_hostname():
    return os.environ.get('TESTHOST') or  subprocess.check_output(['hostname', '-d']).strip()

hostname = detect_hostname()

user_agent_address = 'https://%s' % hostname


def url_home(port=None):
    if port is not None:
        return '%s:%d' % (user_agent_address, port)
    else:
        return user_agent_address


def login_url():
    return url_home(port=8083) + '/login'


def logout_url():
    return url_home(port=8083) + '/logout'


def signup_url():
    return url_home() + '/signup'


def leap_login_url():
    return url_home() + '/login'


def behave_email():
    return '%s@%s' % (behave_testuser(), hostname)


def behave_password():
    return 'Eido6aeg3za9ooNiekiemahm'


def behave_testuser():
    return 'behave-testuser'


def _netrc_couch_credentials():
    with open('/etc/couchdb/couchdb.netrc', 'r') as netrc:
        netrc_line = netrc.readline().strip().split(' ')
    credentials = {}
    for index in xrange(0, len(netrc_line), 2):
        credentials[netrc_line[index]] = netrc_line[index+1]
    return credentials


def _delete_identity(server, username):
    email = '%s@%s' % (username, detect_hostname())
    filter_by_user_id = '''function(doc) { if (doc['address']=='%s') { emit(doc, null);}  }''' % email
    identities = server['identities']
    user_identities = identities.query(filter_by_user_id)
    for ident in user_identities:
        doc = identities.get(ident['id'])
        identities.delete(doc)


def _delete_data(server, user_id):
    user_db = 'user-%s' % user_id
    if user_db in server:
        del server[user_db]


def delete_soledad_server_db(user_id, username):
    couch_credentials = _netrc_couch_credentials()
    server = couchdb.Server("http://%(login)s:%(password)s@%(machine)s:5984" % couch_credentials)
    _delete_identity(server, username)
    _delete_data(server, user_id)    


def delete_soledad_client_db(user_id):
    soledad_folder = LEAP_HOME_FOLDER + user_id
    if os.path.exists(soledad_folder):
        shutil.rmtree(soledad_folder)