Repository: learningequality/kolibri
Issue: Lessons created in one class appear in other classes


### Observed behavior

Reported by Hiba and confirmed on `0.12.2-beta2` release.

### Expected behavior
Lessons should appear only in class they are created for (until coach expressly copies them to another). 

### User-facing consequences
Learners in other classes seeing lessons not intended for them.

### Errors and logs

…

### Steps to reproduce

1. Create 2-3 classes in the facility
2. Create a lesson in one class and activate it
3. Go to Learn and open other classes (make sure you are enrolled in them)
4. Observe the lesson created in the first class

### Context
 * Kolibri version: `0.12.2-beta2` release
 * Operating system: Windows
 * Browser: Chrome & FF



Changed files:
kolibri/plugins/learn/test/test_learner_classroom.py, kolibri/plugins/learn/serializers.py

diff --git a/kolibri/plugins/learn/serializers.py b/kolibri/plugins/learn/serializers.py
index 7ae9160cfe..5df8037d29 100644
--- a/kolibri/plugins/learn/serializers.py
+++ b/kolibri/plugins/learn/serializers.py
@@ -1,5 +1,6 @@
 from django.db.models import Q
 from django.db.models import Sum
+from django.db.models.query import F
 from rest_framework.serializers import JSONField
 from rest_framework.serializers import ModelSerializer
 from rest_framework.serializers import SerializerMethodField
@@ -106,17 +107,18 @@ class LearnerClassroomSerializer(ModelSerializer):
         lesson_assignments = HierarchyRelationsFilter(LessonAssignment.objects.all()) \\
             .filter_by_hierarchy(
                 target_user=current_user,
-                ancestor_collection=instance
+                ancestor_collection=F('collection')
         )
         filtered_lessons = Lesson.objects.filter(
             lesson_assignments__in=lesson_assignments,
-            is_active=True
+            is_active=True,
+            collection=instance,
         ).distinct()
 
         exam_assignments = HierarchyRelationsFilter(ExamAssignment.objects.all()) \\
             .filter_by_hierarchy(
                 target_user=current_user,
-                ancestor_collection=instance
+                ancestor_collection=F('collection')
         )
 
         filtered_exams = Exam.objects.filter(
diff --git a/kolibri/plugins/learn/test/test_learner_classroom.py b/kolibri/plugins/learn/test/test_learner_classroom.py
index 9417f6b409..f50dc5b530 100644
--- a/kolibri/plugins/learn/test/test_learner_classroom.py
+++ b/kolibri/plugins/learn/test/test_learner_classroom.py
@@ -161,3 +161,39 @@ class LearnerClassroomTestCase(APITestCase):
         self.client.login(username='learner', password='password')
         get_response = self.client.get(reverse(self.basename + '-detail', kwargs={'pk': self.own_classroom.id}))
         self.assertEqual(len(get_response.data['assignments']['lessons']), 1)
+
+    def test_learner_only_sees_lessons_for_enrolled_classroom(self):
+        classroom = Classroom.objects.create(name="Other Classroom", parent=self.facility)
+        lesson = Lesson.objects.create(
+            title='Lesson',
+            collection=classroom,
+            created_by=self.coach_user,
+            is_active=True,
+        )
+        LessonAssignment.objects.create(
+            lesson=lesson,
+            collection=classroom,
+            assigned_by=self.coach_user,
+        )
+        self.client.login(username='learner', password='password')
+        get_response = self.client.get(reverse(self.basename + '-list'))
+        self.assertEqual(len(get_response.data[0]['assignments']['lessons']), 0)
+
+    def test_learner_only_sees_lessons_for_single_classroom_when_enrolled_in_multiple(self):
+        classroom = Classroom.objects.create(name="Other Classroom", parent=self.facility)
+        classroom.add_member(self.learner_user)
+        lesson = Lesson.objects.create(
+            title='Lesson',
+            collection=self.own_classroom,
+            created_by=self.coach_user,
+            is_active=True,
+        )
+        LessonAssignment.objects.create(
+            lesson=lesson,
+            collection=self.own_classroom,
+            assigned_by=self.coach_user,
+        )
+        self.client.login(username='learner', password='password')
+        get_response = self.client.get(reverse(self.basename + '-list'))
+        total_lessons = len(get_response.data[0]['assignments']['lessons']) + len(get_response.data[1]['assignments']['lessons'])
+        self.assertEqual(total_lessons, Lesson.objects.count())