def test_owned_stations(self, repository: Repository): """Tests that it's possible to get a list of stations a given user is allowed to manage.""" stations = repository.owned_stations( 5) # get the list of stations that user with user_id=5 owns. self.assertEqual(len(stations), 1) self.assertEqual(stations[0]['name'], 'TKiS-1') self.assertEqual(stations[0]['station_id'], 1)
def login(): repository = Repository() if current_user.is_authenticated: stations = repository.owned_stations(current_user.get_id()) # list of stations l = " ".join(f"{s['name']}({s['station_id']})" for s in stations) app.logger.info("Authenticated user %s, owner of %s" % (current_user.username, l)) return render_template("login.html", user=current_user, stations=stations) form = LoginForm() if form.validate_on_submit(): app.logger.info( "Login requested for user %s, pass=%s, remember_me=%s" % (form.username.data, form.password.data, form.remember.data)) user = repository.read_user(user=form.username.data) if user is None: app.logger.info("Login failed: invalid username: %s" % form.username.data) flash("Invalid username.") return redirect(url_for("login")) u = ApplicationUser(user) if not u.check_password(form.password.data): app.logger.info("Login failed: invalid password %s for user %s" % (form.password.data, form.username.data)) flash("Invalid password.") return redirect(url_for("login")) if u.role == UserRole.BANNED: app.logger.info( "Login failed: attempt to login into disabled account %s" % form.username.data) flash("Account disabled.") return redirect(url_for("login")) app.logger.info("Login successful for user %s" % form.username.data) login_user(u, remember=form.remember.data) next_page = request.args.get("next") if not next_page or url_parse(next_page).netloc != "": next_page = url_for("index") return redirect(next_page) return render_template("login.html", form=form)