1#! /usr/bin/env python3
2#
3# BitBake Toaster Implementation
4#
5# Copyright (C) 2013-2016 Intel Corporation
6#
7# SPDX-License-Identifier: GPL-2.0-only
8#
9from django.urls import reverse
10from tests.browser.selenium_helpers import SeleniumTestCase
11from selenium.webdriver.support.ui import Select
12from selenium.common.exceptions import InvalidElementStateException
13from selenium.webdriver.common.by import By
14
15from orm.models import Project, Release, BitbakeVersion
16
17
18class TestNewProjectPage(SeleniumTestCase):
19    """ Test project data at /project/X/ is displayed correctly """
20
21    def setUp(self):
22        bitbake, c = BitbakeVersion.objects.get_or_create(
23            name="master",
24            giturl="git://master",
25            branch="master",
26            dirpath="master")
27
28        release, c = Release.objects.get_or_create(name="msater",
29                                                   description="master"
30                                                   "release",
31                                                   branch_name="master",
32                                                   helptext="latest",
33                                                   bitbake_version=bitbake)
34
35        self.release, c = Release.objects.get_or_create(
36            name="msater2",
37            description="master2"
38            "release2",
39            branch_name="master2",
40            helptext="latest2",
41            bitbake_version=bitbake)
42
43    def test_create_new_project(self):
44        """ Test creating a project """
45
46        project_name = "masterproject"
47
48        url = reverse('newproject')
49        self.get(url)
50        self.wait_until_visible('#new-project-name', poll=3)
51        self.enter_text('#new-project-name', project_name)
52
53        select = Select(self.find('#projectversion'))
54        select.select_by_value(str(self.release.pk))
55
56        self.click("#create-project-button")
57
58        # We should get redirected to the new project's page with the
59        # notification at the top
60        element = self.wait_until_visible(
61            '#project-created-notification', poll=3)
62
63        self.assertTrue(project_name in element.text,
64                        "New project name not in new project notification")
65
66        self.assertTrue(Project.objects.filter(name=project_name).count(),
67                        "New project not found in database")
68
69    def test_new_duplicates_project_name(self):
70        """
71        Should not be able to create a new project whose name is the same
72        as an existing project
73       """
74
75        project_name = "dupproject"
76
77        Project.objects.create_project(name=project_name,
78                                       release=self.release)
79
80        url = reverse('newproject')
81        self.get(url)
82        self.wait_until_visible('#new-project-name', poll=3)
83
84        self.enter_text('#new-project-name', project_name)
85
86        select = Select(self.find('#projectversion'))
87        select.select_by_value(str(self.release.pk))
88
89        radio = self.driver.find_element(By.ID, 'type-new')
90        radio.click()
91
92        self.click("#create-project-button")
93
94        self.wait_until_present('#hint-error-project-name', poll=3)
95        element = self.find('#hint-error-project-name')
96
97        self.assertTrue(("Project names must be unique" in element.text),
98                        "Did not find unique project name error message")
99
100        # Try and click it anyway, if it submits we'll have a new project in
101        # the db and assert then
102        try:
103            self.click("#create-project-button")
104        except InvalidElementStateException:
105            pass
106
107        self.assertTrue(
108            (Project.objects.filter(name=project_name).count() == 1),
109            "New project not found in database")
110