"""Unit tests for sessions (login/logout)."""


from peanuts.tests.base import BaseTestCase
from peanuts.models.user import User, PeanutsAuth


__all__ = ['SessionTest']


class SessionTest(BaseTestCase):
    """A unit test for sessions."""
    base_url = '/session'

    def test_get_401(self):
        """Tests that the /session/ GET endpoint 401s if not logged in."""
        r = self.get(self.base_url + '/')
        assert r.status_code == 401

    def test_get(self):
        """Tests /session/ GET."""
        password = '123abc'
        peanuts_auth = PeanutsAuth(
            email='test@example.org',
            password=password
            )
        user = User(
            peanuts_auth=peanuts_auth
            )

        self.db_session.add(user)
        self.commit()

        self.login('peanuts', user, password)

        r = self.get(self.base_url + '/')
        assert r.status_code == 200
        assert 'data' in r.json

        data = r.json['data']
        assert 'user_id' in data
        assert data['user_id'] == user.id_

    def test_delete_401(self):
        """Tests that the /session/ DELETE endpoint 401s if not logged in."""
        r = self.delete(self.base_url + '/')
        assert r.status_code == 401

class AuthPeanutsTest(BaseTestCase):
    """A unit test to create a peanuts user."""
    def test_post(self):
        """Tests /session/peanuts/ POST (logs in)."""
        password = '123abc'
        peanuts_auth = PeanutsAuth(
            email='test@example.org',
            password=password
            )
        user = User(
            peanuts_auth=peanuts_auth
            )

        self.db_session.add(user)
        self.commit()

        r = self.login('peanuts', user, password)
        assert r.status_code == 200
        assert 'data' in r.json

        data = r.json['data']
        assert 'user_id' in data
        assert data['user_id'] == user.id_

@@ -664,37 +664,24 @@
 user = User(
-%0A            
 peanuts_auth
@@ -689,37 +689,24 @@
 peanuts_auth
-%0A            
 )%0A%0A        s
@@ -1209,16 +1209,570 @@
 == 401%0A%0A
+    def test_delete(self):%0A        %22%22%22Tests /session/ DELETE.%22%22%22%0A        password = '123abc'%0A        peanuts_auth = PeanutsAuth(%0A            email='test@example.org',%0A            password=password%0A            )%0A        user = User(peanuts_auth=peanuts_auth)%0A%0A        self.db_session.add(user)%0A        self.commit()%0A%0A        self.login('peanuts', user, password)%0A%0A        r = self.delete(self.base_url + '/')%0A        assert r.status_code == 200%0A%0A        # Confirm the logout.%0A        r = self.get(self.base_url + '/')%0A        assert r.status_code == 401%0A%0A
 class Au
@@ -2093,29 +2093,16 @@
  = User(
-%0A            
 peanuts_
@@ -2118,29 +2118,16 @@
 uts_auth
-%0A            
 )%0A%0A     

test /session/ DELETE