Commit 756a581e authored by Marius Engen's avatar Marius Engen
Browse files

Merge branch 'task3' into 'master'

Task 3: Implement integration test

See merge request !6
parents 3b016106 36b0aaf1
Pipeline #167849 passed with stages
in 2 minutes and 19 seconds
No preview for this file type
from users.tests.two_way_domain_test import UserRegistration2WayTestCase
from users.tests.user_serializer_test import UserSerializerTest
import users.tests.data
test_data_one = {
"username": "username",
"email": "name@mail.com",
"password": "a",
"password1": "a",
"phone_number": "12345678",
"country": "Norway",
"city": "Trondheim",
"street_address": "Kongens Gate 1"
}
test_data_two = {
"username": "username",
"email": "name@mail.com",
"password": "",
"password1": "",
"phone_number": "12345678",
"country": "Norway",
"city": "Trondheim",
"street_address": "Kongens Gate 1"
}
fields = [
'username',
'email',
'password',
'password1',
'phone_number',
'country',
'city',
'street_address'
]
field_values = {
'valid':
{
"username": "username_two",
"email": "name_two@mail.com",
"password": "password",
"password1": "password",
"phone_number": "12345678",
"country": "Norway",
"city": "Trondheim",
"street_address": "Kongens Gate 1"
},
'invalid': {
'username': 'User name',
'email': 'not_email',
'password': None,
'password1': None,
'phone_number': 'abcdefghijklmnopqrstuvxyzabcdefghijklmnopqrstuvxyzabcdefghijklmnopqrstuvxyz',
'country': 'abcdefghijklmnopqrstuvxyzabcdefghijklmnopqrstuvxyzabcdefghijklmnopqrstuvxyz',
'city': 'abcdefghijklmnopqrstuvxyzabcdefghijklmnopqrstuvxyzabcdefghijklmnopqrstuvxyz',
'street_address': 'abcdefghijklmnopqrstuvxyzabcdefghijklmnopqrstuvxyzabcdefghijklmnopqrstuvxyz',
},
'blank': {
'username': '',
'email': '',
'password': '',
'password1': '',
'phone_number': '',
'country': '',
'city': '',
'street_address': '',
}
}
test_cases = [
{
"username": "invalid",
"password": "blank",
"password1": "valid",
"email": "invalid",
"phone_number": "valid",
"country": "blank",
"city": "blank",
"street_address": "invalid"
},
{
"username": "invalid",
"password": "valid",
"password1": "blank",
"email": "valid",
"phone_number": "blank",
"country": "blank",
"city": "invalid",
"street_address": "valid"
},
{
"username": "invalid",
"password": "valid",
"password1": "blank",
"email": "blank",
"phone_number": "invalid",
"country": "valid",
"city": "blank",
"street_address": "invalid"
},
{
"username": "invalid",
"password": "blank",
"password1": "blank",
"email": "invalid",
"phone_number": "valid",
"country": "blank",
"city": "invalid",
"street_address": "valid"
},
{
"username": "invalid",
"password": "blank",
"password1": "valid",
"email": "valid",
"phone_number": "blank",
"country": "invalid",
"city": "valid",
"street_address": "blank"
},
{
"username": "valid",
"password": "blank",
"password1": "blank",
"email": "blank",
"phone_number": "invalid",
"country": "blank",
"city": "valid",
"street_address": "blank"
},
{
"username": "valid",
"password": "blank",
"password1": "blank",
"email": "invalid",
"phone_number": "blank",
"country": "valid",
"city": "blank",
"street_address": "valid"
},
{
"username": "valid",
"password": "valid",
"password1": "valid",
"email": "valid",
"phone_number": "blank",
"country": "valid",
"city": "invalid",
"street_address": "blank"
},
{
"username": "blank",
"password": "valid",
"password1": "blank",
"email": "valid",
"phone_number": "valid",
"country": "invalid",
"city": "invalid",
"street_address": "blank"
},
{
"username": "blank",
"password": "blank",
"password1": "blank",
"email": "blank",
"phone_number": "blank",
"country": "valid",
"city": "valid",
"street_address": "invalid"
},
{
"username": "blank",
"password": "blank",
"password1": "blank",
"email": "valid",
"phone_number": "invalid",
"country": "invalid",
"city": "blank",
"street_address": "blank"
},
{
"username": "blank",
"password": "blank",
"password1": "valid",
"email": "blank",
"phone_number": "valid",
"country": "valid",
"city": "invalid",
"street_address": "invalid"
},
{
"username": "blank",
"password": "valid",
"password1": "blank",
"email": "blank",
"phone_number": "blank",
"country": "blank",
"city": "valid",
"street_address": "valid"
},
{
"username": "blank",
"password": "blank",
"password1": "valid",
"email": "invalid",
"phone_number": "invalid",
"country": "blank",
"city": "blank",
"street_address": "blank"
},
{
"username": "invalid",
"password": "blank",
"password1": "valid",
"email": "blank",
"phone_number": "invalid",
"country": "invalid",
"city": "valid",
"street_address": "valid"
},
{
"username": "invalid",
"password": "valid",
"password1": "valid",
"email": "valid",
"phone_number": "blank",
"country": "blank",
"city": "blank",
"street_address": "invalid"
},
{
"username": "invalid",
"password": "blank",
"password1": "blank",
"email": "blank",
"phone_number": "blank",
"country": "invalid",
"city": "invalid",
"street_address": "valid"
},
{
"username": "invalid",
"password": "valid",
"password1": "blank",
"email": "invalid",
"phone_number": "invalid",
"country": "valid",
"city": "valid",
"street_address": "blank"
},
{
"username": "valid",
"password": "valid",
"password1": "valid",
"email": "invalid",
"phone_number": "blank",
"country": "invalid",
"city": "valid",
"street_address": "blank"
},
{
"username": "valid",
"password": "blank",
"password1": "blank",
"email": "valid",
"phone_number": "blank",
"country": "valid",
"city": "blank",
"street_address": "invalid"
},
{
"username": "valid",
"password": "blank",
"password1": "valid",
"email": "blank",
"phone_number": "invalid",
"country": "blank",
"city": "invalid",
"street_address": "valid"
},
{
"username": "valid",
"password": "valid",
"password1": "blank",
"email": "invalid",
"phone_number": "valid",
"country": "blank",
"city": "valid",
"street_address": "blank"
},
{
"username": "valid",
"password": "blank",
"password1": "blank",
"email": "valid",
"phone_number": "blank",
"country": "invalid",
"city": "blank",
"street_address": "invalid"
},
{
"username": "valid",
"password": "valid",
"password1": "valid",
"email": "blank",
"phone_number": "invalid",
"country": "valid",
"city": "blank",
"street_address": "valid"
},
{
"username": "valid",
"password": "blank",
"password1": "blank",
"email": "blank",
"phone_number": "valid",
"country": "blank",
"city": "invalid",
"street_address": "blank"
},
{
"username": "blank",
"password": "blank",
"password1": "valid",
"email": "invalid",
"phone_number": "blank",
"country": "valid",
"city": "invalid",
"street_address": "blank"
},
{
"username": "blank",
"password": "blank",
"password1": "blank",
"email": "valid",
"phone_number": "invalid",
"country": "blank",
"city": "valid",
"street_address": "invalid"
},
{
"username": "blank",
"password": "valid",
"password1": "blank",
"email": "blank",
"phone_number": "valid",
"country": "invalid",
"city": "blank",
"street_address": "valid"
},
{
"username": "blank",
"password": "blank",
"password1": "valid",
"email": "blank",
"phone_number": "blank",
"country": "valid",
"city": "invalid",
"street_address": "blank"
},
{
"username": "blank",
"password": "valid",
"password1": "blank",
"email": "invalid",
"phone_number": "blank",
"country": "blank",
"city": "valid",
"street_address": "invalid"
},
{
"username": "blank",
"password": "blank",
"password1": "valid",
"email": "valid",
"phone_number": "invalid",
"country": "blank",
"city": "blank",
"street_address": "valid"
},
{
"username": "blank",
"password": "valid",
"password1": "blank",
"email": "blank",
"phone_number": "valid",
"country": "invalid",
"city": "blank",
"street_address": "blank"
}
]
import uuid
from django.test import TestCase, Client
from users.tests.data import field_values, test_cases
class UserRegistration2WayTestCase(TestCase):
def setUp(self):
print("Running two way user registration")
self.client = Client()
def test_two_way(self):
for testcase in test_cases:
request = {}
invalid = False
for key, value in testcase.items():
request[key] = field_values[value][key]
if key == 'username' and value == 'valid':
request['username'] = request['username'] + str(uuid.uuid4())
if value == 'invalid':
invalid = True
elif key == ('username' or 'password' or 'password1') and value == 'blank':
invalid = True
# Checking if the repsonse should return status code 400 or 201
actual_status_code = 400 if invalid else 201
expected_status_code = self.client.post('/api/users/', request).status_code
self.assertEqual(expected_status_code, actual_status_code)
{
"baseUrl": "http://localhost:3000"
}
\ No newline at end of file
{
"admin": {
"name": "admin",
"password": "123456"
}
}
\ No newline at end of file
{
"name": "Using fixtures to represent data",
"email": "hello@cypress.io",
"body": "Fixtures are a great way to mock data for responses to routes"
}
describe("Register user", () => {
it("Register only required fields", () => {
cy.register();
cy.get("#form-register-user").within(($form) => {
cy.get('input[name="username"]').type("username" + Date.now());
cy.get('input[name="password"]').type("password123456");
cy.get('input[name="password1"]').type("password123456");
});
cy.get("#btn-create-account").click();
cy.url().should("include", "/workouts.html");
});
it("Register with blank fields", () => {
cy.register();
cy.get("#btn-create-account").click();
cy.get(".alert").contains("Registration failed!");
});
it("Register with valid input", () => {
cy.register();
cy.get("#form-register-user").within(($form) => {
cy.get('input[name="username"]').type("username" + Date.now());
cy.get('input[name="email"]').type("username@mail.com");
cy.get('input[name="password"]').type("123456789");
cy.get('input[name="password1"]').type("123456789");
cy.get('input[name="phone_number"]').type("12345678");
cy.get('input[name="country"]').type("Norge");
cy.get('input[name="city"]').type("Trondheim");
cy.get('input[name="street_address"]').type("Kongens Gate 1");
});
cy.get("#btn-create-account").click();
cy.url().should("include", "/workouts.html");
});
it("Register with invalid username", () => {
cy.register();
cy.get("#form-register-user").within(($form) => {
cy.get('input[name="username"]').type("usernamefnjkljdfjjjjjjjjjjdfjjfdsjkaklfjbdsjflljnfjksdlnfljesfdshuafobnejsklfnhejsbfsjleabfjshrlbfejiufbesklaejfeiwubfueiorwpheufryuobwffnrjlnafjhersbfjlesnajlfjefesbfjhlrhbfjlsdnfjsbkafjebsfjkbesfjhkbejfkaesjfhjldsfjfskføweiilufhjsoæøafberhkfjljselfbhkjbfnskjdjfjhdjfksdjfjfjdjfjjjfjfjsdaklfnøalwkenfblwoifwlndeflabhrføwæoekfsjhbalieøfslakfn ashlefhrwfjsef" + Date.now());
cy.get('input[name="email"]').type("username@mail.com");
cy.get('input[name="password"]').type("123456789");
cy.get('input[name="password1"]').type("123456789");
cy.get('input[name="phone_number"]').type("12345678");
cy.get('input[name="country"]').type("Norge");
cy.get('input[name="city"]').type("Trondheim");
cy.get('input[name="street_address"]').type("Kongens Gate 1");
});
cy.get("#btn-create-account").click();
cy.contains("Registration failed!")
});
it("Register without username", () => {
cy.register();
cy.get("#form-register-user").within(($form) => {
cy.get('input[name="email"]').type("username@mail.com" + Date.now());
cy.get('input[name="password"]').type("123456789");
cy.get('input[name="password1"]').type("123456789");
cy.get('input[name="phone_number"]').type("12345678");
cy.get('input[name="country"]').type("Norge");
cy.get('input[name="city"]').type("Trondheim");
cy.get('input[name="street_address"]').type("Kongens Gate 1");
});
cy.get("#btn-create-account").click();
cy.contains("Registration failed!");
});
it("Register without password", () => {
cy.register();
cy.get("#form-register-user").within(($form) => {
cy.get('input[name="username"]').type("username" + Date.now());
cy.get('input[name="email"]').type("username@mail.com");
cy.get('input[name="password1"]').type("123456789");
cy.get('input[name="phone_number"]').type("12345678");
cy.get('input[name="country"]').type("Norge");
cy.get('input[name="city"]').type("Trondheim");
cy.get('input[name="street_address"]').type("Kongens Gate 1");
});
cy.get("#btn-create-account").click();
cy.contains("Registration failed!");
});
it("Register without password1", () => {
cy.register();
cy.get("#form-register-user").within(($form) => {
cy.get('input[name="username"]').type("username" + Date.now());
cy.get('input[name="email"]').type("username@mail.com");
cy.get('input[name="password"]').type("123456789");
cy.get('input[name="phone_number"]').type("12345678");
cy.get('input[name="country"]').type("Norge");
cy.get('input[name="city"]').type("Trondheim");
cy.get('input[name="street_address"]').type("Kongens Gate 1");
});
cy.get("#btn-create-account").click();
cy.contains("Registration failed!");
});
it("Register with invalid email", () => {
cy.register();
cy.get("#form-register-user").within(($form) => {
cy.get('input[name="username"]').type("username" + Date.now());
cy.get('input[name="email"]').type("usernamemailcom");
cy.get('input[name="password"]').type("123456789");
cy.get('input[name="password1"]').type("123456789");
cy.get('input[name="phone_number"]').type("12345678");
cy.get('input[name="country"]').type("Norge");
cy.get('input[name="city"]').type("Trondheim");
cy.get('input[name="street_address"]').type("Kongens Gate 1");
});
cy.get("#btn-create-account").click();
cy.get(".alert").contains("Registration failed!");
});
it("Valid with no city", () => {
cy.register();
cy.get("#form-register-user").within(($form) => {
cy.get('input[name="username"]').type("username" + Date.now());
cy.get('input[name="email"]').type("username@mail.com");
cy.get('input[name="password"]').type("123456789");
cy.get('input[name="password1"]').type("123456789");
cy.get('input[name="phone_number"]').type("12345678");
cy.get('input[name="country"]').type("Norge");
cy.get('input[name="street_address"]').type("Kongens Gate 1");
});
cy.get("#btn-create-account").click();
cy.url().should("include", "/workouts.html");
});
it("Too long country", () => {
cy.register();
cy.get("#form-register-user").within(($form) => {
cy.get('input[name="username"]').type("username" + Date.now());
cy.get('input[name="email"]').type("username@mail.com");
cy.get('input[name="password"]').type("123456789");
cy.get('input[name="password1"]').type("123456789");
cy.get('input[name="phone_number"]').type("12345678");
cy.get('input[name="country"]').type("N".repeat(51));