Skip to content
This repository was archived by the owner on Apr 18, 2025. It is now read-only.

Commit 19a6101

Browse files
authored
Add permissions to API (#68)
* Add auth to server and fix user.service to accommodate it * Fix tests * Fix test
1 parent cceec96 commit 19a6101

File tree

4 files changed

+27
-12
lines changed

4 files changed

+27
-12
lines changed

client/src/app/services/user.service.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Injectable } from '@angular/core';
2-
import { Http, Response } from '@angular/http';
2+
import { Http, Response, Headers, RequestOptions } from '@angular/http';
33
import { Observable } from 'rxjs/Rx';
44

55
import { environment } from '../../environments/environment';
@@ -12,8 +12,16 @@ export class UserService {
1212
private http: Http,
1313
) { }
1414

15+
private appendToken(): RequestOptions {
16+
const headers = new Headers();
17+
headers.append('Authorization', `Basic ${btoa('admin:pass')}`);
18+
return new RequestOptions({ headers: headers });
19+
}
20+
1521
getUsers(): Observable<User[]> {
16-
return this.http.get(`${environment.server}/api/users`)
22+
const options = this.appendToken();
23+
24+
return this.http.get(`${environment.server}/api/users`, options)
1725
.map(res => res.json())
1826
.catch(this.handleError);
1927
}

server/api/tests.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,22 @@
11
from rest_framework import status
22
from rest_framework.test import APITestCase
33
from django.contrib.auth.models import User
4+
from requests.auth import HTTPBasicAuth
45

56

67
class UsersApiTestCase(APITestCase):
78
def setUp(self):
89
User.objects.create_superuser('admin', '[email protected]', 'admin12345')
910

1011
def test_get_users_objects(self):
12+
self.client.login(username='admin', password='admin12345')
1113
response = self.client.get('/api/users/', format='json')
1214
self.assertEqual(response.status_code, status.HTTP_200_OK)
1315
self.assertEqual(len(response.data), 1)
1416
self.assertEqual(response.data[0]['email'], '[email protected]')
1517
self.assertEqual(response.data[0]['username'], 'admin')
16-
self.assertEqual(response.data[0]['url'], 'http://testserver/api/users/1/')
18+
self.assertEqual(response.data[0]['url'], 'http://testserver/api/users/2/')
19+
20+
def test_forbidden_get_users_objects(self):
21+
response = self.client.get('/api/users/', format='json')
22+
self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)

server/config/settings.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
# SECURITY WARNING: don't run with debug turned on in production!
2626
DEBUG = os.environ.get('DEBUG', False)
2727

28-
ALLOWED_HOSTS = []
28+
ALLOWED_HOSTS = ['localhost']
2929

3030
CORS_ORIGIN_WHITELIST = (
3131
'localhost:4200',
@@ -137,6 +137,13 @@
137137
},
138138
}
139139

140+
# REST API
141+
REST_FRAMEWORK = {
142+
'DEFAULT_PERMISSION_CLASSES': (
143+
'rest_framework.permissions.IsAuthenticated',
144+
)
145+
}
146+
140147

141148
# Internationalization
142149
# https://docs.djangoproject.com/en/1.11/topics/i18n/

server/locustfile.py

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,10 @@
22

33

44
class UserBehavior(TaskSet):
5-
# def on_start(self):
6-
# """ on_start is called when a Locust start before any task is scheduled """
7-
# self.login()
8-
9-
# def login(self):
10-
# self.client.post("/login", {"username":"ellen_key", "password":"education"})
11-
125
@task(1)
136
def users(self):
14-
self.client.get('/api/users')
7+
self.client.login(username='admin', password='pass')
8+
self.client.get('/api/users', serlf.headers)
159

1610

1711
class WebsiteUser(HttpLocust):

0 commit comments

Comments
 (0)