test_auditlog.py 28.3 KB
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 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772
# -*- coding: utf8 -*-
# This file is part of PyBossa.
#
# Copyright (C) 2015 SciFabric LTD.
#
# PyBossa 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.
#
# PyBossa 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 PyBossa.  If not, see <http://www.gnu.org/licenses/>.
import json
from default import db, Test, with_context
from collections import namedtuple
from factories import ProjectFactory, TaskFactory, UserFactory, CategoryFactory
from helper import web

from pybossa.repositories import UserRepository
from pybossa.repositories import AuditlogRepository
from mock import patch, MagicMock

auditlog_repo = AuditlogRepository(db)
user_repo = UserRepository(db)


FakeRequest = namedtuple('FakeRequest', ['text', 'status_code', 'headers'])

class TestAuditlogAPI(Test):

    @with_context
    def test_project_create(self):
        """Test Auditlog API project create works."""
        CategoryFactory.create()
        user = UserFactory.create()

        data = {'name': 'New Name',
                'short_name': 'new_short_name',
                'description': 'new_description',
                'long_description': 'new_long_description',
                'allow_anonymous_contributors': 'False',
                }
        url = '/api/project?api_key=%s' % (user.api_key)
        self.app.post(url, data=json.dumps(data))
        logs = auditlog_repo.filter_by(project_short_name='new_short_name')

        assert len(logs) == 1, logs
        for log in logs:
            assert log.user_id == user.id, log.user_id
            assert log.user_name == user.name, log.user_name
            assert log.project_short_name == 'new_short_name', log.project_short_name
            assert log.caller == 'api', log.caller
            assert log.action == 'create', log.action
            assert log.attribute == 'project', log.attribute
            assert log.old_value == 'Nothing', log.old_value
            assert log.new_value == 'New project', log.new_value

    @with_context
    def test_project_delete(self):
        """Test Auditlog API project create works."""
        user = UserFactory.create()
        project = ProjectFactory.create(owner=user)
        project_short_name = project.short_name

        url = '/api/project/%s?api_key=%s' % (project.id, user.api_key)
        self.app.delete(url)
        logs = auditlog_repo.filter_by(project_short_name=project_short_name)

        assert len(logs) == 1, logs
        for log in logs:
            assert log.user_id == user.id, log.user_id
            assert log.user_name == user.name, log.user_name
            assert log.project_short_name == project_short_name, log.project_short_name
            assert log.caller == 'api', log.caller
            assert log.action == 'delete', log.action
            assert log.attribute == 'project', log.attribute
            assert log.old_value == 'Saved', log.old_value
            assert log.new_value == 'Deleted', log.new_value

    @with_context
    def test_project_update_attributes(self):
        """Test Auditlog API project update attributes works."""
        project = ProjectFactory.create()

        data = {'name': 'New Name',
                'short_name': 'new_short_name',
                'description': 'new_description',
                'long_description': 'new_long_description',
                'allow_anonymous_contributors': 'False',
                }
        attributes = data.keys()
        url = '/api/project/%s?api_key=%s' % (project.id, project.owner.api_key)
        self.app.put(url, data=json.dumps(data))
        logs = auditlog_repo.filter_by(project_id=project.id)

        assert len(logs) == 5, logs
        for log in logs:
            assert log.user_id == project.owner_id, log.user_id
            assert log.user_name == project.owner.name, log.user_name
            assert log.project_short_name == project.short_name, log.project_short_name
            assert log.action == 'update', log.action
            assert log.caller == 'api', log.caller
            assert log.attribute in attributes, log.attribute
            msg = "%s != %s" % (data[log.attribute], log.new_value)
            assert data[log.attribute] == log.new_value, msg

    @with_context
    def test_project_update_attributes_admin(self):
        """Test Auditlog API project update attributes works for admins."""
        project = ProjectFactory.create()
        admin = UserFactory.create(admin=True)

        data = {'name': 'New Name',
                'short_name': 'new_short_name',
                'description': 'new_description',
                'long_description': 'new_long_description',
                'allow_anonymous_contributors': 'False',
                }
        attributes = data.keys()
        url = '/api/project/%s?api_key=%s' % (project.id, admin.api_key)
        self.app.put(url, data=json.dumps(data))
        logs = auditlog_repo.filter_by(project_id=project.id)

        assert len(logs) == 5, logs
        for log in logs:
            assert log.user_id == admin.id, log.user_id
            assert log.user_name == admin.name, log.user_name
            assert log.project_short_name == project.short_name, log.project_short_name
            assert log.action == 'update', log.action
            assert log.caller == 'api', log.caller
            assert log.attribute in attributes, log.attribute
            msg = "%s != %s" % (data[log.attribute], log.new_value)
            assert data[log.attribute] == log.new_value, msg

    @with_context
    def test_project_update_attributes_non_owner(self):
        """Test Auditlog API project update attributes works for non owners."""
        project = ProjectFactory.create()
        user = UserFactory.create()

        data = {'name': 'New Name',
                'short_name': 'new_short_name',
                'description': 'new_description',
                'long_description': 'new_long_description',
                'allow_anonymous_contributors': 'False',
                }
        url = '/api/project/%s?api_key=%s' % (project.id, user.api_key)
        self.app.put(url, data=json.dumps(data))
        logs = auditlog_repo.filter_by(project_id=project.id)

        assert len(logs) == 0, logs

    def test_project_update_task_presenter(self):
        """Test Auditlog API project update info task_presenter works."""
        project = ProjectFactory.create()

        owner_id = project.owner.id
        owner_name = project.owner.name
        data = {'info': {'task_presenter': 'new'}}
        url = '/api/project/%s?api_key=%s' % (project.id, project.owner.api_key)
        self.app.put(url, data=json.dumps(data))
        logs = auditlog_repo.filter_by(project_id=project.id)

        assert len(logs) == 1, logs
        for log in logs:
            assert log.user_id == owner_id, log.user_id
            assert log.user_name == owner_name, log.user_name
            assert log.project_short_name == project.short_name, log.project_short_name
            assert log.action == 'update', log.action
            assert log.caller == 'api', log.caller
            assert log.attribute == 'task_presenter', log.attribute
            msg = "%s != %s" % (data['info']['task_presenter'], log.new_value)
            assert data['info']['task_presenter'] == log.new_value, msg

    def test_project_update_scheduler(self):
        """Test Auditlog API project update info scheduler works."""
        project = ProjectFactory.create()

        owner_id = project.owner.id
        owner_name = project.owner.name
        data = {'info': {'sched': 'depth_first'}}
        url = '/api/project/%s?api_key=%s' % (project.id, project.owner.api_key)
        self.app.put(url, data=json.dumps(data))
        logs = auditlog_repo.filter_by(project_id=project.id)

        assert len(logs) == 1, logs
        for log in logs:
            assert log.user_id == owner_id, log.user_id
            assert log.user_name == owner_name, log.user_name
            assert log.project_short_name == project.short_name, log.project_short_name
            assert log.action == 'update', log.action
            assert log.caller == 'api', log.caller
            assert log.attribute == 'sched', log.attribute
            msg = "%s != %s" % (data['info']['sched'], log.new_value)
            assert data['info']['sched'] == log.new_value, msg

    def test_project_update_two_info_objects(self):
        """Test Auditlog API project update two info objects works."""
        project = ProjectFactory.create()

        owner_id = project.owner.id
        owner_name = project.owner.name
        data = {'info': {'sched': 'depth_first', 'task_presenter': 'new'}}
        attributes = data['info'].keys()
        url = '/api/project/%s?api_key=%s' % (project.id, project.owner.api_key)
        self.app.put(url, data=json.dumps(data))
        logs = auditlog_repo.filter_by(project_id=project.id)

        assert len(logs) == 2, logs
        for log in logs:
            assert log.user_id == owner_id, log.user_id
            assert log.user_name == owner_name, log.user_name
            assert log.project_short_name == project.short_name, log.project_short_name
            assert log.action == 'update', log.action
            assert log.caller == 'api', log.caller
            assert log.attribute in attributes, log.attribute
            msg = "%s != %s" % (data['info'][log.attribute], log.new_value)
            assert data['info'][log.attribute] == log.new_value, msg


