瀏覽代碼

unittest's default uid/space_id

walle 6 年之前
父節點
當前提交
8ccc4af4b7

+ 17 - 3
tests/factories.py

@@ -1,11 +1,10 @@
 # -*- coding: utf-8 -*-
 """Factories to help in tests."""
-from factory import PostGenerationMethodCall, Sequence
+from factory import Sequence
 from factory.alchemy import SQLAlchemyModelFactory
-from werkzeug.security import generate_password_hash
-
 from walle.model.database import db
 from walle.model.user import UserModel
+from werkzeug.security import generate_password_hash
 
 
 class BaseFactory(SQLAlchemyModelFactory):
@@ -29,3 +28,18 @@ class UserFactory(BaseFactory):
         """Factory configuration."""
 
         model = UserModel
+
+
+import pytest
+
+
+@pytest.mark.usefixtures('db')
+class TestApiBase:
+
+    def init_vars(self, data):
+        from flask_login import current_user
+        if 'space_id' in data:
+            data['space_id'] = current_user.space_id()
+        if 'user_id' in data:
+            data['user_id'] = current_user.id
+

+ 9 - 3
tests/test_01_api_environment.py

@@ -1,14 +1,14 @@
 # -*- coding: utf-8 -*-
 """Test Apis."""
-from flask import json
-import types
 import urllib
+
 import pytest
+from factories import TestApiBase
 from utils import *
 
 
 @pytest.mark.usefixtures('db')
-class TestApiEnv:
+class TestApiEnv(TestApiBase):
 
     """api role testing"""
     uri_prefix = '/api/environment'
@@ -32,6 +32,12 @@ class TestApiEnv:
         'space_id': 1,
     }
 
+    def test_init(self, user, testapp, client, db):
+        self.init_vars(self.env_data)
+        self.init_vars(self.env_data_2)
+        self.init_vars(self.env_data_remove)
+
+
     def test_create(self, user, testapp, client, db):
         """create successful."""
         # 1.create another role

+ 2 - 2
tests/test_02_api_role.py

@@ -5,10 +5,10 @@ import types
 import urllib
 import pytest
 from utils import *
-
+from factories import TestApiBase
 
 @pytest.mark.usefixtures('db')
-class TestApiRole:
+class TestApiRole(TestApiBase):
     """api role testing"""
     uri_prefix = '/api/role'
 

+ 1 - 1
tests/test_03_api_user.py

@@ -5,7 +5,7 @@ import urllib
 import pytest
 from flask import current_app
 from utils import *
-
+from factories import TestApiBase
 user_data = {
     'email': u'test01@walle-web.io',
     'password': u'Walle987',

+ 7 - 3
tests/test_05_api_space.py

@@ -5,8 +5,9 @@ from flask import current_app
 from utils import *
 from test_00_base import space_base
 from copy import deepcopy
+from factories import TestApiBase
 @pytest.mark.usefixtures('db')
-class TestApiSpace:
+class TestApiSpace(TestApiBase):
     """api role testing"""
     uri_prefix = '/api/space'
 
@@ -34,8 +35,11 @@ class TestApiSpace:
         'members': json.dumps([{"user_id": 1, "role": "MASTER"}, {"user_id": 3, "role": "DEVELOPER"}]),
     }
 
-    def test_setUp(self):
-        pass
+
+    def test_init(self, user, testapp, client, db):
+        self.init_vars(self.space_data)
+        self.init_vars(self.space_data_2)
+        self.init_vars(self.space_data_remove)
 
 
     # 初始化 space_id=1的用户列表

+ 3 - 3
tests/test_06_api_server.py

@@ -1,14 +1,14 @@
 # -*- coding: utf-8 -*-
 """Test Apis."""
-from flask import json
-import types
 import urllib
+
 import pytest
+from factories import TestApiBase
 from utils import *
 
 
 @pytest.mark.usefixtures('db')
-class TestApiServer:
+class TestApiServer(TestApiBase):
     """api role testing"""
     uri_prefix = '/api/server'
 

+ 19 - 11
tests/test_07_api_project.py

@@ -3,12 +3,13 @@
 import urllib
 import json
 import pytest
-from flask import current_app
+from flask import current_app, session, g
 from utils import *
-
+from flask_login import current_user
+from factories import TestApiBase
 
 @pytest.mark.usefixtures('db')
-class TestApiProject:
+class TestApiProject(TestApiBase):
     """api role testing"""
     uri_prefix = '/api/project'
 
