# Copyright (c) 2016 The Pycroft Authors. See the AUTHORS file.
# This file is part of the Pycroft project and licensed under the terms of
# the Apache License, Version 2.0. See the LICENSE file for details.

from pycroft import config
from pycroft.lib import user as UserHelper
from pycroft.model import session
from ...legacy_base import FactoryDataTestBase
from tests.factories import UserFactory
from ... import factories


# TODO slaughter this test, and only leave things that aren't already tested elsewhere.
class Test_User_Move_Out_And_Back_In(FactoryDataTestBase):
    def create_factories(self):
        super().create_factories()
        self.config = factories.ConfigFactory()
        self.room = factories.RoomFactory(patched_with_subnet=True, number="1", level=1)
        self.processing_user = UserFactory()

    def test_move_out(self):
        test_name = "Hans"
        test_login = "hans66"
        test_email = "hans@hans.de"
        test_building = self.room.building
        test_mac = "12:11:11:11:11:11"
        test_birthdate = "1990-01-01"

        address = self.room.address

        new_user, _ = UserHelper.create_user(
            test_name,
            test_login,
            test_email,
            test_birthdate,
            processor=self.processing_user,
            groups=[self.config.member_group],
            address=address
        )

        UserHelper.move_in(
            new_user,
            building_id=test_building.id,
            level=1,
            room_number="1",
            mac=test_mac,
            processor=self.processing_user,
        )

        self.session.commit()

        out_time = session.utcnow()

        UserHelper.move_out(user=new_user, comment="",
                            processor=self.processing_user, when=out_time)
        self.session.refresh(new_user)
        # check ends_at of moved out user
        for membership in new_user.memberships:
            assert (int := membership.active_during) is not None
            assert (end := int.end) is not None
            assert end <= out_time

        assert not new_user.hosts
        assert new_user.room is None
        # move_out keeps user's address
        assert new_user.address == address

        # check if users finance account still exists
        assert new_user.account is not None

@@ -205,140 +205,96 @@
 ls.%0A
-%0Afrom pycroft import config%0Afrom pycroft.lib import user as UserHelper%0Afrom pycroft.model import session%0Afrom ...legacy_ba
+import pytest%0A%0Afrom pycroft.helpers.interval import single%0Afrom pycroft.lib.u
 se
+r
  import 
 Fact
@@ -293,48 +293,40 @@
 ort 
-FactoryDataTestBase%0Afrom tests.factories
+move_out%0Afrom pycroft.model.user
  imp
@@ -333,23 +333,16 @@
 ort User
-Factory
 %0Afrom ..
@@ -366,95 +366,42 @@
 s%0A%0A%0A
-# TODO slaughter this test, and only leave things that aren't already tested elsewhere.
+@pytest.mark.usefixtures(%22config%22)
 %0Acla
@@ -411,145 +411,113 @@
 Test
-_
 User
-_
 Move
-_
 Out
-_And_Back_In(FactoryDataTestBase):%0A    def create_factories(self):%0A        super().create_factories()%0A        self.config =
+:%0A    @pytest.fixture(scope=%22class%22)%0A    def user(self, class_session) -%3E User:%0A        return
  fac
@@ -527,22 +527,20 @@
 ies.
-Config
+User
 Factory(
 )%0A  
@@ -535,17 +535,16 @@
 Factory(
-)
 %0A       
@@ -548,1000 +548,190 @@
     
-self.room = factories.RoomFactory(patched_with_subnet=True, number=%221%22, level=1)%0A        self.processing_user = UserFactory()%0A%0A    def test_move_out(self):%0A        test_name = %22Hans%22%0A        test_login = %22hans66%22%0A        test_email = %22hans@hans.de%22%0A        test_building = self.room.building%0A        test_mac = %2212:11:11:11:11:11%22%0A        test_birthdate = %221990-01-01%22%0A%0A        address = self.room.address%0A%0A        new_user, _ = UserHelper.create_user(%0A            test_name,%0A            test_login,%0A            test_email,%0A            test_birthdate,%0A            processor=self.processing_user,%0A            groups=%5Bself.config.member_group%5D,%0A            address=address%0A        )%0A%0A        UserHelper.move_in(%0A            new_user,%0A            building_id=test_building.id,%0A            level=1,%0A            room_number=%221%22,%0A            mac=test_mac,%0A            processor=self.processing_user,%0A        )%0A%0A        self.session.commit()%0A%0A        out_time = session.utcnow()%0A%0A        UserHelper.
+    with_host=True,%0A            room__patched_with_subnet=True,%0A        )%0A%0A    def test_move_out(self, session, user, processor, utcnow):%0A        address = user.room.address%0A        
 move
@@ -740,20 +740,16 @@
 ut(user=
-new_
 user, co
@@ -761,36 +761,8 @@
 =%22%22,
-%0A                           
  pro
@@ -772,35 +772,24 @@
 sor=
-self.
 process
-ing_use
+o
 r, when=
 out_
@@ -784,24 +784,22 @@
 r, when=
-o
 ut
-_time
+cnow
 )%0A      
@@ -804,13 +804,8 @@
     
-self.
 sess
@@ -816,20 +816,16 @@
 refresh(
-new_
 user)%0A  
@@ -890,20 +890,16 @@
 ship in 
-new_
 user.mem
@@ -930,16 +930,8 @@
 sert
- (int :=
  mem
@@ -955,104 +955,31 @@
 ring
-) is not None%0A            assert (end := int.end) is not None%0A            assert end %3C= out_time
+.before(single(utcnow))
 %0A%0A  
@@ -995,20 +995,16 @@
 ert not 
-new_
 user.hos
@@ -1017,28 +1017,24 @@
      assert 
-new_
 user.room is
@@ -1090,28 +1090,24 @@
      assert 
-new_
 user.address
@@ -1118,17 +1118,16 @@
 address%0A
-%0A
         
@@ -1191,12 +1191,8 @@
 ert 
-new_
 user

migrate `test_user` test to pytest #580