class TestAuditlogWEB(web.Helper):

    data = {}
    editor = {}

    def setUp(self):
        super(TestAuditlogWEB, self).setUp()
        self.data = {'id': 1,
                     'name': 'Sample Project',
                     'short_name': 'sampleapp',
                     'description': 'Description',
                     'allow_anonymous_contributors': 'true',
                     'category_id': 1,
                     'long_description': 'Long Description\n================',
                     'btn': 'Save'}
        self.editor = {'editor': 'Some HTML code!'}

    @with_context
    def test_project_create(self):
        self.register()
        self.new_project()
        short_name = 'sampleapp'

        logs = auditlog_repo.filter_by(project_short_name=short_name)
        assert len(logs) == 1, logs
        for log in logs:
            assert log.attribute == 'project', log.attribute
            assert log.old_value == 'Nothing', log.old_value
            assert log.new_value == 'New project', log.new_value
            assert log.caller == 'web', log.caller
            assert log.action == 'create', log.action
            assert log.user_name == 'johndoe', log.user_name
            assert log.user_id == 1, log.user_id

    @with_context
    def test_project_create(self):
        self.register()
        self.new_project()
        self.delete_project()
        short_name = 'sampleapp'

        logs = auditlog_repo.filter_by(project_short_name=short_name, offset=1)
        assert len(logs) == 1, logs
        for log in logs:
            assert log.attribute == 'project', log.attribute
            assert log.old_value == 'Saved', log.old_value
            assert log.new_value == 'Deleted', log.new_value
            assert log.caller == 'web', log.caller
            assert log.action == 'delete', log.action
            assert log.user_name == 'johndoe', log.user_name
            assert log.user_id == 1, log.user_id

    @with_context
    def test_project_update_name(self):
        self.register()
        self.new_project()
        short_name = 'sampleapp'

        url = "/project/%s/update" % short_name

        self.data['name'] = 'New'

        self.app.post(url, data=self.data, follow_redirects=True)

        logs = auditlog_repo.filter_by(project_short_name=short_name, offset=1)
        assert len(logs) == 1, logs
        for log in logs:
            assert log.attribute == 'name', log.attribute
            assert log.old_value == 'Sample Project', log.old_value
            assert log.new_value == self.data['name'], log.new_value
            assert log.caller == 'web', log.caller
            assert log.action == 'update', log.action
            assert log.user_name == 'johndoe', log.user_name
            assert log.user_id == 1, log.user_id

    @with_context
    def test_project_update_short_name(self):
        self.register()
        self.new_project()
        short_name = 'newshort_name'

        url = "/project/sampleapp/update"

        self.data['short_name'] = 'newshort_name'

        res = self.app.post(url, data=self.data, follow_redirects=True)

        logs = auditlog_repo.filter_by(project_short_name=short_name)
        assert len(logs) == 1, logs
        for log in logs:
            assert log.attribute == 'short_name', log.attribute
            assert log.old_value == 'sampleapp', log.old_value
            assert log.new_value == self.data['short_name'], log.new_value
            assert log.caller == 'web', log.caller
            assert log.action == 'update', log.action
            assert log.user_name == 'johndoe', log.user_name
            assert log.user_id == 1, log.user_id

    @with_context
    def test_project_description(self):
        self.register()
        self.new_project()
        short_name = 'sampleapp'

        url = "/project/%s/update" % short_name

        attribute = 'description'

        new_string = 'New Something'

        old_value = self.data[attribute]

        self.data[attribute] = new_string

        self.app.post(url, data=self.data, follow_redirects=True)

        logs = auditlog_repo.filter_by(project_short_name=short_name, offset=1)
        assert len(logs) == 1, logs
        for log in logs:
            assert log.attribute == attribute, log.attribute
            assert log.old_value == old_value, log.old_value
            assert log.new_value == self.data[attribute], log.new_value
            assert log.caller == 'web', log.caller
            assert log.action == 'update', log.action
            assert log.user_name == 'johndoe', log.user_name
            assert log.user_id == 1, log.user_id

    @with_context
    def test_project_allow_anonymous_contributors(self):
        self.register()
        self.new_project()
        short_name = 'sampleapp'

        url = "/project/%s/update" % short_name

        attribute = 'allow_anonymous_contributors'

        new_value = 'false'

        old_value = self.data[attribute]

        self.data[attribute] = new_value

        self.app.post(url, data=self.data, follow_redirects=True)

        logs = auditlog_repo.filter_by(project_short_name=short_name, offset=1)
        assert len(logs) == 1, logs
        for log in logs:
            assert log.attribute == attribute, log.attribute
            assert log.old_value == old_value.capitalize(), log.old_value
            assert log.new_value == new_value.capitalize(), log.new_value
            assert log.caller == 'web', log.caller
            assert log.action == 'update', log.action
            assert log.user_name == 'johndoe', log.user_name
            assert log.user_id == 1, log.user_id

    @with_context
    def test_project_published(self):
        owner = UserFactory.create(email_addr='a@a.com', pro=True)
        owner.set_password('1234')
        user_repo.save(owner)
        project = ProjectFactory.create(owner=owner, published=False)
        self.signin(email='a@a.com', password='1234')
        TaskFactory.create(project=project)
        short_name = project.short_name

        url = "/project/%s/publish" % short_name

        attribute = 'published'

        new_string = 'true'

        old_value = 'false'

        self.data[attribute] = new_string

        self.app.post(url, follow_redirects=True)

        logs = auditlog_repo.filter_by(project_short_name=short_name)
        assert len(logs) == 1, logs
        for log in logs:
            assert log.attribute == attribute, log.attribute
            assert log.old_value == old_value, (log.old_value, old_value)
            assert log.new_value == self.data[attribute], log.new_value
            assert log.caller == 'web', log.caller
            assert log.action == 'update', log.action
            assert log.user_name == owner.name, log.user_name
            assert log.user_id == owner.id, log.user_id

    @with_context
    def test_project_long_description(self):
        self.register()
        self.new_project()
        short_name = 'sampleapp'

        url = "/project/%s/update" % short_name

        attribute = 'long_description'

        new_string = 'New long desc'

        old_value = self.data[attribute]

        self.data[attribute] = new_string

        self.app.post(url, data=self.data, follow_redirects=True)

        logs = auditlog_repo.filter_by(project_short_name=short_name, offset=1)
        assert len(logs) == 1, logs
        for log in logs:
            assert log.attribute == attribute, log.attribute
            assert log.old_value == old_value, log.old_value
            assert log.new_value == self.data[attribute], log.new_value
            assert log.caller == 'web', log.caller
            assert log.action == 'update', log.action
            assert log.user_name == 'johndoe', log.user_name
            assert log.user_id == 1, log.user_id

    @with_context
    def test_project_password(self):
        self.register()
        self.new_project()
        short_name = 'sampleapp'

        url = "/project/%s/update" % short_name

        attribute = 'password'

        new_string = 'new password'

        old_value = None

        self.data[attribute] = new_string
        self.data['protect'] = True

        self.app.post(url, data=self.data, follow_redirects=True)

        logs = auditlog_repo.filter_by(project_short_name=short_name, offset=1)
        assert len(logs) == 1, logs
        for log in logs:
            assert log.attribute == 'passwd_hash', log.attribute
            assert log.old_value == old_value, log.old_value
            assert log.new_value != None, log.new_value
            assert log.caller == 'web', log.caller
            assert log.action == 'update', log.action
            assert log.user_name == 'johndoe', log.user_name
            assert log.user_id == 1, log.user_id

    @with_context
    @patch('pybossa.forms.validator.requests.get')
    def test_project_webhook(self, mock):
        html_request = FakeRequest(json.dumps(self.data), 200,
                                   {'content-type': 'projectlication/json'})
        mock.return_value = html_request

        self.register()
        self.new_project()
        short_name = 'sampleapp'

        url = "/project/%s/update" % short_name

        attribute = 'webhook'

        new_string = 'http://google.com'

        old_value = ''

        self.data[attribute] = new_string

        self.app.post(url, data=self.data, follow_redirects=True)

        logs = auditlog_repo.filter_by(project_short_name=short_name, offset=1)
        assert len(logs) == 1, logs
        for log in logs:
            assert log.attribute == attribute, log.attribute
            assert log.old_value == old_value, log.old_value
            assert log.new_value == self.data[attribute], log.new_value
            assert log.caller == 'web', log.caller
            assert log.action == 'update', log.action
            assert log.user_name == 'johndoe', log.user_name
            assert log.user_id == 1, log.user_id

    @with_context
    def test_project_task_presenter(self):
        self.register()
        self.new_project()
        short_name = 'sampleapp'

        url = "/project/%s/tasks/taskpresentereditor" % short_name

        attribute = 'editor'

        new_string = 'new code'

        old_value = None

        self.editor[attribute] = new_string

        self.app.post(url, data=self.editor, follow_redirects=True)

        logs = auditlog_repo.filter_by(project_short_name=short_name, offset=1)
        assert len(logs) == 1, logs
        for log in logs:
            assert log.attribute == 'task_presenter', log.attribute
            assert log.old_value == old_value, log.old_value
            assert log.new_value == new_string, log.new_value
            assert log.caller == 'web', log.caller
            assert log.action == 'update', log.action
            assert log.user_name == 'johndoe', log.user_name
            assert log.user_id == 1, log.user_id

    @with_context
    def test_project_task_scheduler(self):
        self.register()
        self.new_project()
        short_name = 'sampleapp'

        url = "/project/%s/tasks/scheduler" % short_name

        attribute = 'sched'

        new_string = 'depth_first'

        old_value = 'default'

        self.app.post(url, data={'sched': new_string}, follow_redirects=True)

        logs = auditlog_repo.filter_by(project_short_name=short_name, offset=1)
        assert len(logs) == 1, logs
        for log in logs:
            assert log.attribute == 'sched', log.attribute
            assert log.old_value == old_value, log.old_value
            assert log.new_value == new_string, log.new_value
            assert log.caller == 'web', log.caller
            assert log.action == 'update', log.action
            assert log.user_name == 'johndoe', log.user_name
            assert log.user_id == 1, log.user_id

    @with_context
    def test_project_task_priority(self):
        self.register()
        self.new_project()
        self.new_task(1)
        short_name = 'sampleapp'

        url = "/project/%s/tasks/priority" % short_name

        attribute = 'task.priority_0'

        new_string = json.dumps({'task_id': 1, 'task_priority_0': 0.5})

        old_value = json.dumps({'task_id': 1, 'task_priority_0': 0.0})

        self.app.post(url, data={'task_ids': '1', 'priority_0': '0.5'}, follow_redirects=True)

        logs = auditlog_repo.filter_by(project_short_name=short_name, offset=1)
        assert len(logs) == 1, logs
        for log in logs:
            assert log.attribute == attribute, log.attribute
            assert log.old_value == old_value, log.old_value
            assert log.new_value == new_string, log.new_value
            assert log.caller == 'web', log.caller
            assert log.action == 'update', log.action
            assert log.user_name == 'johndoe', log.user_name
            assert log.user_id == 1, log.user_id

    @with_context
    def test_project_task_priority_two_tasks(self):
        self.register()
        self.new_project()
        self.new_task(1)
        self.new_task(1)
        short_name = 'sampleapp'

        url = "/project/%s/tasks/priority" % short_name

        attribute = 'task.priority_0'

        self.app.post(url, data={'task_ids': '1,2', 'priority_0': '0.5'}, follow_redirects=True)

        logs = auditlog_repo.filter_by(project_short_name=short_name, offset=1)
        assert len(logs) == 2, logs
        id = 1
        for log in logs:
            new_string = json.dumps({'task_id': id, 'task_priority_0': 0.5})
            old_value = json.dumps({'task_id': id, 'task_priority_0': 0.0})

            assert log.attribute == attribute, log.attribute
            assert log.old_value == old_value, log.old_value
            assert log.new_value == new_string, log.new_value
            assert log.caller == 'web', log.caller
            assert log.action == 'update', log.action
            assert log.user_name == 'johndoe', log.user_name
            assert log.user_id == 1, log.user_id
            id = id +1

    @with_context
    def test_project_task_redundancy(self):
        self.register()
        self.new_project()
        self.new_task(1)
        short_name = 'sampleapp'

        url = "/project/%s/tasks/redundancy" % short_name

        attribute = 'task.n_answers'

        new_string = '10'
        # Depends on each specific task, so old value will be non-avaliable
        old_value = 'N/A'

        self.app.post(url, data={'n_answers': '10'}, follow_redirects=True)

        logs = auditlog_repo.filter_by(project_short_name=short_name, offset=1)
        assert len(logs) == 1, logs
        for log in logs:
            assert log.attribute == attribute, log.attribute
            assert log.old_value == old_value, log.old_value
            assert log.new_value == new_string, log.new_value
            assert log.caller == 'web', log.caller
            assert log.action == 'update', log.action
            assert log.user_name == 'johndoe', log.user_name
            assert log.user_id == 1, log.user_id

    def test_project_auditlog_autoimporter_create(self):
        self.register()
        self.new_project()
        self.new_task(1)
        short_name = 'sampleapp'

        url = "/project/%s/tasks/autoimporter" % short_name
        data = {'form_name': 'csv', 'csv_url': 'http://fakeurl.com'}

        self.app.post(url, data=data, follow_redirects=True)

        attribute = 'autoimporter'

        new_value = '{"type": "csv", "csv_url": "http://fakeurl.com"}'

        old_value = 'Nothing'

        logs = auditlog_repo.filter_by(project_short_name=short_name, offset=1)
        assert len(logs) == 1, logs
        for log in logs:
            assert log.attribute == attribute, log.attribute
            assert log.old_value == old_value, log.old_value
            assert log.new_value == new_value, log.new_value
            assert log.caller == 'web', log.caller
            assert log.action == 'create', log.action
            assert log.user_name == 'johndoe', log.user_name
            assert log.user_id == 1, log.user_id

    def test_project_auditlog_autoimporter_delete(self):
        self.register()
        owner = user_repo.get(1)
        autoimporter = {'type': 'csv', 'csv_url': 'http://fakeurl.com'}
        project = ProjectFactory.create(owner=owner, info={'autoimporter': autoimporter})
        short_name = project.short_name

        attribute = 'autoimporter'

        old_value = json.dumps(autoimporter)

        new_value = 'Nothing'

        url = "/project/%s/tasks/autoimporter/delete" % short_name
        self.app.post(url, data={}, follow_redirects=True)

        logs = auditlog_repo.filter_by(project_short_name=short_name)
        assert len(logs) == 1, logs
        for log in logs:
            assert log.attribute == attribute, log.attribute
            assert log.old_value == old_value, log.old_value
            assert log.new_value == new_value, log.new_value
            assert log.caller == 'web', log.caller
            assert log.action == 'delete', log.action
            assert log.user_name == 'johndoe', log.user_name
            assert log.user_id == 1, log.user_id

    @with_context
    def test_project_auditlog_access_anon(self):
        # Admin
        self.register()
        self.new_project()
        self.new_task(1)
        short_name = 'sampleapp'
        self.signout()

        url = "/project/%s/auditlog" % short_name

        res = self.app.get(url, follow_redirects=True)
        assert "Sign in" in res.data, res.data

    @with_context
    def test_project_auditlog_access_owner(self):
        # Admin
        self.register()
        self.signout()
        # User
        self.register(name="Iser")
        self.new_project()
        self.new_task(1)
        short_name = 'sampleapp'

        url = "/project/%s/auditlog" % short_name

        res = self.app.get(url, follow_redirects=True)
        assert  res.status_code == 403, res.status_code

    @with_context
    def test_project_auditlog_access_pro(self):
        # Admin
        self.register()
        self.signout()
        # User
        self.register(name="Iser")
        self.new_project()
        self.new_task(1)
        short_name = 'sampleapp'

        user = user_repo.filter_by(name="Iser")[0]
        user.pro = True
        user_repo.save(user)

        url = "/project/%s/auditlog" % short_name

        res = self.app.get(url, follow_redirects=True)
        assert  res.status_code == 200, res.status_code

    @with_context
    def test_project_auditlog_access_admin(self):
        # Admin
        self.register()
        self.signout()
        # User
        self.register(name="user", password="user")
        self.new_project()
        self.new_task(1)
        self.signout()
        # Access as admin
        self.signin()
        short_name = 'sampleapp'

        url = "/project/%s/auditlog" % short_name

        res = self.app.get(url, follow_redirects=True)
        assert  res.status_code == 200, res.status_code