@@ -16,7 +17,7 @@ class TestApiProject:
 
     project_data = {
         "environment_id": 1,
-        "space_id": 1,
+        "space_id": 'to be init_vars',
         "excludes": u"*.log",
         "keep_version_num": 11,
         "name": u"walden-瓦尔登",
@@ -34,7 +35,7 @@ class TestApiProject:
         "target_user": u"work",
         "target_port": u"22",
         "task_vars": u"debug=1;\\napp=auotapp.py",
-        "user_id": 1,
+        "user_id": 'to be init_vars',
     }
 
     project_data_members = [
@@ -52,7 +53,7 @@ class TestApiProject:
 
     project_data_2 = {
         "environment_id": 2,
-        "space_id": 1,
+        "space_id": 'to be init_vars',
         "excludes": u"*.log",
         "keep_version_num": 10,
         "name": u"walle-web",
@@ -70,12 +71,12 @@ class TestApiProject:
         "target_user": u"work",
         "target_port": u"22",
         "task_vars": u"debug=1;\\napp=auotapp.py",
-        "user_id": 1
+        "user_id": 'to be init_vars',
     }
 
     project_data_2_update = {
         "environment_id": 1,
-        "space_id": 1,
+        "space_id": 'to be init_vars',
         "excludes": u"*.log",
         "keep_version_num": 11,
         "name": u"walle-web to walden edit",
@@ -93,13 +94,13 @@ class TestApiProject:
         "target_user": u"work",
         "target_port": u"22",
         "task_vars": u"debug=1;\\napp=auotapp.py; project=walden",
-        "user_id": 1
+        "user_id": 'to be init_vars',
     }
 
     project_data_remove = {
         'name': u'this server will be deleted soon',
         "environment_id": 1,
-        "space_id": 1,
+        "space_id": 'to be init_vars',
         "excludes": u"*.log",
         "keep_version_num": 11,
         "post_deploy": u"echo post_deploy",
@@ -116,9 +117,16 @@ class TestApiProject:
         "target_user": u"work",
         "target_port": u"22",
         "task_vars": u"debug=1;\\napp=auotapp.py",
-        "user_id": 1
+        "user_id": 'to be init_vars',
     }
 
+    def test_init(self, user, testapp, client, db):
+        self.init_vars(self.project_data)
+        self.init_vars(self.project_data_2)
+        self.init_vars(self.project_data_remove)
+        self.init_vars(self.project_data_2_update)
+
+
     def test_create(self, user, testapp, client, db):
         """create successful."""
         # 1. create another project

+ 4 - 5
tests/test_08_api_task.py

@@ -1,13 +1,13 @@
 # -*- coding: utf-8 -*-
 """Test Apis."""
-from flask import json
-import types
 import urllib
-import pytest
+
+from factories import TestApiBase
 from utils import *
 from walle.model.task import TaskModel
 
-class TestApiTask:
+
+class TestApiTask(TestApiBase):
     """api role testing"""
     uri_prefix = '/api/task'
 
@@ -148,7 +148,6 @@ class TestApiTask:
         response_success(resp)
         assert resp_json(resp)['data']['status'] == TaskModel.status_reject
 
-
     def test_get_remove(self, user, testapp, client):
         """Login successful."""
         # 1.create another role

+ 4 - 5
walle/model/user.py

@@ -8,12 +8,10 @@ from datetime import datetime
 
 from flask_login import UserMixin
 from sqlalchemy import String, Integer, DateTime, or_
-from sqlalchemy.orm import aliased
+from walle import model
 from walle.model.database import SurrogatePK, db, Model
 from walle.service.extensions import permission
-from walle.service.rbac.access import Access as AccessRbac
 from walle.service.rbac.role import *
-from walle import model
 from werkzeug.security import check_password_hash, generate_password_hash
 
 
@@ -46,7 +44,6 @@ class UserModel(UserMixin, SurrogatePK, Model):
         2: '冻结',
     }
 
-
     def add(self, *args, **kwargs):
         data = dict(*args)
         user = UserModel(**data)
@@ -164,6 +161,9 @@ class UserModel(UserMixin, SurrogatePK, Model):
         MemberModel = model.member.MemberModel
         return MemberModel().spaces(user_id=self.id)
 
+    def space_id(self):
+        return session['space_id']
+
     @classmethod
     def fresh_session(cls):
         # 0.超管
@@ -260,4 +260,3 @@ class UserModel(UserMixin, SurrogatePK, Model):
             'enable_audit': False,
             'enable_block': False,
         }
-